ENع
←Servos and motors
Lesson 17 · Interrupts, timers and PWM

Driving a servo and a one-way DC motor with a MOSFET

14 min11 steps5 questionsIntermediate
About this lesson

Two motors, two kinds of pulse. A hobby servo reads the width of a pulse sent every 20 ms as an angle and holds it with its own feedback; the Arduino Servo library makes those pulses with a hardware timer. A plain DC motor reads nothing: an IRLZ44N MOSFET switches its 6 V supply with PWM, the motor spins at the average, and a flyback diode protects the MOSFET at every off-edge. Target board: Arduino Uno R3. “PWM: dimming an LED” explains duty cycle and analogWrite, and “Pin current limits and driving loads with a transistor” explains why a pin needs a MOSFET for big loads; here we use both.

You'll learn to

Tick what you feel confident about. These self-checks do not affect your lesson score.

Fig 17 · Interactive
01 / 11

Inside a hobby servo

A hobby servo hides a DC motor, gears, a position sensor and a small control circuit in one box. It has three wires: power, ground and signal. You never drive its motor yourself: you send an angle, and it turns there and holds it.

hobby servo   motor + gears + position sensor + control circuit
wires         power (red), ground (black or brown),
              signal (yellow, orange or white)
feedback      it turns until the sensor reads the angle you asked for
Loading figure…
or ← →
Read the lesson step by step
  1. Inside a hobby servo

    A hobby servo hides a DC motor, gears, a position sensor and a small control circuit in one box. It has three wires: power, ground and signal. You never drive its motor yourself: you send an angle, and it turns there and holds it.

    hobby servo   motor + gears + position sensor + control circuit
    wires         power (red), ground (black or brown),
                  signal (yellow, orange or white)
    feedback      it turns until the sensor reads the angle you asked for
  2. The angle is a pulse width

    The signal wire carries a short pulse about every 20 ms. Its width is the message. Standard servos read 1000 µs as one end, 1500 µs as the middle and 2000 µs as the other end. The servo turns until its sensor agrees.

    frame   one pulse about every 20 ms  (50 per second)
    width   the message:
              1000 µs  one end
              1500 µs  the middle
              2000 µs  the other end
    the duty cycle is not the message; the width is
  3. servo.attach(9) starts the pulses

    servo.attach(9) hands pin 9 to the Servo library. On the Uno it takes over Timer1, a hardware timer, which makes the pulses in the background while loop() runs. It starts at 1500 µs, the middle. Side effect: analogWrite stops working on pins 9 and 10.

    line 1   servo.attach(9);
    library  Timer1 counts and interrupts: no delay(), no busy loop
    start    1500 µs (DEFAULT_PULSE_WIDTH)
    cost     analogWrite() no longer works on pins 9 and 10
  4. servo.write(90): 1472 µs

    servo.write(90) maps 0 to 180 degrees onto the library's 544 to 2400 µs: 90 becomes 1472 µs. Every 20 ms pin 9 now goes high for 1472 µs. The servo sees a target near the middle and holds it there.

    line 2   servo.write(90);
    map      90 * (2400 - 544) / 180 + 544 = 1472 µs
    pin 9    high 1472 µs, then low, every 20 ms
    servo    target: the middle
  5. servo.write(180): 2400 µs

    servo.write(180) widens the pulse to 2400 µs, so the horn swings to the other end. The pin only sends the message. The servo's motor takes its current from a separate 5 V supply that shares ground with the board.

    line 3   servo.write(180);
    map      180 -> 2400 µs  (MAX_PULSE_WIDTH)
    servo    new target: the far end, the horn swings
    power    from its own 5 V supply, grounds joined
    pin 9    only the signal, almost no current
  6. A motor needs a switch

    Now a plain DC motor on its own 6 V supply. A pin gives at most 40 mA: not enough for most motors. So an IRLZ44N MOSFET switches the motor on the low side, as in “Pin current limits and driving loads with a transistor”.

    pin limit   40 mA, not enough for most motors
    motor       on its own 6 V supply
    MOSFET      IRLZ44N, logic-level: full on at 5 V on the gate
                0.025 Ω on, rated 55 V
    low side    supply -> motor -> MOSFET -> ground
  7. analogWrite(3, 191)

    analogWrite(3, 191) makes pin 3 a PWM signal: high for 191 of every 255 parts of each cycle, about 490 cycles a second. Through its 220 Ω resistor the pin drives the MOSFET's gate, so the MOSFET switches the motor's 6 V just as fast.

    line 4   analogWrite(3, 191);
    PWM      191 / 255 = 75 % duty, about 490 Hz
    cycle    2.04 ms: high 1.53 ms, low 0.51 ms
    gate     5 V / 0 V through 220 Ω -> MOSFET on / off
  8. The motor feels the average

    The motor's spinning mass cannot follow 490 pulses a second, so it responds to the average: 191 ÷ 255 × 6 V ≈ 4.5 V. A bigger number means a faster motor. The current loops supply, motor, MOSFET, ground, never through the pin.

    average  191 / 255 × 6 V = 4.49 V  ≈ 4.5 V
    speed    rises with the duty cycle
    loop     + supply -> motor -> drain -> source -> ground
    pin      only charges the gate
  9. Switching off a coil

    A motor is a coil, which resists sudden changes in its current. At each off-edge of the PWM the MOSFET cuts that current. With nowhere to go, the drain voltage can jump far above 6 V and destroy the MOSFET, rated 55 V.

    coil     v = L · di/dt: a fast cut makes a big voltage
    when     every off-edge, about 490 times a second
    drain    can leap far above 6 V
    MOSFET   rated 55 V: a big enough spike destroys it
  10. A flyback diode

    A flyback diode across the motor gives that current a way round: out of the motor, through the diode and back in, fading as heat. While the MOSFET is on, the diode points the wrong way and carries nothing. Its stripe faces +6 V.

    diode    1N4001 across the motor, stripe to +6 V
    MOSFET on    diode reverse-biased: no current
    MOSFET off   coil current: motor -> diode -> motor
    drain    held about one diode drop above the supply
  11. Speed, but one way only

    analogWrite(3, 0) holds pin 3 LOW and the motor coasts to a stop. This circuit sets speed, never direction: current only ever flows one way through the motor. Reversing it needs an H-bridge, in “H-bridges, motor direction and stepper motors”.

    line 5   analogWrite(3, 0);
    pin 3    0 -> held LOW, MOSFET off, motor coasts
    one way  current always + -> motor -> MOSFET
    reverse  needs an H-bridge (four switches)
    servo    still holds 2400 µs: Timer1 keeps pulsing

Quiz Unlocks after the last step.