Language: EN

arduino-motor-dc-tb6612fng

Controlling two DC motors with Arduino and TB6612FNG driver

What is a TB6612FNG?

The TB6612FNG is a motor driver that allows us to control two DC motors from Arduino, varying both the speed and direction of rotation.

The TB6612FNG can be considered an improved version of the L298N. Like the L298N, it is internally formed by two H-bridges, along with the necessary electronics to simplify its use and eliminate possible short circuits due to user errors.

However, in the case of the TB6612FNG, the H-bridges are formed by MOSFET transistors, instead of BJT transistors like in the L298N. This allows the TB6612FNG to have better efficiency and smaller dimensions than the L298N.

The TB6612FNG also allows for the control of higher current intensities, being able to supply 1.2A per channel continuously, and 3.2A peak. Remember that the L298N has a theoretical maximum current of 2A, but losses cause it to practically only be able to supply 0.8-1A.

In addition, the TB6612FNG does not have the voltage drop that the L298N has due to its BJT transistors, which could be greater than 3V. Instead, the TB6612FNG behaves like a small 0.5 Ohm resistor.

As a downside, the TB6612FNG can provide lower voltages, up to 13.5V (compared to the 35V of the L298N). Furthermore, it is somewhat more difficult to assemble, as L298N boards frequently incorporate connection boards that allow for easy motor connection.

Finally, the protection against induced currents is somewhat more limited than that of the L298N, so we may experience an Arduino reboot when powering medium or large loads.

The TB6612FNG has two channels, so it is possible to control two DC motors independently. It can also control a single stepper motor, although in general, we will prefer to use specific controllers.

In each channel, we can control the direction of rotation and the speed, for which it accepts a PWM signal with a maximum frequency of 100 kHz (well below the normal PWM range in Arduino).

The TB6612FNG has thermal protection, reverse current protection in the motor power supply, filtering capacitors in both power lines, low voltage detection, and protections against induced currents in the motors.

The TB6612FNG also incorporates a Standby mode, which completely deactivates the driver, entering a power-saving mode.

The TB6612FNG driver can be used in electronics and robotics projects. It is widely used in electronic and robotics projects, due to its ease of use, low cost, and good value for money.

Price

The TB6612FNG driver is an inexpensive device. Initially, the TB6612FNG was more expensive than the L298N, but prices have dropped, and currently the TB6612FNG is cheaper than the L298N.

You can find a TB6612FNG for €1.10 from international sellers on AliExpress or eBay.

arduino-tb6612fng-componente

Assembly diagram

The assembly diagram is not too complicated. On one hand, we supply the motor voltage from an external power source, through the VM pin. The maximum voltage is 15V.

Additionally, we have to power the module electronics through the VCC pin. The voltage range for VCC is 2.7 to 5.5V.

For module control, the pins AIN1, AIN2, and PWMA control channel A, while the pins BIN1, BIN2, and PWMB control channel B.

Finally, the STBY pin controls the Standby mode. We must set it to HIGH to activate the motor. We can connect it to a digital pin on Arduino if we want to be able to activate Standby mode, or connect it to VCC if we want to leave it permanently disconnected.

arduino-tb6612fng-esquema

The connection, from the perspective of Arduino, would be as follows.

arduino-tb6612fng-conexion

Code examples

The necessary code is similar to what we saw when we looked at the L298N. Similarly, it is convenient to group the code into functions that we can reuse, or the code will grow rapidly.

The following code allows for the movement and turning of a vehicle, acting on both motors. We use the Standby mode to reduce consumption when it is stopped, extending the battery life.

const int pinPWMA = 6;
const int pinAIN2 = 7;
const int pinAIN1 = 8;
const int pinBIN1 = 9;
const int pinBIN2 = 10;
const int pinPWMB = 11;
const int pinSTBY = 12;

const int waitTime = 2000;  // wait between phases
const int speed = 200;    // turn speed

const int pinMotorA[3] = { pinPWMA, pinAIN2, pinAIN1 };
const int pinMotorB[3] = { pinPWMB, pinBIN1, pinBIN2 };

enum moveDirection {
  forward,
  backward
};

enum turnDirection {
  clockwise,
  counterClockwise
};

void setup()
{
  pinMode(pinAIN2, OUTPUT);
  pinMode(pinAIN1, OUTPUT);
  pinMode(pinPWMA, OUTPUT);
  pinMode(pinBIN1, OUTPUT);
  pinMode(pinBIN2, OUTPUT);
  pinMode(pinPWMB, OUTPUT);
}

void loop()
{
  enableMotors();
  move(forward, 180);
  delay(waitTime);

  move(backward, 180);
  delay(waitTime);

  turn(clockwise, 180);
  delay(waitTime);

  turn(counterClockwise, 180);
  delay(waitTime);

  fullStop();
  delay(waitTime);
}

// Functions that control the vehicle
void move(int direction, int speed)
{
  if (direction == forward)
  {
    moveMotorForward(pinMotorA, speed);
    moveMotorForward(pinMotorB, speed);
  }
  else
  {
    moveMotorBackward(pinMotorA, speed);
    moveMotorBackward(pinMotorB, speed);
  }
}

void turn(int direction, int speed)
{
  if (direction == forward)
  {
    moveMotorForward(pinMotorA, speed);
    moveMotorBackward(pinMotorB, speed);
  }
  else
  {
    moveMotorBackward(pinMotorA, speed);
    moveMotorForward(pinMotorB, speed);
  }
}

void fullStop()
{
  disableMotors();
  stopMotor(pinMotorA);
  stopMotor(pinMotorB);
}

// Functions that control the motors
void moveMotorForward(const int pinMotor[3], int speed)
{
  digitalWrite(pinMotor[1], HIGH);
  digitalWrite(pinMotor[2], LOW);

  analogWrite(pinMotor[0], speed);
}

void moveMotorBackward(const int pinMotor[3], int speed)
{
  digitalWrite(pinMotor[1], LOW);
  digitalWrite(pinMotor[2], HIGH);

  analogWrite(pinMotor[0], speed);
}

void stopMotor(const int pinMotor[3])
{
  digitalWrite(pinMotor[1], LOW);
  digitalWrite(pinMotor[2], LOW);

  analogWrite(pinMotor[0], 0);
}

void enableMotors()
{
  digitalWrite(pinSTBY, HIGH);
}

void disableMotors()
{
  digitalWrite(pinSTBY, LOW);
}

In a complex project, these functions would be integrated into the objects that make up our model.

Download the code

All the code in this post is available for download on Github. github-full