L298N code not proper

Discuss everything related to Arduino - Open-source electronic prototyping platform allowing to create interactive electronic objects.
vkarpuram
Posts: 11
Joined: Thu Nov 17, 2011 4:46 am

L298N code not proper

Postby vkarpuram » Sun Oct 14, 2012 5:30 am

int motor1Left = 5;// defines pin 5 as connected to the motor
int motor1Right= 6;// defines pin 6 as connected to the motor
int motor2Left = 9;// defines pin 9 as connected to the motor
int motor2Right = 3;// defines pin 3 as connected to the motor

int channel1 = 10; // defines the channels that are connected
int channel2 = 11;// to pins 10 and 11 of arduino respectively

int Channel1 ; // Used later to
int Channel2 ; // store values

void setup ()
{
pinMode (motor1Left, OUTPUT);// initialises the motor pins
pinMode (motor1Right, OUTPUT);
pinMode (motor2Left, OUTPUT);
pinMode (motor2Right, OUTPUT);// as outputs
pinMode (channel1, INPUT);// initialises the channels
pinMode (channel2, INPUT);// as inputs
Serial.begin (9600); // Sets the baud rate to 9600 bps


}

void loop ()
{
Channel1 = (pulseIn (channel1, HIGH)); // Checks the value of channel1
// Serial.println (Channel1); //Prints the channels value on the serial monitor

if (Channel1 > 1400 && Channel1 < 1600 ) /*If these conditions are true, do the following. These are the values that I got from my transmitter, which you may customize according to your transmitter values */
{
digitalWrite (motor1Left, LOW); // Sets both the
digitalWrite (motor1Right, LOW);// motors to low
}

if (Channel1 < 1400) // Checks if Channel1 is lesser than 1400
{
digitalWrite (motor2Left, LOW);// Turns the right
digitalWrite (motor1Right, LOW); // motor forward // MOVES THE BOT FORWARD
digitalWrite (motor1Left, HIGH);// Turns the left
digitalWrite (motor2Right, HIGH);// motor forward



}
if (Channel1 > 1600) // Checks if Channel1 is greater than 1600
{
digitalWrite (motor1Left, LOW);// Turns the left
digitalWrite (motor2Right, LOW);// motor backward
digitalWrite (motor2Left, HIGH); //MOVES THE BOT BACKWARDS
digitalWrite (motor1Right, HIGH);// motor backward


}
Channel2 = (pulseIn (channel2, HIGH)); // Checks the value of channel1
// qqqqSerial.println (Channel2); //Prints the channels value value on the serial monit
if (Channel2 > 1400 && Channel2 < 1600 ) // If these conditions are true, do the following
{
digitalWrite (motor2Left, LOW);// Sets both the
digitalWrite (motor2Right, LOW);// motors to low
}

if (Channel2 < 1400) // Checks if Channel2 is lesser than 1400
{
digitalWrite (motor1Left, HIGH);// Turns the left
digitalWrite (motor1Right, LOW); // motor backward
digitalWrite (motor2Left, HIGH);// Turns the right
digitalWrite (motor2Right, LOW);// motor forward
}
if (Channel2 > 1600) // Checks if Channel2 is greater than 1600
{
digitalWrite (motor1Left, LOW);// Turns the left
digitalWrite (motor1Right, HIGH); // motor forward
digitalWrite (motor2Left, LOW);// Turns the right
digitalWrite (motor2Right, HIGH);// motor backward
}
}

/* NOTE - Check the values of the channels that you get in the serial monitor
and adjust the values in the if statements accordingly. In my case when the stick
was centered, my readings were 1400 to 1470. When the stick was raised, the readings were
above 1470 and when it was lowered, the readings were below 1300.
*/




This is the code.....One motor moves very fast and one goes quite slow !!
abhimanyu
Posts: 94
Joined: Tue Oct 18, 2011 9:49 pm

Re: L298N code not proper

Postby abhimanyu » Sun Oct 14, 2012 7:31 am

There is no speed control defined in the code, i.e., no PWM signals.
The code just defines which motor should turn on/ off and in which direction.

If now you are facing a problem of one motor running slow and one fast, you should check your Hardware connections,
battery connections, battery voltage. Are you using separate power supply for both motors?
abhimanyu
Posts: 94
Joined: Tue Oct 18, 2011 9:49 pm

Re: L298N code not proper

Postby abhimanyu » Sun Oct 14, 2012 9:18 am

See this reference circuit from Texas Instruments' datasheet of their motor driver.
It connects a single motor in parallel mode thereby doubling the available current.
Attachments
ti drv8833 datasheet.jpg
ti drv8833 datasheet.jpg (65.63 KiB) Viewed 2041 times

Return to “Arduino”

Who is online

Users browsing this forum: No registered users and 1 guest