I did a search on this but didn’t find a similar question. I’m having a problem controlling two motors, and I’ve tried two different boards – the Arduino motor shield and the Sparkfun ArduMoto, both using the L298 H-bridge. Basically, I can get both motors to go forward, but I can’t get either (or both) motors to go in reverse. I’ve made the code as simple as possible:
#define motR_PWM_PIN 3
#define motR_DIR_PIN 5
#define motR_BRK_PIN 8
#define motL_PWM_PIN 4
#define motL_DIR_PIN 6
#define motL_BRK_PIN 9
byte speedHIGH = 200;
byte speedMID = 100;
byte speedLOW = 50;
//==============================================================
void setup(){
Serial.begin(19200);
//SET DIGITAL OUTPUT PINS
pinMode(motR_PWM_PIN, OUTPUT); pinMode(motL_PWM_PIN, OUTPUT);
pinMode(motR_DIR_PIN,OUTPUT); pinMode(motL_DIR_PIN,OUTPUT);
pinMode(motR_BRK_PIN,OUTPUT); pinMode(motL_BRK_PIN,OUTPUT);
digitalWrite(motR_BRK_PIN,0);
digitalWrite(motL_BRK_PIN,0);
}
void loop(){
//both forward - this works fine, both motors forward, motor board LEDs light up
digitalWrite(motR_DIR_PIN, HIGH);
digitalWrite(motL_DIR_PIN, HIGH);
analogWrite(motR_PWM_PIN, speedMID);
analogWrite(motL_PWM_PIN, speedMID);
delay(2000);
//if I set either (or both) dir pins to low, NEITHER motor gets any current, and the motor board LEDs are off
digitalWrite(motR_DIR_PIN, LOW);
digitalWrite(motL_DIR_PIN, LOW);
analogWrite(motR_PWM_PIN, speedMID);
analogWrite(motL_PWM_PIN, speedMID);
delay(2000);
}
And still I get the same thing. No current goes to the motors when the direction pins are set to 0/LOW. It does not matter what I set the PWM to, and it happens even if the only thing I do is run both motors with the direction pins low, so I know it isn’t an overcurrent problem.
Has anyone seen this? Anyone have any ideas? Is it something with the L298?
Here are the best pics my buddy and I could get (attached).
It might be hard to follow the wiring from the pics. Basically the shield is separated from the Mega. We connect pins 3 and 4 on the Mega to the motor PWM pins on the shield, pins 5 and 6 to the direction pins on the shield, and (for the Arduino board only - didn't need to for the ArduMoto) pins 8 and 9 on the mega to the brake pins on the shield. The motors are connected to the shield via the A and B posts. The Mega gets power from USB or a 9V; the shield is powered by a 4-cell NiMH pack that is giving ~5.1V. We also tried running the shield off the Mega's 5V output just to see if it made a difference, but it didn't.
Thanks for the response! You may be on to something. I will try playing with the brake settings. Let me see if I understand the schematic.
From the schematic of the motor shield you posted, it would appear that the L298 uses its two In pins to control the motor direction via an NAND logic gate. To reduce the number of pins needed, the motor shield has its own NAND logic gate that uses its Dir and Brake pins. So if I read it right, the Brake pin must always be HIGH to get motion, as you are suggesting. If DirA is HIGH, In1 on the L298 is HIGH, so In2 must be LOW meaning DirA = BrakeA = HIGH. If DirA is LOW, In1 on the L298 is LOW, so In2 must be HIGH meaning DirA != Brake A -> BrakeA = HIGH.
I was stuck at first on reading the L298 schematic but now I see how the logic works with In1 and In2. (Sorry, I'm just a simple Mech Eng!)
Yes, you seem to be missing something, and that something might also be the reason why you can't change direction.
Your motor should be connected between two output pins of the L298.
That would be out1 and out2 or out3 and out4.
If you have only 1 of these connected to your motor, you can't control direction.
In knut_ny's drawing you can see DIR and DIR#.
If you see something like that, it means DIR# should be read as DIRNOT, it is the inverse of DIR.
A better known way to mark this is like this: !DIR.
So, first you set direction.
You do that by setting In1 HIGH and In2 LOW, or the other way around for the other direction.
Then you set EnA HIGH to get your motor running at full speed, or you control that speed by applying PWM to it.
By using the inverting gate, you can control the direction with only one pin instead of 2.
So this part was added to the shield for your convenience.
You shouldn't mistake the gates on the shield with those that are internal in the chip.
If you have In1 and In2 LOW, the output will be idle.
But if you have both HIGH, the outputs will be shorted, which is effectively a brake (try to turn the motor by hand if the wires are shorted, and if they are open).
That brake disable offers you a way to have a brake.
If you do not need it, you can tie it to a HIGH level without controlling it.