Control motors using TB6612FNG
You can control two motors using the Toshiba TB6612FNG motor driver.
Wiring
Connect the AIN1, AIN2, PWMA, BIN1, BIN2, PWMB, and STBY pins of the TB6612FNG to the microcontroller's GPIO pins.
Connect the VCC and GND pins of the TB6612FNG to the microcontroller's power supply pins.
Connect the A01, A02, B01, and B02 pins of the TB6612FNG to the two motors.
Connect the VM and GND pins of the TB6612FNG to a power source for the motors, such as dry-cell batteries.
For example, suppose you are using a standard ESP32 microcontroller and connecting it to the TB6612FNG as follows:
| TB6612FNG | ESP32 |
|---|---|
| AIN1 | 13 |
| AIN2 | 14 |
| PWMA | 12 |
| BIN1 | 33 |
| BIN2 | 32 |
| PWMB | 27 |
| STBY | 26 |
| VCC | 3V3 |
| GND | GND |
The wiring for this setup looks like the diagram below.
Initializing the TB6612FNG
Use the initialization block to initialize the TB6612FNG.
First, assign the specific pins (such as AIN1) to variables.
Then, specify these variables in the corresponding parameters (like AIN1) within the initialization block.
Running the Motor
Use the "run single motor" block to operate either motor A or motor B.
Specify the motor to run (A or B), the direction (forward/reverse), and the speed (0–100).
Use the "run both motors" block to operate both motors (A and B) simultaneously. Specify the rotation direction (forward/reverse) and speed (0–100) for each motor.
Stopping the Motors
You can stop the motors by selecting "Stop" or "Brake" as the rotation mode in the motor control block.
With "Stop," there is almost no resistance if you try to turn the stopped motor by hand.
With "Brake," on the other hand, you will feel resistance if you try to turn the stopped motor by hand.
Example
The program below demonstrates how to alternate between forward and reverse rotation using two motors connected to a TB6612FNG driver.
This example assumes the wiring is set up using an ESP32, as shown in the diagram in the "Wiring" section of this page.
You can download the sample program here.