Pages: [1]   Go Down
Author Topic: [Working Code]How to use the L298 Compact Motor Driver  (Read 4133 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 14
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

OK, Got this little puppy for a Sumo project.
Put it together (only burned myself once) and tested it IAW the directions... it's good to go (yea!)
Next, I wrote this:
Code:
// Motor Controls
const int PWMELPin = 10; // Enable PWM Left Motor
const int PWMERPin = 11; // Enable PWM Right Motor
const int L1Pin = 2; // L1 = Left Motor Lead 1
const int L2Pin = 3; // L2 = Left Motor Lead 2
const int L3Pin = 4; // L3 = Right Motor Lead 1
const int L4Pin = 5; // L4 = Right Motor Lead 2
const int LEDPin = 13; // LED
const int Duration = 1000;
void setup()
    {
    pinMode(PWMELPin, OUTPUT);
    pinMode(PWMERPin, OUTPUT);
    pinMode(L1Pin, OUTPUT);
    pinMode(L2Pin, OUTPUT);
    pinMode(L3Pin, OUTPUT);
    pinMode(L4Pin, OUTPUT);
    pinMode(LEDPin, OUTPUT);
    }
//Main Loop
void loop()
    {
    while (true)
        {
        MotorL(0, 1);
        MotorR(0, 1);
        digitalWrite(LEDPin, LOW);
        delay(Duration);
        MotorL(1, 1);
        MotorR(1, 1);
        digitalWrite(LEDPin, HIGH);
        delay(Duration);
        MotorL(0, 0);
        MotorR(0, 0);
        digitalWrite(LEDPin, LOW);
        delay(Duration);
        MotorL(1, 0);
        MotorR(1, 0);
        digitalWrite(LEDPin, HIGH);
        delay(Duration);
        continue;
        }
    }
// ***********************************************************************
// Motor Driver Routines**************************************************
// ***********************************************************************
// (Direction, Speed)
// Direction
// 1=Forward
// 0= Reverse
// Speed
// 1 = High/250
// 0 = Low/50
// ***********************************************************************
void MotorL(int Direction, int Speed)
    {
    // Left Motor
    if (Speed = 1)
        {
        analogWrite(PWMELPin, 250);
        }
    else
        {
        analogWrite(PWMELPin, 50);
        }
    if (Direction = 1)
        {
        digitalWrite(L1Pin, HIGH);
        digitalWrite(L2Pin, LOW);
        }
    else
        {
        digitalWrite(L1Pin, LOW);
        digitalWrite(L2Pin, HIGH);
        }
    }
// ***********************************************************************
void MotorR(int Direction, int Speed)
    {
    // Left Motor
    if (Speed = 1)
        {
        analogWrite(PWMELPin, 250);
        }
    else
        {
        analogWrite(PWMELPin, 50);
        }
    if (Direction = 1)
        {
        digitalWrite(L1Pin, HIGH);
        digitalWrite(L2Pin, LOW);
        }
    else
        {
        digitalWrite(L1Pin, LOW);
        digitalWrite(L2Pin, HIGH);
        }
    }
// ***********************************************************************
Seems simple enough...
Connected a 9v battery to +/- on the Motor Controller
Attached all 6 control wires as indicated in the code above
put a motor on each output
Connected the Arduino to laptop via USB (jumpered to draw power from USB)
Loaded the script and....

LED on 13 slowly blinks,so I know the code is running, but no joy on the motor control direction/speed controlling...

What (probably OBVIOUS) thing am I missing?
is there any other connection I need to make?
Pullup/Pulldown/pullout resistors?
« Last Edit: March 16, 2011, 11:16:55 am by Everseeker » Logged

0
Offline Offline
Shannon Member
****
Karma: 206
Posts: 12113
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Small 9V batteries aren't usually up to driving a motor, perhaps that's the issue?
Logged

[ I won't respond to messages, use the forum please ]

0
Offline Offline
Newbie
*
Karma: 0
Posts: 14
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

well, couple problems:
1. The motors are not that big (the GM8's actually)
2. Meter shows 0v to them

I am suspecting there's something more ... basic ... that I am missing
Am I supposed to ... I dunno... Connect the grounds of the 2 together?
stick that regulated 5V somewhere? (No, not gonna put it THERE)

What should I do next?
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 14
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

OK, I rechecked everything... then decided to bypass the entire Motor Controller, passing all 6 thru LEDS

I quickly discovered 1 issue...
My Left and Right motor controller subroutines were the same
EXACTLY the same... right down to the pins (D'oh!)
here's the "correct" code:
Code:
// Motor Controls
const int PWMELPin = 10; // Enable PWM Left Motor
const int PWMERPin = 11; // Enable PWM Right Motor
const int L1Pin = 2; // L1 = Left Motor Lead 1
const int L2Pin = 3; // L2 = Left Motor Lead 2
const int L3Pin = 4; // L3 = Right Motor Lead 1
const int L4Pin = 5; // L4 = Right Motor Lead 2
const int LEDxPin = 12; // LEDx
const int LEDPin = 13; // LED
const int Duration = 1000;
void setup()
    {
    pinMode(PWMELPin, OUTPUT);
    pinMode(PWMERPin, OUTPUT);
    pinMode(L1Pin, OUTPUT);
    pinMode(L2Pin, OUTPUT);
    pinMode(L3Pin, OUTPUT);
    pinMode(L4Pin, OUTPUT);
    pinMode(LEDPin, OUTPUT);
    pinMode(LEDxPin, OUTPUT);    }
//Main Loop
void loop()
    {
    while (true)
        {
        MotorL(0, 1);
        MotorR(0, 1);
        digitalWrite(LEDPin, LOW);
        delay(Duration);
        MotorL(1, 1);
        MotorR(1, 1);
        digitalWrite(LEDPin, HIGH);
        delay(Duration);
        MotorL(0, 0);
        MotorR(0, 0);
        digitalWrite(LEDPin, LOW);
        delay(Duration);
        MotorL(1, 0);
        MotorR(1, 0);
        digitalWrite(LEDPin, HIGH);
        delay(Duration);
        continue;
        }
    }
// ***********************************************************************
// Motor Driver Routines**************************************************
// ***********************************************************************
// (Direction, Speed)
// Direction
// 1=Forward
// 0= Reverse
// Speed
// 1 = High/250
// 0 = Low/50
// ***********************************************************************
void MotorL(int Direction, int Speed)
    {
    // Left Motor
    if (Speed = 1)
        {
        analogWrite(PWMELPin, 250);
        }
    else
        {
        analogWrite(PWMELPin, 50);
        }
    if (Direction = 1)
        {
        digitalWrite(LEDxPin, HIGH);
        digitalWrite(L1Pin, HIGH);
        digitalWrite(L2Pin, LOW);
        }
    else
        {
        digitalWrite(LEDxPin, LOW);
        digitalWrite(L1Pin, LOW);
        digitalWrite(L2Pin, HIGH);
        }
    }
// ***********************************************************************
void MotorR(int Direction, int Speed)
    {
    // Left Motor
    if (Speed = 1)
        {
        analogWrite(PWMERPin, 250);
        }
    else
        {
        analogWrite(PWMERPin, 50);
        }
    if (Direction = 1)
        {
        digitalWrite(L3Pin, HIGH);
        digitalWrite(L4Pin, LOW);
        }
    else
        {
        digitalWrite(L3Pin, LOW);
        digitalWrite(L4Pin, HIGH);
        }
    }
// ***********************************************************************
That said, when I run this, both "E", L1, and L3 pins go on(High),  "L2 and L4" pins stay off(Low) they never move, but LED pin 13 slowly blinks

Ummm... when I add a pin 12 LED and stick that in the L side subroutine (as shown above).... 12 never blinks/flickers. it just stays ON......
as if the subroutines are not working...????
(NEW to C programming... am I missing something else???)
« Last Edit: March 14, 2011, 08:01:36 pm by Everseeker » Logged

0
Offline Offline
Shannon Member
****
Karma: 206
Posts: 12113
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You used current limiting resistors on those LEDs?  You mustn't overload output pins.

And yes you must link grounds !!  A signal only exists relative to ground.
Logged

[ I won't respond to messages, use the forum please ]

0
Offline Offline
Newbie
*
Karma: 0
Posts: 14
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes, resistors in place.
Arduino powered by usb, motor controller,  by a 9v battery.
Are you SURE I should run a wire from the 9v -  to the Arduino ground?

Edit to add:
when I run other programs, such as "Cylon lights" I see all the lines out DO work, and all the LEDs are connected to my breadboard correctly

this is looking more and more like an issue with my code...

Am I configuring the Function correctly?
it looks like NOTHING happens in them.............
« Last Edit: March 15, 2011, 07:19:35 am by Everseeker » Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 14
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

OK, Here is the final, WORKING code. Feel free to use this as a starting point for projects...
Code:
// Motor Controls
const int PWMELPin = 10; // Enable PWM Left Motor
const int PWMERPin = 11; // Enable PWM Right Motor
const int L1Pin = 2; // L1 = Left Motor Lead 1
const int L2Pin = 3; // L2 = Left Motor Lead 2
const int L3Pin = 4; // L3 = Right Motor Lead 1
const int L4Pin = 5; // L4 = Right Motor Lead 2
const int LEDPin = 13; // Telltale LED
const int Duration = 2000;
void setup()
{
    pinMode(PWMELPin, OUTPUT);
    pinMode(PWMERPin, OUTPUT);
    pinMode(L1Pin, OUTPUT);
    pinMode(L2Pin, OUTPUT);
    pinMode(L3Pin, OUTPUT);
    pinMode(L4Pin, OUTPUT);
    pinMode(LEDPin, OUTPUT);
}

//Main Loop
void loop()
{
    MotorL(0, 1);
    MotorR(0, 1);
    digitalWrite(LEDPin, LOW);
    delay(Duration);
    MotorL(1, 1);
    MotorR(1, 1);
    digitalWrite(LEDPin, HIGH);
    delay(Duration);
    MotorL(0, 0);
    MotorR(0, 0);
    digitalWrite(LEDPin, LOW);
    delay(Duration);
    MotorL(1, 0);
    MotorR(1, 0);
    digitalWrite(LEDPin, HIGH);
    delay(Duration);
}

// ***********************************************************************
// Motor Driver Routines**************************************************
// ***********************************************************************
// Function(Direction, Speed)
// Direction
// 1=Forward
// 0= Reverse
// Speed
// 1 = High/250
// 0 = Low/20
// ***********************************************************************
void MotorL(int Direction, int Speed)
{
    // Left Motor
    if (Speed == 1)
    {
        analogWrite(PWMELPin, 250);
    }
    else
    {
        analogWrite(PWMELPin, 20);
    }
    if (Direction == 1)
    {
        digitalWrite(L1Pin, HIGH);
        digitalWrite(L2Pin, LOW);
    }
    else
    {
        digitalWrite(L1Pin, LOW);
        digitalWrite(L2Pin, HIGH);
    }
}

// ***********************************************************************
void MotorR(int Direction, int Speed)
{
    // Right Motor
    if (Speed == 1)
    {
        analogWrite(PWMERPin, 250);
    }
    else
    {
        analogWrite(PWMERPin, 20);
    }
    if (Direction == 1)
    {
        digitalWrite(L3Pin, HIGH);
        digitalWrite(L4Pin, LOW);
    }
    else
    {
        digitalWrite(L3Pin, LOW);
        digitalWrite(L4Pin, HIGH);
    }
}

// ***********************************************************************
Notes:
1. Do NOT bind the - pin from the L298 to the Arduino Ground... that is not needed
2. Do giggle at me for spending forever trying to figure out what was wrong, before someone pointed out that Speed = 0 is NOT the same as Speed == 0....sigh......
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

just to confirm, youre setting your L1-L2/L3-L4 pins to zeroes and ones for direction and using PWM for the 2 separate (independent) Enable lines?
Logged

WI
Offline Offline
Newbie
*
Karma: 0
Posts: 17
z0t
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You will want all the grounds connected.
Logged

Pages: [1]   Go Up
Jump to: