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.
Predict: Read the problem and decide what you think before you try anything. A wrong guess is fine: it shows you what to look for.
Try: Try it on the board and compare the result with your prediction.
Investigate: Work out why it happens, one step at a time.
Modify: Change the problem on purpose and see what happens.
Practise: A few on your own, to check you have it.
Make: Make your own problem using what you have learned. Expect a few tries; that is normal.
WidthMore contexts, same idea.
Make your prediction first. Help opens once you've locked it in.
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
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.
analogRead(A0)→0…1023The knob, as before.
analogWrite←0…255Brightness: 0 off, 255 fully on.
1024÷256=4The input range is 4 times the output range.
512÷4=128Halfway on the knob gives 128.
128÷255≈0.5On for about half of each cycle.
Now the top of the knob.
The knob reads 1023. What does analogWrite get after level / 4?
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)
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.
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
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.
Find the mistake: turn the knob slowly so students see the four ramps.
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
A student uses analogWrite on pin 8. Ask: Is there a ~ next to 8? Fix: Use 3, 5, 6, 9, 10 or 11.
Brightness jumps as the knob turns. Ask: What is the biggest number analogWrite takes? Fix: level / 4.
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).
128: Committing to what analogWrite(9, 128) does to the pin tests the new move before the signal is shown: on and off fast, not a lower voltage.
Try: see the signal: The drawn signal settles the prediction: full height, half the time.
Worked example: knob to brightness: Scaling 0–1023 down to 0–255 models the move with numbers; Sam's missing ÷ 4 is planted after the method, with what the real board does (it keeps only the low 8 bits).
Vary: change one thing: Fixing Sam's range, then moving to a non-PWM pin, shows both limits of analogWrite on the board.
Build it in Tinkercad: Tinkercad's oscilloscope shows the real PWM signal, so students see the same square wave on a fuller simulator.
PWM: Values and fractions both ways, and which pins can do it.
turn it round: Reversing the mapping (brightest at the bottom of the knob) makes students reason about the calculation rather than copy it.