Skip to content
Study Hall
Lesson 4 of 4Computer Interfacing
Unit 6 · Lesson 4 of 4

PWM, Motors and Control Systems

Making things move: dimming, speed control, servos, motor drivers, and the difference feedback makes.

5 min read10 practice questionsInteractiveAsk AI about this →

Key things to remember

  1. 1PWM switches a digital pin rapidly; duty cycle sets the average voltage.
  2. 2analogWrite(pin, value) takes 0–255. .
  3. 3PWM is available only on pins 3, 5, 6, 9, 10 and 11 on an Uno.
  4. 4A motor needs a transistor (for current) and a flyback diode (for the inductive spike).
  5. 5An H-bridge reverses a motor; closing both switches on one side causes destructive shoot-through.
  6. 6A servo reads pulse WIDTH: 1.0 ms = 0°, 1.5 ms = 90°, 2.0 ms = 180°, repeated every 20 ms.
  7. 7Separate supplies must share a common ground.
  8. 8Closed loop measures the result and corrects; open loop does not.

A digital output pin has exactly two states, so it cannot produce half a volt of brightness. What it can do is switch very fast — thousands of times per second — and spend a controllable fraction of each cycle high. Average that over time and you get an effective voltage anywhere between 0 and 5 V. That technique is pulse width modulation, and it is how a digital device dims a lamp or slows a motor.

A digital pin can only be 0 V or 5 V — so it switches fast and you get the average.25%1.25 Vavg50%2.5 Vavg75%3.75 VavganalogWrite takes 0–255. It dims an LED or slows a motor, but it is not a true analog voltage — a meter would still see pulses.
Fig 1Same frequency, different duty cycle, different average.
Duty cycle is the only thing analogWrite changes — the frequency stays fixed.
analogWrite is not analog
analogWrite(pin, value) takes 0 to 255, where 0 is always off, 255 is always on, and 128 is about 50% duty. The pin still only ever outputs 0 V or 5 V — a fast oscilloscope would show a square wave, not a steady level. It works because an LED's afterglow and your eye, or a motor's inertia, do the averaging for you.
Loading interactive…
Change the duty cycle and see the waveform, the average, and the effect.
Arduino CWhat it does
analogWrite(9, 0);0% duty — fully off
analogWrite(9, 64);About 25% duty, averaging 1.25 V
analogWrite(9, 128);About 50% duty, averaging 2.5 V
analogWrite(9, 255);100% duty — fully on
analogWrite(9, map(raw, 0, 1023, 0, 255));Drive PWM from an analog sensor reading

Driving a DC motor

A motor cannot be connected to a pin. It draws hundreds of milliamps against the pin's 40 mA limit, and it is an inductive load: when current stops, the collapsing magnetic field generates a reverse voltage spike that can be many times the supply voltage. Two components solve both problems — a transistor to carry the current, and a flyback diode to absorb the spike.

  1. The Arduino pin connects through a resistor (about 1 kΩ) to the base of an NPN transistor — this limits base current to a few milliamps.
  2. The motor connects between a separate supply's positive rail and the transistor's collector.
  3. The transistor's emitter goes to ground.
  4. A flyback diode goes across the motor, reverse-biased — cathode to the positive side — so it does nothing normally and conducts the spike away when the motor switches off.
  5. The Arduino's ground and the motor supply's ground must be connected together, or the transistor has no shared reference and nothing works.
Common ground is not optional
When two supplies are involved, their grounds must be joined. Without a shared reference, the transistor's base-emitter voltage is undefined and the circuit behaves erratically or not at all. This is one of the most common faults in a first motor project, and it produces symptoms that look like almost anything.

Reversing direction: the H-bridge

A single transistor can turn a motor on and off but not reverse it, because the current can only flow one way. An H-bridge is four switches arranged around the motor so that closing one diagonal pair drives it forwards and closing the other pair drives it backwards. Ready-made driver chips — L293D, L298N, DRV8833 — contain the bridge and the flyback diodes in one package, and are what you should actually use.

Four switches let one supply drive a motor either way.+VGNDQ1Q2Q3Q4MQ1 + Q4 on → forwardQ2 + Q3 on → reverseall off → coastsboth bottoms on → brakesQ1 + Q3 = short circuit(“shoot-through” — never do this)A ready-made driver IC (L293D, L298N, DRV8833) contains the bridge plus the flyback diodes that absorb the coil's kickback.
Fig 2Closing one diagonal pair drives forward; the other drives reverse.
Shoot-through
Turning on both switches on the same side of an H-bridge creates a direct short from the supply to ground through two transistors, destroying them almost instantly. Driver ICs include logic that prevents this, which is one more reason to use a chip rather than four discrete transistors.

