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.
Load these into Waypoints Prepare: choose Load URL and paste the copied address, or download the file and import it. In Prepare, the worksheet's Teacher edition shows the answers; the student copy hides them.
SlidesDownload Present from the browser, or export to PowerPoint or PDF.
WorksheetDownload Print the student copy; the Teacher edition has the answers.
Students will be able to
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.
Timing
Predict and Try 6 min · Worked example 9 min · Vary 5 min · Tinkercad 12 min · Practise and Make 8 min
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
What you will see
A question to ask
The fix
The motor won't go backwards.
Which input is HIGH?
Swap IN1 and IN2.
Answers to the checks
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? Swap IN1 and IN2. ENA only sets speed (0 to 255); it can't be negative. Both LOW stops the motor.
Forwards: Make the motor go forwards at about half speed.
IN1 HIGH, IN2 LOW, ENA about 128.
Backwards: Now backwards, at the same speed.
Swap IN1 and IN2; leave ENA alone.
Direction, then speed: IN1 on pin 8, IN2 on pin 7, ENA on pin 9. Which lines make the motor go backwards at three-quarter speed? digitalWrite(8, LOW); digitalWrite(7, HIGH); analogWrite(9, 191); Direction on 8 and 7, speed 191 (75% of 255) on ENA.
Find the mistake: Sam's motor turns at full speed whatever Sam does. Click the line where Sam's sketch goes wrong. line 9 Line 9: speed belongs on ENA (pin 9, a PWM pin). Pin 8 is a direction input and has no PWM; analogWrite there just sets it HIGH. ENA was never set, so on a driver with ENA tied HIGH it runs flat out.
Brake: With ENA above 0, set both inputs HIGH.
Braking: both ends of the motor are joined, so it stops quickly.
Coast: Now set one input HIGH and ENA to 0.
Coasting: the driver switches off and the motor spins down by itself.
Two motors: 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… Spins on the spot Opposite directions turn it on the spot.
Speed: ENA is at 64. What percentage of full speed is that (to the nearest whole number)? 25 64 ÷ 255 ≈ 25%.
a gentle start: A conveyor belt must run backwards at 30% so parcels don't fall off. Set it.
IN1 LOW, IN2 HIGH, ENA about 77 (30% of 255).
Solutions
One possible solution for each task. Other correct programs are fine.
Vary: change one thing › Brake
Open task: any program that does what the step asks.
Vary: change one thing › Coast
Open task: any program that does what the step asks.
a gentle start
Open task: any program that does what the step asks.
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.
Exit ticket
Write the three lines that make a motor (IN1 on 8, IN2 on 7, ENA on 9) go forwards at full speed.
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.