← Teacher guide

Outputs that vary

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.

Arduino · Writing C++ · about 45 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 12 min · Vary 8 min · Tinkercad 9 min · Practise and Make 10 min

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

What you will seeA question to askThe fix
A student uses analogWrite on pin 8.Is there a ~ next to 8?Use 3, 5, 6, 9, 10 or 11.
Brightness jumps as the knob turns.What is the biggest number analogWrite takes?level / 4.

Answers to the checks

  1. 128: analogWrite(9, 128); — what does pin 9 do?
    It switches fully on and off, about 490 times a second, on for 128/255 of each cycle. Your eye averages it, so the LED looks about half as bright.
  2. Try: see the signal: Upload and run. Look at the signal drawn beside the LED.

    The signal is either at the top (5 V) or the bottom (0 V); it is at the top for half of each cycle.
  3. Scale it: The knob reads 1023. What does analogWrite get after level / 4?
    255
    1023 / 4 = 255 in whole numbers: fully on.
  4. On how long?: analogWrite(9, 64). For how much of each cycle is the pin on?
    About a quarter
    64 ÷ 255 ≈ 0.25: on a quarter of the time, at the full 5 V.
  5. Find the mistake: 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.
    line 8
    Line 8: analogRead gives 0–1023 but analogWrite takes 0–255. The board keeps only the lowest 8 bits, so 256 acts like 0 and it starts again. Use level / 4.
  6. Smooth: Fix the sketch so the knob sets the brightness smoothly, all the way to fully on.

    analogWrite(9, level / 4): 0–1023 becomes 0–255.
  7. Not a PWM pin: This LED is on pin 8 (no ~). Upload: what does analogWrite(8, 128) do? Then try 100.

    Pin 8 can't do PWM: analogWrite just sets it HIGH for 128 or more and LOW below. 100 is off.
  8. 75%: You want the LED on for 75% of each cycle. What value? (whole number)
    191
    0.75 × 255 ≈ 191.
  9. Which pin: Which Uno pin can fade an LED with analogWrite?
    10
    PWM pins are 3, 5, 6, 9, 10 and 11.
  10. turn it round: Change it so the LED is brightest with the knob at 0 and off at the top. Test it at a few positions.

    255 - level / 4 turns the scale round.

Solutions

One possible solution for each task. Other correct programs are fine.

Vary: change one thing › Smooth

Open task: any program that does what the step asks.

Vary: change one thing › Not a PWM pin

Open task: any program that does what the step asks.

turn it round

Open task: any program that does what the step asks.

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.

Exit ticket

Why does analogWrite(9, 128) make an LED look half as bright, if the pin is only ever 0 V or 5 V?

The pin is on for half of each cycle and switches too fast to see, so the eye sees the average: half as bright.

What each step is for

Where it leads

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

Sources