I've looked at other questions, this one came the closest, but my problem is not in terms of power supply.
To drive multiple DC motors from an Arduino Mega 2560, I got a few HG7881-based dual H-bridge drivers. Looking at the datasheet of that chip, they seem to be sufficient. Testing the motors with my bench supply at 9V, they never reach 400mA when stalled, which is still far below the typical continuous output current of 600mA.
When I drive just a single motor through one of these H-bridges, it works 100% fine, even PWM mostly works, down to about 30% duty cycle.
However, once I connect a second H-bridge to two more pins of the Arduino, and just set those to OUTPUT
, the motor stops working:
int motor0_forward = 2;
int motor0_backward = 3;
int motor1_forward = 4;
int motor1_backward = 5;
int led_pin = 13;
void setup()
{
// Set up motor 0
pinMode(motor0_forward, OUTPUT);
pinMode(motor0_backward, OUTPUT);
digitalWrite(motor0_forward, LOW);
digitalWrite(motor0_backward, LOW);
// Set up control LED
pinMode(led_pin, OUTPUT);
digitalWrite(led_pin, LOW);
// Set up motor 1
// Everything works if these four lines are commented out!
pinMode(motor1_forward, OUTPUT); // ???
pinMode(motor1_backward, OUTPUT); // ???
digitalWrite(motor1_forward, LOW); // ???
digitalWrite(motor1_backward, LOW); // ?*/
}
void loop()
{
// Blink control LED
digitalWrite(led_pin, HIGH);
delay(50);
digitalWrite(led_pin, LOW);
// Test motor 0
digitalWrite(motor0_forward, HIGH);
delay(50);
digitalWrite(motor0_forward, LOW);
digitalWrite(motor0_backward, HIGH);
delay(50);
digitalWrite(motor0_backward, LOW);
delay(500);
}
Instead of commenting the lines out, I can also set the pinMode
to INPUT
to get the pins into Hi-Z, and that is the workaround I am using in my actual program.
However, I would like to understand what's going on here.
I have the on-board LED blinking to make sure there's no brown-out or reset happening, and it blinks steady in short pulses, so no issue here.
As the outputs for motor1
are set to low, the connected motor is not driven. The load should at maximum be 20mA, according to the datasheet, but none of that should be supplied by the Arduino. The lab supply shows ~43mA current draw, which seems a little higher than advertised but still ok for two H-bridges. So I'm fairly certain that there's no overcurrent situation on the Arduino itself.
I tried using two separate boards, no difference. I tried using other pins, even pins without PWM output, no luck. I tried putting in series resistors to limit the current somewhat, but the highest I got with a single motor still working was 330R, and that didn't solve the problem either.
What does work is disconnecting the motor1
H-Bridge from the Arduino physically. When I do that, motor0
immediately starts moving again. With an analog oscilloscope, I could confirm that the signal sent out on the motor0
pins by the Arduino does not change whatsoever between motor1
being connected or not.
So, what the hell is going on here? What else can I check?