Here’s an example of a program that we ran:
#define PWMA 0
#define AIN1 2
#define AIN2 1
#define BIN1 4
#define BIN2 5
#define PWMB 6
#define STBY 3
#define motor_A 0
#define motor_B 1
#define FORWARD 1
#define REVERSE 0
#define RIGHT 1
#define LEFT 0
void setup()
{
pinMode(PWMA,OUTPUT);
pinMode(AIN1,OUTPUT);
pinMode(AIN2,OUTPUT);
pinMode(PWMB,OUTPUT);
pinMode(BIN1,OUTPUT);
pinMode(BIN2,OUTPUT);
pinMode(STBY,OUTPUT);
motor_standby(false); //Must set STBY pin to HIGH in order to move
}
void loop()
{
motor_brake(); //STOP for 1s
delay(1000);
motor_drive(FORWARD, 150); //Move FORWARD at full speed for 1s
delay(1000);
motor_brake(); //STOP for 1s
delay(1000);
motor_drive(REVERSE, 150); //REVERSE at about half-speed for 1s
delay(1000);
motor_brake(); //STOP for 1s
delay(1000);
motor_turn(RIGHT, 150, 150); //Spin RIGHT for 0.5s
delay(1000);
motor_brake();
delay(100000000);
}
//Turns off the outputs of the Motor Driver when true
void motor_standby(char standby)
{
if (standby == true) digitalWrite(STBY,LOW);
else digitalWrite(STBY,HIGH);
}
//Stops the motors from spinning and locks the wheels
void motor_brake()
{
digitalWrite(AIN1,1);
digitalWrite(AIN2,1);
digitalWrite(PWMA,LOW);
digitalWrite(BIN1,1);
digitalWrite(BIN2,1);
digitalWrite(PWMB,LOW);
}
//Controls the direction the motors turn, speed from 0(off) to 255(full speed)
void motor_drive(char direction, unsigned char speed)
{
if (direction == FORWARD)
{
motor_control(motor_A, FORWARD, speed);
motor_control(motor_B, FORWARD, speed);
}
else
{
motor_control(motor_A, REVERSE, speed);
motor_control(motor_B, REVERSE, speed);
}
}
//You can control the turn radius by specifying the speed of each motor
//Set both to 255 for it to spin in place
void motor_turn(char direction, unsigned char speed_A, unsigned char speed_B )
{
if (direction == RIGHT)
{
motor_control(motor_A, REVERSE, speed_A);
motor_control(motor_B, FORWARD, speed_B);
}
else
{
motor_control(motor_A, FORWARD, speed_A);
motor_control(motor_B, REVERSE, speed_B);
}
}
void motor_control(char motor, char direction, unsigned char speed)
{
if (motor == motor_A)
{
if (direction == FORWARD)
{
digitalWrite(AIN1,HIGH);
digitalWrite(AIN2,LOW);
}
else
{
digitalWrite(AIN1,LOW);
digitalWrite(AIN2,HIGH);
}
analogWrite(PWMA,speed);
}
else
{
if (direction == FORWARD) //Notice how the direction is reversed for motor_B
{ //This is because they are placed on opposite sides so
digitalWrite(BIN1,LOW); //to go FORWARD, motor_A spins CW and motor_B spins CCW
digitalWrite(BIN2,HIGH);
}
else
{
digitalWrite(BIN1,HIGH);
digitalWrite(BIN2,LOW);
}
analogWrite(PWMB,speed);
}
}
In this case both motors turn fine with 150 PWM.
If we change the code to: motor_drive(FORWARD, 120) or motor_turn(RIGHT, 120, 120)
Then only motor B will turn.
If we change the code to: motor_drive(FORWARD, 130) or motor_turn(RIGHT, 130, 130)
Then both motors will turn, and motor A will go according to 130 PWM.
It seems to be around 128 PWM that it suddenly starts turning.
It is connected as follows:
The arduino is powered by a 9V in Vin and GND.
Outputs 0-6 on the arduino go in the motor driver.
The logic on the motor driver is powered by 5V from the arduino.
The other 9V is then connected to Vm and GND on the motor driver(does it matter what GND?)
The motors are connected to A01,A02,B01 and B02 on the driver.
Then we have two GND left on the driver that are connected together and into GND on the arduino.
I’ve tried another power supply and we still had the same problem.
I measured the stall current for the motors and it is 300mA.
Thank you for the reply!