Analog Output: PWM and analogWrite
Fade an LED smoothly instead of just switching it, using PWM output on the Uno's marked pins.
You'll need: an LED, a resistor, a breadboard, and your Uno.
Lesson 30 explained PWM in theory: rapid on/off switching whose duty cycle controls the perceived average power. On an Uno, only certain digital pins can generate PWM in hardware — they're marked with a small "~" next to the pin number on the board silkscreen (pins 3, 5, 6, 9, 10, and 11). Wire your LED and resistor, exactly like Lesson cb-02, to one of those PWM-capable pins instead of any plain digital pin.
void setup() {
pinMode(9, OUTPUT);
}
void loop() {
analogWrite(9, 64); // roughly 25% brightness
delay(1000);
analogWrite(9, 191); // roughly 75% brightness
delay(1000);
}
analogWrite(pin, value) takes a duty-cycle value from 0 (always off) to 255 (always on) and generates the corresponding PWM signal on that pin — despite the name, it isn't a true analog voltage, just the same rapid switching from Lesson 30, done automatically in hardware rather than something you'd have to time yourself with delays.
Try replacing the two fixed values above with a for loop that counts from 0 to 255 and back down, with a short delay each step, for a smooth, continuous fade rather than two fixed brightness levels — a natural next experiment once the basic version above is working.