← Arduino

Outputs that vary

analogWrite(9, value) sets a brightness from 0 to 255 on a PWM pin (marked ~ on the Uno: 3, 5, 6, 9, 10, 11). The pin doesn't get a lower voltage: it switches fully on and off very fast, and the value sets how much of the time it is on. analogRead gives 0 to 1023, so divide by 4 to drive it from a knob.

A digital pin is only ever 0 V or 5 V. So how does a dimmer switch, a fading LED or a motor speed control work from one?

analogWrite(pin, value) takes a value from 0 (off) to 255 (fully on). It only works on the PWM pins, marked ~ on the Uno: 3, 5, 6, 9, 10 and 11. PWM stands for pulse-width modulation.

WidthMore contexts, same idea.
PredictRead the problem and decide what you think before you try anything. A wrong guess is fine: it shows you what to look for.

128

analogWrite(9, 128); — what does pin 9 do?

TryTry it on the board and compare the result with your prediction.

Try: see the signal

Try it yourselfYour edits are saved in this browser.

Upload and run. Look at the signal drawn beside the LED.

Use the board. This ticks when you get there.

InvestigateWork out why it happens, one step at a time.

Worked example: knob to brightness

Worked exampleBefore each step, say what you think comes next.
  1. analogRead(A0)→0…1023\text{analogRead(A0)} \to 0 \ldots 1023The knob, as before.

Now the top of the knob.

The knob reads 1023. What does analogWrite get after level / 4?

analogWrite(9, 64). For how much of each cycle is the pin on?

As Sam turns the knob up, the LED gets brighter, then suddenly dark, then brighter again, four times over. 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.

Fix the sketch so the knob sets the brightness smoothly, all the way to fully on.

Use the board. This ticks when you get there.

This LED is on pin 8 (no ~). Upload: what does analogWrite(8, 128) do? Then try 100.

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.

PWM

You want the LED on for 75% of each cycle. What value? (whole number)

Which Uno pin can fade an LED with analogWrite?

MakeMake your own problem using what you have learned. Expect a few tries; that is normal.

Turn it round

Try it yourselfYour edits are saved in this browser.
Need a hint?

When level / 4 is 0 you want 255, and when it is 255 you want 0.

Another hint

255 minus something.

Another hint

analogWrite(9, 255 - level / 4);

Change it so the LED is brightest with the knob at 0 and off at the top. Test it at a few positions.

Use the board. This ticks when you get there.

For teachers

Students see analogWrite(9, 128) as a fast on-off signal rather than a lower voltage, scale a knob's 0–1023 down to 0–255, meet the real board's behaviour when they don't (it wraps round), find that only the ~ pins can do PWM, and turn the mapping round. About 45 minutes.

Open the teacher guide: answers, solutions, slides and a worksheet.

Students will be able to

Before class

Model: PWM pins 3, 5, 6, 9, 10, 11; analogWrite keeps the lowest 8 bits (so 300 acts like 44), as the Uno's timers do; on other pins it sets HIGH for 128 and above. The signal is drawn as six cycles with the on part as wide as value/255. Real PWM on pin 9 runs at about 490 Hz.

The same idea controls motor speed (Power and motors) and dims LEDs in screens.

Running the lesson

  1. Predict: ask what an LED would look like at 2.5 V (it may not light at all), versus flickering faster than the eye can see.
  2. Find the mistake: turn the knob slowly so students see the four ramps.
  3. Tinkercad: the oscilloscope is the best moment of the lesson.

The hard part (on purpose)

'Half brightness means half voltage' is intuitive and wrong. Keep the drawn signal in view: always full height, only the width changes.

When students get stuck

Stretch and support

Stretch: Fade in and out by itself with a for loop: for (int b = 0; b <= 255; b++) { analogWrite(9, b); delay(5); }

Support: Draw three signals for 64, 128 and 191 and shade the on parts.

Where it leads

Next: Real hardware (breadboard and upload), then Power and motors (the same PWM sets motor speed through a driver).

Sources

What each step is for

Now you can
Next lesson6. From simulator to breadboardA breadboard joins the five holes in each numbered column (a–e, and separately f–j) with a metal strip; the rails along the edges join along their length. Each leg of a part must be in a different strip, and the loop goes from strip to strip through the parts.