Built-in project · Arduino Uno R3

TB6612 robot drive on an Arduino Uno

Two gearbox motors on a TB6612FNG from a four-AA pack, with the SparkFun library: forward, half speed, a spin, reverse, a brake and standby. The whole build, an Arduino Uno and 4 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.

IntermediateRuns in your browser. Free, and no account needed.

TB6612 robot drive · Arduino Uno R3live0.000 s 0.00x
Click to open it in the editor
The circuit itself, running here on the simulator. Press what can be pressed; click anything else to open it in the editor.

The code

The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.

sketch.ino · Arduino Uno R3
// TB6612 robot drive: two motors on a TB6612FNG, the way the SparkFun
// Inventor's Kit wires it, off a pack of four AA cells.
//
//   pin 5  PWMA, the left motor's speed      pin 6  PWMB, the right motor's
//   pin 2  AIN1   pin 4  AIN2                pin 7  BIN1   pin 8  BIN2
//   pin 9  STBY, which must be high or the driver does nothing
//   VCC    the Uno's 5 V (the logic side)    VM  the battery pack's red lead
//   GND    the Uno's GND and the pack's black lead, on one ground
//
// SparkFun_TB6612.h does the pin work: drive(speed) takes -255 to 255, the
// sign is the direction, and brake() shorts the motor through the bridge.
//
// # Why the pack, and not the Uno's 5 V
//
// Motors are noisy and hungry. A separate motor rail is how every robot is
// built, and the TB6612's MOSFET bridge keeps only a fraction of a volt of
// it, where an L293D keeps more than two. Four fresh AA cells are 6.4 V with
// nothing on them and a little less once the motors start: the battery sags
// by its own internal resistance, and the readout on the pack shows it.
//
// # Brake or coast
//
// On this chip PWM low is a short brake, not a coast, so analogWrite at half
// speed stops the motor firmly between pulses. brake() is the same thing held
// on; standby() lets both motors coast to a stop.

#include <SparkFun_TB6612.h>

const int AIN1 = 2;
const int AIN2 = 4;
const int PWMA = 5;
const int BIN1 = 7;
const int BIN2 = 8;
const int PWMB = 6;
const int STBY = 9;

// 1 or -1: flip one if a motor was wired the other way round.
const int offsetA = 1;
const int offsetB = 1;

Motor leftMotor = Motor(AIN1, AIN2, PWMA, offsetA, STBY);
Motor rightMotor = Motor(BIN1, BIN2, PWMB, offsetB, STBY);

void say(const char *what) {
  Serial.print("drive: ");
  Serial.println(what);
}

void setup() {
  Serial.begin(115200);
  Serial.println("Mokxi Arduino Uno: two motors on a TB6612FNG");
}

void loop() {
  say("forward, full speed");
  forward(leftMotor, rightMotor, 255);
  delay(1500);

  say("forward, half speed (PWM low brakes between pulses)");
  forward(leftMotor, rightMotor, 128);
  delay(1500);

  say("spin left on the spot");
  left(leftMotor, rightMotor, 255);
  delay(1200);

  say("reverse");
  back(leftMotor, rightMotor, 200);
  delay(1500);

  say("brake");
  brake(leftMotor, rightMotor);
  delay(1000);

  say("standby: both motors coast");
  leftMotor.standby();
  delay(1500);
}

Parts list

5 parts, plus the jumper wires. Every one is in the editor's parts bin.

How it is wired

15 connections, pin by pin, read from the circuit itself. Each line is one set of pins joined together, by a jumper wire or a breadboard strip.

  • Ground: Arduino Uno R3 pin GND; Motor driver, TB6612FNG pin GND1
  • Arduino Uno R3 pin 9; Motor driver, TB6612FNG pin STBY
  • Arduino Uno R3 pin 8; Motor driver, TB6612FNG pin BIN2
  • Arduino Uno R3 pin 7; Motor driver, TB6612FNG pin BIN1
  • Arduino Uno R3 pin 6; Motor driver, TB6612FNG pin PWMB
  • Arduino Uno R3 pin 5; Motor driver, TB6612FNG pin PWMA
  • Arduino Uno R3 pin 4; Motor driver, TB6612FNG pin AIN2
  • Arduino Uno R3 pin 2; Motor driver, TB6612FNG pin AIN1
  • Arduino Uno R3 pin 5V; Motor driver, TB6612FNG pin VCC
  • Motor driver, TB6612FNG pin VM; Battery, 9 V, AA or 18650 pin V+
  • Motor driver, TB6612FNG pin AO1; DC motor (1) pin 1
  • Motor driver, TB6612FNG pin AO2; DC motor (1) pin 2
  • Motor driver, TB6612FNG pin BO2; DC motor (2) pin 2
  • Motor driver, TB6612FNG pin BO1; DC motor (2) pin 1
  • Motor driver, TB6612FNG pin GND3; Battery, 9 V, AA or 18650 pin V-

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.