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.
Predict: Read the problem and decide what you think before you try anything. A wrong guess is fine: it shows you what to look for.
Try: Try it on the board and compare the result with your prediction.
Investigate: Work out why it happens, one step at a time.
Modify: Change the problem on purpose and see what happens.
Practise: A few on your own, to check you have it.
Make: Make your own problem using what you have learned. Expect a few tries; that is normal.
WidthMore contexts, same idea.
Make your prediction first. Help opens once you've locked it in.
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
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.
digitalWrite(8, HIGH)IN1 HIGH.
digitalWrite(7, LOW)IN2 LOW: forwards.
analogWrite(9, 128)ENA at 128 of 255: about half speed.
swap 8 and 7Backwards. ENA is unchanged.
Now 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
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.
I can make a motor go forwards, backwards and stop with a driver.
I can set a motor's speed with PWM on the enable pin.
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
Predict: ask what 'minus speed' would mean to a PWM signal.
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
The motor won't go backwards. Ask: Which input is HIGH? Fix: Swap IN1 and IN2.
Stretch and support
Stretch: Write a function drive(int speed) that goes forwards for positive speeds and backwards for negative ones.
backwards: Committing to how to reverse tests the new move: direction comes from which input is HIGH, not from a negative speed.
Try: drive it: Driving the board forwards and backwards settles the prediction and shows ENA as speed only.
Worked example: the signals in code: Reading the driver's three signals in a sketch models the move in code. Sam's PWM on a direction pin is planted after the method.
Vary: change one thing: Stopping two ways (both the same, or ENA at 0) shows the difference between braking and coasting.
Build it in Tinkercad: Tinkercad's L293D and DC motor let students wire the real chip and run a forwards, stop, backwards sketch.
drive codes: Reading drive signals for a two-motor robot.
a gentle start: Setting a precise direction and speed from a brief applies both signals.
Now you can
Make a motor go forwards, backwards and stop with a driver.