I am working on an Arduino Uno Project of a car with two motors. The motor driver board is an L9110 H-Bridge. Here is how everything is connected:
And here is the code I am trying to run:
//Define all the pins for the motors
#define motorPinA1 3 //A pin right motor
#define motorPinB1 5 //B pin right motor
#define motorPinA2 6 //A pin left motor
#define motorPinB2 9 //B pin left motor
//Movement functions
void stopCar();
void frwrd(int motorSpdRight, int motorSpdLeft);
void back(int motorSpdRight, int motorSpdLeft);
//Global Variables
int motorSpeedRight = 0; //The speed of the right motor
int motorSpeedLeft = 0; //The speed of the left motor
void setup() {
//Movement
pinMode(motorPinA1, OUTPUT);
pinMode(motorPinB1, OUTPUT);
pinMode(motorPinA2, OUTPUT);
pinMode(motorPinB2, OUTPUT);
//Serial Monitor for debugging
Serial.begin(9600);
motorSpeedLeft = 100;
motorSpeedRight = 100;
frwrd(100, 100);
delay(2000);
stopCar();
delay(1000);
back(100, 100);
delay(2000);
stopCar();
delay(1000);
}
void loop() {
Serial.print("Forward");
Serial.print("\n");
frwrd(motorSpeedRight, motorSpeedLeft);
delay(2000);
stopCar();
delay(1000);
back(motorSpeedRight, motorSpeedLeft);
delay(2000);
stopCar();
delay(1000);
}
// functions we need to make the code run
void stopCar(){
Serial.print("Stoping Car\n");
// Put motor 1 in halt
analogWrite(motorPinA1, 0);
analogWrite(motorPinB1, 0);
// Put motor 2 in halt
analogWrite(motorPinA2, 0);
analogWrite(motorPinB2, 0);
}
// For the farward direction A pins will be high
void frwrd(int motorSpdRight, int motorSpdLeft){
// Put motor 1 (Right) in forward motion with speed = motor_spd
analogWrite(motorPinA1, motorSpdRight);
analogWrite(motorPinB1, 0);
// Put motor 2 (Left) in forward motion with speed = motor_spd
analogWrite(motorPinA2, motorSpdLeft);
analogWrite(motorPinB2, 0);
/*
// Put motor 1 (Right) in forward motion
digitalWrite(motorPinA1, HIGH);
digitalWrite(motorPinB1, LOW);
// Put motor 2 (Left) in forward motion
digitalWrite(motorPinA2, HIGH);
analogWrite(motorPinB2, LOW);
*/
}
// For the backward direction B pins will be high
void back(int motorSpdRight, int motorSpdLeft){
// Put motor 1 (Right) in backward motion with speed = motor_spd
analogWrite(motorPinA1, 0);
analogWrite(motorPinB1, motorSpdRight);
// Put motor 2 (Left) in backward motion with speed = motor_spd
analogWrite(motorPinA2, 0);
analogWrite(motorPinB2, motorSpdLeft);
/*
// Put motor 1 (Right) in backward motion
digitalWrite(motorPinA1, LOW);
digitalWrite(motorPinB1, HIGH);
// Put motor 2 (Left) in backward motion
digitalWrite(motorPinA2, LOW);
digitalWrite(motorPinB2, HIGH);
*/
}
Pins 3, 5, 6 and 9 are PWM so I can alalogWrite()
to them and control the speed of the motors. Code uploads fine but although both motors move forward normally motor 1 (on pins 3, 5) doesn't move backward. The strange thing is that if I use digitalWrite
(the commented code in the two functions) instead everything works normally but the motors move in full speed so it's not usable for my project. I have tested all the pins in other projects and everything worked fine so I know my board is not faulty.
I have also taken some measurements with a multimeter while the code was running. In the forward motion, both pairs of pins measured 1.95V. In the backward motion, the problematic pins measured 0V.
Any help would be appreciated!!
motorPinA1
is not as clear asmotor1fwd
ormotorLeftFwd