← Power and motors

Motor drivers

A motor driver (an H-bridge) lets two pins choose the direction: IN1 HIGH and IN2 LOW is forwards, swapped is backwards, the same is stop. A third, PWM pin on ENA sets the speed with analogWrite. Two jobs, two kinds of signal.

A transistor switches a motor on and off. But a robot has to reverse, and go slowly round corners. How does a pin make a motor go backwards?

A motor driver such as the L293D (or TB6612, L298N) sits between the Uno and the motor. Inside it is an H-bridge: four switches that can send the battery's current through the motor either way.

IN1 IN2 motor
HIGH LOW forwards
LOW HIGH backwards
same same stops (both HIGH brakes)

ENA takes analogWrite (0–255): the speed.

WidthMore contexts, same idea.
PredictRead the problem and decide what you think before you try anything. A wrong guess is fine: it shows you what to look for.

Backwards

The motor goes forwards with IN1 HIGH, IN2 LOW and ENA at 200. How do you make it go backwards at the same speed?

TryTry it on the board and compare the result with your prediction.

Try: drive it

Try it yourselfYour edits are saved in this browser.

Make the motor go forwards at about half speed.

Use the board. This ticks when you get there.

Now backwards, at the same speed.

Use the board. This ticks when you get there.

InvestigateWork out why it happens, one step at a time.

Worked example: the signals in code

Worked exampleBefore each step, say what you think comes next.
  1. digitalWrite(8, HIGH)\text{digitalWrite(8, HIGH)}IN1 HIGH.

Now backwards at three-quarter speed.

IN1 on pin 8, IN2 on pin 7, ENA on pin 9. Which lines make the motor go backwards at three-quarter speed?

Sam's motor turns at full speed whatever Sam does. Click the line where Sam's sketch goes wrong.

ModifyChange the problem on purpose and see what happens.

Vary: change one thing

Try it yourselfYour edits are saved in this browser.

With ENA above 0, set both inputs HIGH.

Use the board. This ticks when you get there.

Now set one input HIGH and ENA to 0.

Use the board. This ticks when you get there.

Modify TypicalChange the problem on purpose and see what happens.

Build it in Tinkercad

PractiseA few on your own, to check you have it.

Drive codes

A robot has a left and a right motor. The left goes forwards and the right goes backwards, both at the same speed. The robot…

ENA is at 64. What percentage of full speed is that (to the nearest whole number)?

MakeMake your own problem using what you have learned. Expect a few tries; that is normal.

A gentle start

Try it yourselfYour edits are saved in this browser.
Need a hint?

Backwards: which input is HIGH?

Another hint

30% of 255 is about 77.

A conveyor belt must run backwards at 30% so parcels don't fall off. Set it.

Use the board. This ticks when you get there.

For teachers

Students drive a motor through an H-bridge driver, using two direction inputs and a PWM enable, read the three signals in a sketch, find a PWM signal on the wrong pin, and compare braking with coasting. About 40 minutes.

Open the teacher guide: answers, solutions, slides and a worksheet.

Students will be able to

Before class

Model: an L293D-style driver with its own motor battery; IN1/IN2 HIGH-LOW forwards, LOW-HIGH backwards, both the same stops (both HIGH brakes), ENA 0–255 sets speed. Many driver boards tie ENA HIGH with a jumper: remove it to use PWM.

The L293D drops about 1.5–2 V; the TB6612 much less. The Robotics challenges (Cutebot) hide the same driver behind MakeCode blocks.

Running the lesson

  1. Predict: ask what 'minus speed' would mean to a PWM signal.
  2. Find the mistake: ask which pins have a ~.

The hard part (on purpose)

Students try a negative speed for backwards. Keep the truth table on the board.

When students get stuck

Stretch and support

Stretch: Write a function drive(int speed) that goes forwards for positive speeds and backwards for negative ones.

Support: A printed truth table beside the board.

Where it leads

Next: Two supplies, one ground.

Sources

What each step is for

Now you can
Next lesson3. Two supplies, one groundThe motor gets its own battery, but the grounds must be joined: a signal is a voltage compared with ground, so the driver can't read the Uno's pins without a shared 0 V. Powering the motor from the Uno's own 5V pulls it down when the motor starts, and the Uno resets.