Introduction: Simple 2 Wheel ESP32 Robot Using the TB6612FNG Dual Motor Controller

This is a Simple ESP32 Robot using the TB6612FNG Dual Motor Controller. This is to demonstrate the possibilities of creating more complex robots based on the ESP32 and the TB6612FNG motor controller. The Robot simply makes a series of moves, turn left, turn right, move forward, and reverse.

Supplies

  1. You'll need: a 4 cell AA battery pack
  2. Two motor Robot Chassis
  3. ESP32 microcontroller, I used the ESP32 DEVKITV1
  4. TB6612FNG motor controller
  5. Dupont Connectors or other Wiring

Step 1: Install the Motor Control Library in the Arduino IDE

First you will need to install the TB6612FNG_ESP32 library by Pablo López. This is an Arduino library for the ESP32 microcontroller that provides an easy-to-use interface for controlling a TB6612FNG motor controller. The library handles the low-level details of communicating with the motor controller and provides simple functions for controlling the speed and direction of the connected motors.

To use the TB6612FNG_ESP32 library, you'll need to install it in your Arduino IDE. Here's how:

  1. Download the TB6612FNG_ESP32 library from the GitHub repository
  2. https://github.com/pablopeza/TB6612FNG_ESP32
  3. Open the Arduino IDE and go to Sketch > Include Library > Add .ZIP Library.
  4. Select the downloaded library ZIP file and click "Open".
  5. The library should now be installed and ready to use.

Step 2: Build the Two Motor Robot

There are many robot kits you can choose from I would suggest one similar to this one. They are relatively inexpensive and can be found on Amazon or eBay for less than $15.00 . Also, you will probably need to solder the header pins onto the TB6612FNG Motor controller most of these that I have seen do not have the header pins already attached, so some soldering will probably be required.

Step 3: Wire the Connections

Wiring connections from TB6612FNG Motor Controller to the Motors:

Connect the leads from your motors to the Motor Controller, don't worry if you don't get them correct the first time

  • Motor 1 should go to A01 and A02
  • Motor 2 should go to B01 and B02

If the motors are NOT turning in the right direction reverse the pins (for example the A01 and A02) pins

If the left and right motors are reversed then swap the A01 and A02 wires to the B01 and B02 pins 

Wiring connections from ESP32 to TB6612FNG Motor Controller should be as follows:

  • ESP32 Pin D13 to TB6612FNG Pin AIN1
  • ESP32 Pin D12 to TB6612FNG Pin BIN1
  • ESP32 Pin D14 to TB6612FNG Pin AIN2
  • ESP32 Pin D27 to TB6612FNG Pin BIN2
  • ESP32 Pin D26 to TB6612FNG Pin PWMA
  • ESP32 Pin D25 to TB6612FNG Pin PWMB
  • ESP32 Pin D33 to TB6612FNG Pin STBY

Wiring connections from the Battery Pack to the ESP32 and TB6612FNG

  • A 4 Cell battery pack is used to supply 6 Volts of DC current to the robot and to the ESP32
  • Connect the battery to the motor controller. The TB6612FNG motor controller has a power input pin, labeled "VM". Connect the positive lead of the battery pack to the "VM" pin and the negative lead to the "GND" pin,
  • Connect the battery to the ESP32. connect the positive lead to the "VIN" pin of the ESP32 and the negative lead to the "GND" pin
  • A wire splitter was used to connect the Positive (+) side of the Battery pack to the VIN pin of the ESP32, and to the VM pin of the TB6612FNG Motor Controller.
  • Another wire splitter was used to connect the Negative (-) side of the Battery pack to the GND pin of the ESP32, and to the GND pin of the TB6612FNG Motor Controller

More pictures of the robot and wiring can be found at my GitHub page

https://github.com/mkconer/ESP32-Robot-TB6612FNG

Step 4: Download the Sketch / Code From Github

Download the Arduino sketch from the GitHub page here. https://github.com/mkconer/ESP32-Robot-TB6612FNG


Or you can copy and paste it from here.

// Simple 2 Wheel robot demonstrating the use of the ESP32 and the TB6612FNG Motor Controller


// This is a modified version of the sparkfun Dual Motor Controller TB6612FNG example sketch
// Which can be found here. https://www.sparkfun.com/products/14450?gclid=Cj0KCQiA6LyfBhC3ARIsAG4gkF8E3EeZWggXUgO7oEWK2d7iwPDjHLctu7CT5xEef9ki6SXmh0vvJ7waAuB7EALw_wcB
// The Library has been modified for the ESP32 by Pablo López
// It can be downloaded from Github https://github.com/pablopeza/TB6612FNG_ESP32
// Download the Zip file and install it in the Arduino IDE by clicking Sketch/ Include Library / Add Zip Library


// #include <Arduino.h>
#include <TB6612_ESP32.h>


// *** Wiring connections from ESP32 to TB6612FNG Motor Controller ***


