TB6612FNG Driver for ESP32

Friday, Aug 8, 2025 | 3 minute read | Updated at Friday, Aug 8, 2025

@

The TB6612FNG is a dual-channel H-bridge motor driver that can handle up to 1.2A of continuous current per channel, making it ideal for small to medium-sized DC motors.

Technical Specifications

The TB6612FNG offers several advantages over simpler motor drivers like the L298N:

  • Dual H-Bridge Configuration: Controls two DC motors independently
  • Current Capacity: 1.2A continuous, 3.2A peak per channel
  • Voltage Range: 4.5V to 13.5V motor supply voltage
  • Low Voltage Drop: Approximately 0.5V across the driver
  • Four Operation Modes: Forward (CW), Reverse (CCW), Short Brake, and Stop

Pin Configuration

#define AIN1 2   // Motor A direction pin 1
#define AIN2 4   // Motor A direction pin 2
#define PWMA T0  // Motor A PWM control (GPIO pin varies)
#define BIN1 7   // Motor B direction pin 1
#define BIN2 8   // Motor B direction pin 2
#define PWMB T2  // Motor B PWM control (GPIO pin varies)
#define STBY 9   // Standby control pin

ESP32 Integration: Hardware Setup

Wiring Configuration

Here’s the recommended pin mapping for optimal performance:

#define AIN1 2   // Motor A direction pin 1
#define AIN2 4   // Motor A direction pin 2
#define PWMA T0  // Motor A PWM control (GPIO pin varies)
#define BIN1 7   // Motor B direction pin 1
#define BIN2 8   // Motor B direction pin 2
#define PWMB T2  // Motor B PWM control (GPIO pin varies)
#define STBY 9   // Standby control pin

Power Considerations

  • Logic Supply: 3.3V from ESP32
  • Motor Supply: Separate 6V-12V supply (depending on motor requirements)
  • Common Ground: Essential for proper operation
  • Decoupling Capacitors: 100µF across motor supply for noise reduction

Library Architecture

The TB6612_ESP32 library provides an object-oriented approach to motor control:

#include <TB6612_ESP32.h>

// Motor initialization with ESP32-specific parameters
Motor motor1 = Motor(AIN1, AIN2, PWMA, offsetA, STBY, 5000, 8, 1);
Motor motor2 = Motor(BIN1, BIN2, PWMB, offsetB, STBY, 5000, 8, 2);

Key Parameters Explained

  • Frequency (5000 Hz): PWM frequency for smooth motor operation
  • Resolution (8-bit): Provides 0-255 speed range
  • Channel: ESP32 LEDC channel assignment for hardware PWM

Basic Motor Control Functions

Individual Motor Control

// Forward motion at full speed
motor1.drive(255);

// Reverse motion at half speed  
motor1.drive(-128);

// Timed movement (2 seconds forward)
motor1.drive(255, 2000);

// Emergency stop
motor1.brake();

Coordinated Movement

// Both motors forward
forward(motor1, motor2, 150);

// Both motors reverse
back(motor1, motor2, 150);

// Turn left (differential steering)
left(motor1, motor2, 100);

// Turn right
right(motor1, motor2, 100);

Hardware PWM Implementation

The library leverages ESP32’s LEDC (LED Control) peripheral for hardware PWM generation:

void Motor::fwd(int speed) {
    digitalWrite(In1, HIGH);
    digitalWrite(In2, LOW);
    ledcWrite(Channel, speed);  // Hardware PWM output
}

Robot Locomotion

void autonomousNavigation() {
    // Move forward for 3 seconds
    forward(leftMotor, rightMotor, 200);
    delay(3000);
    
    // Turn right 90 degrees
    right(leftMotor, rightMotor, 150);
    delay(500);
    
    // Continue forward
    forward(leftMotor, rightMotor, 200);
}

Motor Calibration

Different motors may have varying characteristics. The library includes offset parameters to compensate:

const int offsetA = 1;   // Standard direction
const int offsetB = -1;  // Reversed direction for mechanical alignment

The best practice is to use motor encoders to have feedback on the speed of the motor.

Thats all folks

The complete library and examples are available on GitHub, offering a solid foundation for your next motor control application. Start with the basic examples and gradually incorporate advanced features as your project requirements evolve.

Additional Resources

Happy building, and may your motors always turn in the right direction!

© 2016 - 2025 A Brave New Blog

🌱 Powered by Hugo with theme Dream.

Me