Fade an LED from off to maximum brightness in a smooth ramp. Control the speed of a small motor. By the end of the week you understand pulse-width modulation, you can use analogWrite to set a pin's output level from 0 to 255, and you can pick the right pin for the right job.
Reading (~45 min)
- Arduino reference for
analogWrite. Pay attention to which pins support PWM (marked with~on the board) - Optional: SparkFun's "Pulse Width Modulation" tutorial
Lecture (~1.5 hr)
- What PWM actually is. The pin switches between HIGH and LOW very fast (~490 Hz on most Arduino pins). The "duty cycle" is the fraction of time the pin is HIGH. 100% duty = pin always high; 0% duty = pin always low; 50% duty = half the time on, half off
- Why this works for LEDs. The LED switches on and off too fast for your eye to see; your eye averages the rapid on-off into a steady-looking brightness. 50% duty appears half as bright as 100% duty
- Why this works for motors. The motor's inertia smooths the rapid on-off into a steady speed. Lower duty = lower average current = slower spin
- analogWrite(pin, value). Value is 0-255 (8-bit). 0 = always off; 255 = always on; intermediate values = proportional duty cycle. Despite the name, this is NOT actually analog output; it is PWM. The naming is unfortunate; the behavior is what matters
- Pins that support PWM. On the R4 Minima: pins 3, 5, 6, 9, 10, 11. Not all pins. If you analogWrite to a non-PWM pin, you get full off or full on depending on whether your value is above or below half. Read the pinout
Lab exercises (~2 hr)
Lab 10.1: Fade PWM. Smoothly fade an LED from off to fully bright over 2 seconds; then fade back down; repeat. The Arduino's Fade example sketch is your starting point. ~60 minutes.
Independent practice (~3 hr)
- Build a "breathing" LED that fades up and down continuously with a sinusoidal pattern (use the sin() function and map the result to 0-255). The breathing effect is smoother than linear fade
- Combine PWM with the photoresistor from week 9: brightness of an LED mirrors ambient light level. Map analogRead's 0-1023 to analogWrite's 0-255
- Drive the kit's small DC motor with PWM through the NPN transistor from week 6. Vary the duty cycle to vary the speed. Listen for the PWM frequency; you may hear it as a faint whine
Reflection prompts
- PWM gives the appearance of analog output via fast digital switching. What is lost compared to a true analog output? When would the difference matter?
- The Arduino's PWM frequency is ~490 Hz. For an LED that is plenty (your eye sees the average). For a motor it is also plenty. For an audio speaker it would be inadequate. Why?
- analogWrite uses 8-bit values (0-255). analogRead returns 10-bit values (0-1023). Mismatched. What does map() do to bridge them, and what precision do you lose?
What's next
Week 11 introduces the Serial Monitor as your primary debugging tool. Every sketch from now on will print useful information to the Serial Monitor so you can see what the Arduino is thinking.