Skip to content

Class 1 · Drive

Today the car gets built and learns to move. You'll assemble the chassis, wire a TB6612 motor driver to the Uno, and write the first version of the command language the Pi will use later: short text commands that you'll type into the Serial Monitor for now.

By the end of class

The car drives a square, about 50 cm on a side, from commands you type into the Serial Monitor.

New parts today

Part What it's for
2WD chassis, two TT motors, caster The car
TB6612FNG motor driver Speed and direction for both motors
4×AA holder with switch Motor power
Mini breadboard, standoffs Mounting the Uno and driver on the chassis

Build it

  1. Assemble the chassis following the instructions that came with it. Solder wires to the motors if they don't have them, and leave the slotted encoder disks on the motor shafts (you may use them in Class 6).
  2. Mount the Uno, the mini breadboard and the AA holder. Leave room at the front for the HC-SR04, at the sides for two IR sensors (Class 2), and on top for the Pi (Class 3).
  3. Press the TB6612 module into the mini breadboard. Read the pin labels printed on your board: modules from different sellers put the pins in different orders. You should find VM, VCC, GND (two or three), AO1, AO2, BO1, BO2, PWMA, AIN1, AIN2, STBY, BIN1, BIN2, PWMB.
  4. Wire it with the AA pack switched off:
From To Why
Uno pin 9 PWMA Left motor speed (PWM)
Uno pin 7 AIN1 Left motor direction
Uno pin 8 AIN2 Left motor direction
Uno pin 10 PWMB Right motor speed (PWM)
Uno pin 11 BIN1 Right motor direction
Uno pin 12 BIN2 Right motor direction
Uno pin 4 STBY HIGH = driver on
Uno 5V VCC Driver's logic power
Uno GND GND Common ground
AA pack + (through switch) VM Motor power
AA pack − GND Common ground
Left motor AO1, AO2
Right motor BO1, BO2

These pins stay the same for all six classes. Pins 5 and 6 are saved for the HC-SR04 (trig 5, echo 6, as in the dispenser), and pins 2 and 3 are saved for wheel sensors in Class 6.

Two rules for these pins

  • Don't add the Servo library to this project. It takes over the timer behind pins 9 and 10, and your motors stop responding to speed.
  • Check the AA pack with the multimeter before you connect it to VM: red probe on the + wire should read about +6 V.

Tool: analogWrite() — somewhere between on and off

digitalWrite() gives a pin two choices: 0 V or 5 V. analogWrite(pin, level) switches the pin on and off about 500 times a second, and level (0 to 255) sets what fraction of the time it's on. An LED looks dimmer; a motor turns slower. This is called PWM (pulse-width modulation).

It only works on the pins marked with ~ on the Uno: 3, 5, 6, 9, 10, 11.

Example: an LED with a 220 Ω resistor on pin 9 that fades up, then off.

#include <Arduino.h>

const int LED = 9;

void setup() {
  pinMode(LED, OUTPUT);
}

void loop() {
  analogWrite(LED, 0);     // off
  delay(500);
  analogWrite(LED, 64);    // 25 % of the time on
  delay(500);
  analogWrite(LED, 255);   // fully on
  delay(500);
}

Tool: the TB6612 truth table

Each motor has three control pins. The two IN pins choose the direction, and the PWM pin sets the speed. STBY must be HIGH or nothing moves.

IN1 IN2 PWM Motor
HIGH LOW 0–255 Turns one way, at that speed
LOW HIGH 0–255 Turns the other way
LOW LOW any Coasts to a stop
HIGH HIGH any Brakes

Inside the chip is an H-bridge, the same idea as the four-transistor circuit from Unit 4, built from MOSFETs that waste far less voltage than the old L298N.

Example: one motor on channel A, as if it were a desk fan, ramping up in one direction, then reversing.

// fan: forward slow, forward fast, reverse, stop
digitalWrite(STBY, HIGH);

digitalWrite(AIN1, HIGH);
digitalWrite(AIN2, LOW);
analogWrite(PWMA, 100);  delay(2000);
analogWrite(PWMA, 255);  delay(2000);

digitalWrite(AIN1, LOW);
digitalWrite(AIN2, HIGH);
analogWrite(PWMA, 150);  delay(2000);

analogWrite(PWMA, 0);

TT motors don't start below a certain PWM level. Below about 60–80 they just hum. Find your car's number.

Mission 1 · Both wheels, both ways

Start a new PlatformIO project for the Uno with this platformio.ini:

[env:uno]
platform = atmelavr
board = uno
framework = arduino
monitor_speed = 115200
monitor_echo = yes
monitor_filters = send_on_enter

The last two lines make the Serial Monitor show what you type and send the whole line when you press Enter.

Write a function setMotors(int left, int right) where each number goes from −255 (full reverse) to 255 (full forward), and 0 is stop. Use constrain() so a bad number can't cause trouble. Test it with the car on a stand.

If a wheel turns backwards when it should go forward, swap that motor's two wires at AO1/AO2 or BO1/BO2.

Done when

With the wheels off the ground, setMotors(150, 150) turns both wheels forward, setMotors(-150, -150) both backward, and setMotors(150, -150) spins the car right in place.

Tool: reading a command from Serial

The Serial Monitor can send text to the Uno, too. Serial.available() tells you how many characters are waiting. Serial.read() takes one character. Serial.parseInt() reads the next whole number, skipping spaces.

Example: a command language for an LED on pin 9. B 200 sets the brightness to 200; X turns it off.

#include <Arduino.h>

const int LED = 9;

void setup() {
  pinMode(LED, OUTPUT);
  Serial.begin(115200);
  Serial.setTimeout(50);   // parseInt waits at most 50 ms for digits
}

void loop() {
  if (Serial.available() > 0) {
    char cmd = Serial.read();

    if (cmd == 'B') {
      int level = constrain(Serial.parseInt(), 0, 255);
      analogWrite(LED, level);
      Serial.print("ok B ");
      Serial.println(level);
    } else if (cmd == 'X') {
      analogWrite(LED, 0);
      Serial.println("ok X");
    }
    // anything else (spaces, Enter) is ignored
  }
}

Replying with ok is a good habit. In Class 3, a Python program on the Pi will send these commands, and the reply is how it knows the Uno heard.

Mission 2 · A command language for the car

Make the car obey commands from the Serial Monitor. Start with these, and add your own if you want:

Command Meaning
F 150 Forward at speed 150 until told otherwise
B 150 Backward at speed 150
S Stop
T 90 Turn in place 90° left (counterclockwise); T -90 turns right

Positive angles turn left, as in math class. You'll measure the car's heading the same way in Class 5, so keep it that way.

For T, the car doesn't know angles yet, only time. Spin at a fixed speed and time a full 360° with your phone's stopwatch, then compute milliseconds per degree. Store that as a const and adjust it until 90° looks like 90°.

Write this down somewhere you'll find it: the Pi will use the exact same commands.

Done when

Typing F 150, waiting, S, T 90, four times, drives a square about 50 cm on a side, and the car ends within about 15 cm of where it started.

If you finish early

  • Add a D <left> <right> command that sets each wheel directly (D 150 100 curves right). The Pi will use it later to steer.
  • Make F take an optional time: F 150 1000 drives forward for 1 s, then stops.
  • The car probably drifts to one side going "straight". Add a trim constant that slows the faster wheel a little.

Sign-off with Dr. Alvear

  • Wiring matches the pin table; AA pack + goes only to VM
  • F, B, S and T work from the Serial Monitor, with an ok reply
  • The car drives a square on command
  • Command list written down
  • Build log updated (photo + two sentences)