← Teacher guide

Motor drivers

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.

Power and motors · Driving a motor · about 40 minutes · Open the student lesson

Classroom resources

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.

Students will be able to

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

  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

What you will seeA question to askThe fix
The motor won't go backwards.Which input is HIGH?Swap IN1 and IN2.

Answers to the checks

  1. 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.
  2. Forwards: Make the motor go forwards at about half speed.

    IN1 HIGH, IN2 LOW, ENA about 128.
  3. Backwards: Now backwards, at the same speed.

    Swap IN1 and IN2; leave ENA alone.
  4. 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.
  5. 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.
  6. Brake: With ENA above 0, set both inputs HIGH.

    Braking: both ends of the motor are joined, so it stops quickly.
  7. Coast: Now set one input HIGH and ENA to 0.

    Coasting: the driver switches off and the motor spins down by itself.
  8. 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.
  9. Speed: ENA is at 64. What percentage of full speed is that (to the nearest whole number)?
    25
    64 ÷ 255 ≈ 25%.
  10. 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.

digitalWrite(8, HIGH); digitalWrite(7, LOW); analogWrite(9, 255);

What each step is for

Where it leads

Next: Two supplies, one ground.

Sources