Servos

A hobby servo is not controlled by duty cycle — it is controlled by pulse width. It expects a pulse every 20 ms whose width encodes the target angle: 1.0 ms for 0°, 1.5 ms for 90°, 2.0 ms for 180°. Inside, a small closed-loop controller compares the commanded position with a potentiometer reading the actual shaft position and drives its motor until they match. That is why a servo holds its angle against a load, and why it is a genuine closed-loop system in miniature.

Arduino CWhat it does
#include <Servo.h>Include the servo library
Servo arm;Create a servo object
arm.attach(9);In setup: assign it to pin 9
arm.write(90);Move to 90° — the library generates the pulse train
arm.write(map(raw, 0, 1023, 0, 180));Drive the servo from a potentiometer
Power a servo separately
A servo draws a current spike each time it starts moving — often several hundred milliamps. Powered from the Arduino's 5 V pin, that spike drops the board's supply voltage enough to reset it, which looks like a program crash. Give the servo its own supply and join the grounds.

Open and closed loop control

Feedback is the entire difference between the two.OPEN LOOP — hope for the bestSet pointControllerActuatorOutput"run the motor for 3 seconds" — if the load changes, the result changes.CLOSED LOOP — measure and correctSet pointCompareActuatorOutputSENSOR feedbackA thermostat, cruise control and a line-following robot are all closed loop. A toaster timer is open loop.
Fig 3Feedback is the entire difference.
AOpen loop
  • Sends a command and assumes it worked
  • No sensor measuring the result
  • Simple, cheap, predictable
  • A toaster timer, a plain DC motor run for 3 seconds
BClosed loop
  • Measures the result and corrects the command
  • Requires a sensor on the output
  • More complex, but handles disturbance and wear
  • A thermostat, cruise control, a servo, a line-following robot

The practical test is simple: does anything measure whether the command actually worked? "Run the motor at 70% for two seconds" is open loop, and the distance travelled changes with battery voltage, load and floor surface. "Run the motor until the encoder counts 500 pulses" is closed loop, and it travels the same distance every time regardless. Feedback costs a sensor and some code, and buys you consistency.

Worked examples

Reveal one step at a time and try to beat it to the next line.

Example 1
What duty cycle and average voltage does analogWrite(9, 180) produce on a 5 V pin?
Example 2
Design a circuit and sketch so a potentiometer on A0 controls the speed of a 6 V motor drawing 400 mA.
Example 3
A robot is programmed to run its motors for 2 seconds to travel one metre. It goes 1.2 m on fresh batteries and 0.7 m on tired ones. Classify the system, explain the cause, and describe a fix.

Where marks get lost

  • Calling analogWrite an analog output. It is a square wave whose duty cycle you control.
  • Using analogWrite on a pin without PWM — pins 3, 5, 6, 9, 10 and 11 only on an Uno.
  • Driving a motor straight from a pin, destroying it.
  • Omitting the flyback diode, so the inductive spike damages the transistor.
  • Forgetting to connect the grounds of two separate supplies.
  • Confusing a servo's pulse width with duty cycle. A servo reads the width of the pulse, not the fraction of the period.
  • Powering a servo from the Arduino's 5 V pin, causing brownout resets that look like software crashes.
  • Calling a timed motor run "closed loop" — without a sensor measuring the result, it is open loop.

Flashcards 10

What does PWM stand for and what does it vary?
What range does analogWrite() accept?
Average voltage from analogWrite on a 5 V pin
Which Uno pins support analogWrite?
+ 6 more

Practice

1
What duty cycle does analogWrite(pin, 51) produce? Answer as a percentage.
%
2
What average voltage does a 60% duty cycle give on a 5 V pin?
V
3
Why is a flyback diode fitted across a motor?
4
A servo is commanded to 90°. What pulse is being sent?
5
Which system is closed loop?
6
What analogWrite() value gives a 40% duty cycle? Round to the nearest whole number.
7
Two switches on the same side of an H-bridge are turned on at once. What happens?
8
Order the elements of a closed-loop control system.
  1. 1The controller compares the set point with the measurement
  2. 2A sensor measures the result and feeds it back
  3. 3The actuator changes the output
  4. 4A set point is specified
9
A servo powered from the Arduino's 5 V pin makes the board reset whenever it moves. Why?
10
Match each component to its role in a motor circuit.
Transistor
Flyback diode
H-bridge
Common ground

Spot a mistake on this page? Let us know