#define AIN1 13 // ESP32 Pin D13 to TB6612FNG Pin AIN1
#define BIN1 12 // ESP32 Pin D12 to TB6612FNG Pin BIN1
#define AIN2 14 // ESP32 Pin D14 to TB6612FNG Pin AIN2
#define BIN2 27 // ESP32 Pin D27 to TB6612FNG Pin BIN2
#define PWMA 26 // ESP32 Pin D26 to TB6612FNG Pin PWMA
#define PWMB 25 // ESP32 Pin D25 to TB6612FNG Pin PWMB
#define STBY 33 // ESP32 Pin D33 to TB6612FNG Pin STBY


// *** Wiring connections from TB6612FNG Motor Controller to the Motors ***


// Connect the leads from your motors, don't worry if you don't get them correct the first time
// Motor 1 should go to A01 and A02
// Motor 2 should go to B01 and B02
// If the motors are NOT turning in the right direction reverse the pins (for example the A01 and A02) pins
// If the left and right motors are reversed then swap the A01 and A02 wires to the B01 and B02 pins 


// *** Wiring connections from the Battery Pack to the ESP32 and TB6612FNG Motor Controller ***


// A 4 Cell battery pack was used to supply 6 Volts of DC current to the robot and to the ESP32
// A wire splitter was used to connect the Positive (+) side of the Battery pack to the VIN pin of the ESP32,
// and to the VM pin of the TB6612FNG Motor Controller.
// Another wire splitter was used to connect the Negative (-) side of the Battery pack to the GND pin of the ESP32,
// and to the GND pin of the TB6612FNG Motor Controller


// these constants are used to allow you to make your motor configuration
// line up with function names like forward.  Value can be 1 or -1
const int offsetA = 1;
const int offsetB = 1;

Motor motor1 = Motor(AIN1, AIN2, PWMA, offsetA, STBY,5000 ,8,1 );
Motor motor2 = Motor(BIN1, BIN2, PWMB, offsetB, STBY,5000 ,8,2 );

// Initializing motors.  The library will allow you to initialize as many
// motors as you have memory for.  If you are using functions like forward
// that take 2 motors as arguements you can either write new functions or
// call the function more than once.

void setup()
{


}


void loop()
{
     delay(2000); // Short delay to catch your robot

     //Use of the drive function which takes as arguements the speed
     //and optional duration.  A negative speed will cause it to go
     //backwards.  Speed can be from -255 to 255.  Also use of the
     //brake function which takes no arguements.

     motor1.drive(255,2000);       // Turn Motor 1 for 2 seconds at full speed
     //motor1.drive(-255,2000);
     motor1.brake();
     delay(2000);

     //Use of the drive function which takes as arguements the speed
     //and optional duration.  A negative speed will cause it to go
     //backwards.  Speed can be from -255 to 255.  Also use of the
     //brake function which takes no arguements.

     motor2.drive(255,2000);        // Turn Motor 2 for 2 seconds at full speed
     //motor2.drive(-255,2000);
     motor2.brake();
     delay(2000);

     //Use of the forward function, which takes as arguements two motors
     //and optionally a speed.  If a negative number is used for speed
     //it will go backwards

     forward(motor1, motor2, 255);        // Forward Motor 1 and Motor 2 for 1 seconds at full speed
     delay(1000);

     // Brake
     brake(motor1, motor2);     // Stop Motor 1 and Motor 2 for 2 seconds 
     delay(2000);    

     //Use of the back function, which takes as arguments two motors
     //and optionally a speed.  Either a positive number or a negative
     //number for speed will cause it to go backwards

     back(motor1, motor2, -255);         // Reverse Motor 1 and Motor 2 for 1 seconds at full speed
     delay(1000);

     // Brake again
     brake(motor1, motor2);       // Stop Motor 1 and Motor 2 for 2 seconds 
     delay(2000);  

     //Use of the left and right functions which take as arguements two
     //motors and a speed.  This function turns both motors to move in
     //the appropriate direction.  For turning a single motor use drive.

     left(motor1, motor2, 255);
     delay(2000);
     brake(motor1, motor2);
     delay(1000); 
     right(motor1, motor2, 255);
     delay(2000);

     // Brake again
     brake(motor1, motor2);       // Stop Motor 1 and Motor 2 for 2 seconds 
     delay(2000); 
}

Step 5: Upload the Code to the ESP32

Upload the Sketch to the ESP32 and don't forget to install the batteries! Have fun playing around with the Code getting the robot to move in different directions and at different speeds.

Step 6: More Information About the Code

This is for a simple 2 wheel robot using an ESP32 microcontroller and a TB6612FNG motor controller. The TB6612FNG motor controller is connected to the motors and to the ESP32 microcontroller, which is powered by a 4 cell battery pack. The code demonstrates the use of the TB6612_ESP32 library, which has been modified for use with the ESP32 by Pablo López.

The code defines the pin connections between the ESP32 and the TB6612FNG motor controller, as well as the pin connections between the motor controller and the motors. It also defines two Motor objects, motor1 and motor2, which are initialized with the appropriate pin assignments and other parameters using the Motor class from the TB6612_ESP32 library.

The loop function contains several calls to the drive, brake, etc. functions of the Motor class to control the motors. The drive function takes a speed and an optional duration as arguments, and sets the motors to move at the specified speed for the specified duration. The brake function stops the motors. The forward and back functions take two Motor objects and an optional speed as arguments, and cause the two motors to move in the same direction or opposite directions, respectively.