Back

Activity 2.12: Motors and the Driver

Motion & Autonomy — Days 1 and 2 of 4

divider

Activity 2.12

Motors and the Driver

Key Concepts

Motor Drivers

PWM as Power

Differential Steering

A Pin Cannot Turn a Motor

led.value(1) # a pin can just about light an LED
motor.value(1) # a pin cannot begin to turn a motor

An LED draws about 10 milliamps. These motors want a couple of hundred. A GPIO pin can supply roughly twelve.

Connecting a motor straight to a pin does not make a slow motor. It makes a broken board.

So the Pin Gives Orders Instead

A motor driver sits between your board and the motors. Your pins tell it what to do; the battery provides the muscle.

This is a genuinely new arrangement. Everything else this unit has driven, your pins powered directly.

Two Pins Decide Direction

Terminal window
IN1 IN2 what the motor does
1 0 turn one way
0 1 turn the other way
0 0 stop

Not one pin but two, because a motor has three states, and one pin can only express two. Which of them is HIGH decides which way the shaft turns.

PWM, Wearing Its Other Hat

speaker.freq(880) # 2.10: how OFTEN = pitch
motor_pwm.duty_u16(40000) # here: how MUCH = power

Same tool as the speaker in 2.10. There you cared how often it switched, because that set the pitch. Here you care what fraction of the time it is on, because that sets the power.

One Function, Signed Speed

motor(in1, in2, pwm, 40000) # forward
motor(in1, in2, pwm, -40000) # backward
motor(in1, in2, pwm, 0) # stop

A negative number means backwards. One parameter carries both direction and power, and the function sorts out which pins that implies.

Steering Without a Steering Wheel

Two wheels, no rudder. You turn by driving them differently — one faster than the other to curve, or in opposite directions to spin on the spot.

Which is why the last function you write today takes two numbers: drive(left, right).

Objectives — Day 1

  • Explaining why a motor cannot be driven straight from a GPIO pin
  • Wiring the TB6612FNG motor driver
  • Spinning one motor forward and backward

Objectives — Day 2

  • Driving two motors together
  • Turning by driving the wheels at different speeds
  • Controlling speed with PWM duty
  • Writing drive(left, right) as a reusable helper

Key Terms

Motor Driver
A chip that takes low-power instructions from your pins and switches high-power current from a battery.
Duty Cycle
The fraction of time a PWM signal is on. More on-time means more power.
Differential Steering
Turning by running two wheels at different speeds or directions instead of steering them.
Standby
The driver's off switch. STBY must be HIGH before it will do anything at all.

'F' → Fullscreen

divider

Build

The bench circuit comes down today. The driver needs pins the LED array has been using, and the robot is a different object. Keep the OLED — it comes with you.

Tasks 1 to 3 are the first session; tasks 4 to 6 are the second. Create a new MicroPython program named 2-12-motors.


Task 1: One Motor, One Direction

  • Wire the driver. There are eleven connections and every one matters — work down the diagram and check each off.
  • Connect one gearmotor to the AO1/AO2 outputs.
  • Set the direction pins, set the speed, wait two seconds, stop.

Two connections cause almost every failure here. STBY must be HIGH or the driver ignores you completely and silently. And the battery's minus must share a ground with the board, or the driver has no common reference and behaves erratically.

Hookup diagram for the TB6612FNG motor driver showing board pins PWMA, AIN2, AIN1, STBY, BIN1, BIN2 and PWMB on the left and VM, VCC, GND and the four motor outputs on the right
[PHOTO PLACEHOLDER — Task 1: Driver Wired With One Motor Attached]
Motors and the Driver
from machine import Pin, PWM
import time
a_in1 = Pin(31, Pin.OUT)
a_in2 = Pin(32, Pin.OUT)
a_pwm = PWM(Pin(33))
a_pwm.freq(1000)
SPEED = 40000
a_in1.value(1)
a_in2.value(0)
a_pwm.duty_u16(SPEED)
time.sleep(2)
a_pwm.duty_u16(0)

Task 2: Now the Other Way

  • After running forward, swap which direction pin is HIGH and run again.
  • Watch the shaft reverse without touching the speed.
Motors and the Driver
from machine import Pin, PWM
import time
a_in1 = Pin(31, Pin.OUT)
a_in2 = Pin(32, Pin.OUT)
a_pwm = PWM(Pin(33))
a_pwm.freq(1000)
SPEED = 40000
a_in1.value(1)
a_in2.value(0)
a_pwm.duty_u16(SPEED)
time.sleep(2)
a_in1.value(0)
a_in2.value(1)
time.sleep(2)
a_pwm.duty_u16(0)

Task 3: Wrap It, With a Sign

  • Write motor(in1, in2, pwm, speed) where a positive speed goes forward, a negative speed goes backward, and zero stops.
  • Use it to run forward, then backward, then stop.

Note what -speed is doing: the duty cycle can never be negative, so the function has to flip the sign back after it has used it to pick the direction.

