Hi,
I have a custom board with TB6612FNG and Atmega328P with arduino uno bootloader. When I program it, the motor controller works well with PWM control. However, when I switch power from USB power to battery power, the motor no longer works, it kind of jitters and no motor movement. However, if I update speed to 255 or -255, it works on full speed.
So to sum it up, the motor controller works nice at full speed irrespective of USB power or battery power. However, if I reduce speed to anything but 255 or -255, it just does not work on battery power, but only on USB power. When I connect both battery and USB power, even then it does not work on lower speeds.
Battery (VM) is connected to a regulator (1117 - 5V - VCC) which powers the board and the motor controller and direct power - VM is connected to TB6612FNG VM pin.
Please find below code and also the schematic of custom board. Any help is appreciated.
#define PWMA 5
#define AIN1 10
#define AIN2 4
#define BIN1 7
#define BIN2 8
#define PWMB 6
#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);
delay(5000);
}
void loop()
{
motor_brake();
delay(1000);
motor_drive(FORWARD, 150); // if this replaced with 255, it works on battery also, but at full speed
delay(1000);
motor_brake();
delay(1000);
motor_drive(REVERSE, 150); // if this replaced with 255, it works on battery also, but at full speed
delay(1000);
motor_brake();
delay(1000);
motor_turn(RIGHT, 150, 150); // if this replaced with 255, it works on battery also, but at full speed
delay(1000);
motor_brake();
delay(1000);
}
void motor_brake()
{
digitalWrite(AIN1, 1);
digitalWrite(AIN2, 1);
digitalWrite(PWMA, LOW);
digitalWrite(BIN1, 1);
digitalWrite(BIN2, 1);
digitalWrite(PWMB, LOW);
}
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);
}
}
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)
{
digitalWrite(BIN1, LOW);
digitalWrite(BIN2, HIGH);
}
else
{
digitalWrite(BIN1, HIGH);
digitalWrite(BIN2, LOW);
}
analogWrite(PWMB, speed);
}
}

