Ok heres the problem, no matter what I do I can't get the H-bridge to work properly.
I've triple checked my connections, here is the test code i'm using
// h bridge test
const int switchPin = 12; // switch input const int motor1Pin = 7; // H-bridge leg 1 (pin 2, 1A) const int motor2Pin = 6; // H-bridge leg 2 (pin 7, 2A) const int ledPin = 13; // LED const int enablePin1 =8; // Enable motor pin
void setup() { // set the switch as an input: pinMode(switchPin, INPUT);
// set all the other pins you're using as outputs: pinMode(motor1Pin, OUTPUT); pinMode(motor2Pin, OUTPUT); pinMode(ledPin, OUTPUT);
// set enablePin high so that motor can turn on:
digitalWrite(enablePin1, HIGH);
// blink the LED 3 times. This should happen only once. // if you see the LED blink three times, it means that the module // reset itself,. probably because the motor caused a brownout // or a short. blink(ledPin, 3, 100); }
void loop() { // if the switch is high, motor will turn on one direction:
if (digitalRead(switchPin) == HIGH) { digitalWrite(motor1Pin, LOW); // set leg 1A of the H-bridge low digitalWrite(motor2Pin, HIGH); // set leg 2A of the H-bridge high
} // if the switch is low, motor will turn in the other direction:
else { digitalWrite(motor1Pin, HIGH); // set leg 1A of the H-bridge high digitalWrite(motor2Pin, LOW); // set leg 2A of the H-bridge low
} }
/* blinks an LED */ void blink(int whatPin, int howManyTimes, int milliSecs) { int i = 0; for ( i = 0; i < howManyTimes; i++) { digitalWrite(whatPin, HIGH); delay(milliSecs/2); digitalWrite(whatPin, LOW); delay(milliSecs/2); } }
I would suggest to connect all 4 GND pins (4,5,12,13) . Don’t know, if they are internally connected or not, nor if this can cause any problems.
What kind of battery and motor you’re using? Switching a motor from full speed forward to full speed reverse isn’t wise. That could result in a voltage drop, that could cause a controller reset. Better use PWM and ramp the motor down to zero, reverse direction, than ramp up to full speed.
I haven’t gotten that far yet! PWM is still confusing me as far as how to impliment it. I know it’s probally very simple but this is my first attempt at robot building.
PWM is simple to use with an Arduino. Just connect the Enable pin from the motor driver to an PWM capable pin (3,5,6,9,10,11). Use AnalogWrite(pwmval); to control the electrical power for the motor. A pwmval of 0 stops the motor, a pwmval of 255 runs the motor with fullspeed. Any pwmval between will result in a different speed between stop and full speed.
A ramp function can be used to accelerate the motor to a desired speed in a given time.
I’m writing on a PWM tutorial currently. Hope to get it finished this weekend.