Motors and the Driver
from machine import Pin, PWM
import time
a_in1 = Pin(31, Pin.OUT)
a_in2 = Pin(32, Pin.OUT)
a_pwm = PWM(Pin(33))
a_pwm.freq(1000)
SPEED = 40000
def motor(in1, in2, pwm, speed):
if speed > 0:
in1.value(1)
in2.value(0)
pwm.duty_u16(speed)
elif speed < 0:
in1.value(0)
in2.value(1)
pwm.duty_u16(-speed)
else:
in1.value(0)
in2.value(0)
pwm.duty_u16(0)
motor(a_in1, a_in2, a_pwm, SPEED)
time.sleep(2)
motor(a_in1, a_in2, a_pwm, -SPEED)
time.sleep(2)
motor(a_in1, a_in2, a_pwm, 0)

Task 4: The Second Motor

  • Wire the left motor to BO1/BO2 and its three control pins.
  • Run both motors forward at once.
  • Hold the robot off the ground for this one.

Both wheels turning forward will very likely not drive straight. That is not a bug, and it is the first thing the Final Project asks you to deal with.

[PHOTO PLACEHOLDER — Task 4: Both Motors and Wheels Attached]
Motors and the Driver
from machine import Pin, PWM
import time
a_in1 = Pin(31, Pin.OUT)
a_in2 = Pin(32, Pin.OUT)
a_pwm = PWM(Pin(33))
a_pwm.freq(1000)
b_in1 = Pin(21, Pin.OUT)
b_in2 = Pin(35, Pin.OUT)
b_pwm = PWM(Pin(34))
b_pwm.freq(1000)
SPEED = 40000
def motor(in1, in2, pwm, speed):
if speed > 0:
in1.value(1)
in2.value(0)
pwm.duty_u16(speed)
elif speed < 0:
in1.value(0)
in2.value(1)
pwm.duty_u16(-speed)
else:
in1.value(0)
in2.value(0)
pwm.duty_u16(0)
motor(a_in1, a_in2, a_pwm, SPEED)
motor(b_in1, b_in2, b_pwm, SPEED)
time.sleep(2)
motor(a_in1, a_in2, a_pwm, 0)
motor(b_in1, b_in2, b_pwm, 0)

Task 5: One Call for Both Wheels

  • Write drive(left, right) that takes a speed for each wheel.
  • Write stop() as well — you will use it constantly.
Motors and the Driver
from machine import Pin, PWM
import time
a_in1 = Pin(31, Pin.OUT)
a_in2 = Pin(32, Pin.OUT)
a_pwm = PWM(Pin(33))
a_pwm.freq(1000)
b_in1 = Pin(21, Pin.OUT)
b_in2 = Pin(35, Pin.OUT)
b_pwm = PWM(Pin(34))
b_pwm.freq(1000)
SPEED = 40000
def motor(in1, in2, pwm, speed):
if speed > 0:
in1.value(1)
in2.value(0)
pwm.duty_u16(speed)
elif speed < 0:
in1.value(0)
in2.value(1)
pwm.duty_u16(-speed)
else:
in1.value(0)
in2.value(0)
pwm.duty_u16(0)
def drive(left, right):
motor(b_in1, b_in2, b_pwm, left)
motor(a_in1, a_in2, a_pwm, right)
def stop():
drive(0, 0)
drive(SPEED, SPEED)
time.sleep(1)
stop()

Task 6: Four Maneuvers

  • Drive forward, then backward, then spin one way, then spin the other, then stop.
  • Work out the two numbers for each maneuver before you write it.
Motors and the Driver
from machine import Pin, PWM
import time
a_in1 = Pin(31, Pin.OUT)
a_in2 = Pin(32, Pin.OUT)
a_pwm = PWM(Pin(33))
a_pwm.freq(1000)
b_in1 = Pin(21, Pin.OUT)
b_in2 = Pin(35, Pin.OUT)
b_pwm = PWM(Pin(34))
b_pwm.freq(1000)
SPEED = 40000
def motor(in1, in2, pwm, speed):
if speed > 0:
in1.value(1)
in2.value(0)
pwm.duty_u16(speed)
elif speed < 0:
in1.value(0)
in2.value(1)
pwm.duty_u16(-speed)
else:
in1.value(0)
in2.value(0)
pwm.duty_u16(0)
def drive(left, right):
motor(b_in1, b_in2, b_pwm, left)
motor(a_in1, a_in2, a_pwm, right)
def stop():
drive(0, 0)
drive(SPEED, SPEED)
time.sleep(1)
drive(-SPEED, -SPEED)
time.sleep(1)
drive(-SPEED, SPEED)
time.sleep(0.6)
drive(SPEED, -SPEED)
time.sleep(0.6)
stop()

Challenge (Optional): Slow Is Harder Than Fast

  • Find the lowest SPEED at which your wheels still turn reliably from a standing start.
  • Try it on the desk and on carpet. The number will differ, and knowing why is worth more than the number.
divider

Checkpoint

  • One motor turns, stops, and reverses on command
  • motor() with a negative speed runs backward, and with zero stops dead
  • Both wheels run together, held off the ground
  • drive(-SPEED, SPEED) spins the robot rather than driving it
  • Every program you run ends with the motors stopped
divider

Reflection

Answer the following questions before submitting your work.

  1. Every other component this unit has used was powered straight from a pin. Explain why a motor cannot be, and what the driver actually does about it.
  2. Direction needs two pins rather than one. Explain why one pin is not enough, using the three things a motor can be doing.
  3. You used PWM for the speaker in 2.10 and for the motors here. Describe what changed about which part of the signal you cared about, and why the same tool does both jobs.
divider

Submit

Submit the required files to the appropriate dropbox.

Activity Complete