Introduction To Coding Class

Lesson #4: Fade an LED

Mission Brief: The Fading Signal

The maintenance bay of Cogsworth Station hummed with quiet efficiency as you checked your tablet for the next repair assignment. Emergency lighting systems throughout the lower decks had been malfunctioning, creating harsh, jarring on-off cycles that disrupted sleep cycles and strained tired eyes.

Chief Engineer Vasquez appeared beside your workbench, her weathered hands gesturing toward the flickering test LED on your breadboard. "We need smooth transitions," she explained, her voice carrying the weight of countless sleepless nights. "When the lights dim for night shift, they should fade gently, not snap off like broken circuits. The crew's already on edge from the hull breach repairs, and sudden lighting changes are pushing people to their breaking point."

She handed you a data pad displaying power consumption charts. "The old system was binary, full power or nothing. But our new HERO boards can do something the original designers never imagined: they can control brightness with precision. Instead of just turning lights on or off, we can make them breathe, fade in and out like a gentle tide."

The implications hit you immediately. Smooth lighting transitions could transform the harsh industrial environment of Cogsworth Station into something more livable, more human. Your fingers moved instinctively toward your toolkit as Vasquez continued, "The technique is called Pulse Width Modulation. Instead of changing voltage, we change timing. Rapid pulses of full power, so fast the human eye sees them as steady dimming. It's elegant, efficient, and exactly what we need to keep this crew functioning."

Outside the viewport, the cold stars offered no comfort, but in your hands, the simple LED held the promise of bringing a touch of warmth to the metal corridors of home.

What You'll Learn

When you finish this lesson, you'll be able to:

  • Make an LED smoothly fade from bright to dim and back again
  • Understand how Pulse Width Modulation (PWM) creates the illusion of dimming
  • Use the analogWrite() function to control LED brightness with precision
  • Create automatic fading effects that reverse direction at the brightness limits
  • Control the speed of fading by adjusting delay timing
  • Identify which pins on your microcontroller support PWM output

Understanding PWM: The Art of Fake Dimming

Your microcontroller lives in a digital world of absolutes: pins are either HIGH (5 volts) or LOW (0 volts). There's no in-between, no partial voltage settings. So how do we create the smooth dimming effect that Chief Vasquez needs for the station's lighting system?

The answer lies in a clever trick called Pulse Width Modulation, or PWM. Think of it like a strobe light at a dance club, but flashing so fast your eyes can't detect the individual flashes. Instead of reducing the voltage to dim the LED, PWM rapidly switches the full 5 volts on and off. When the LED spends more time OFF than ON, it appears dimmer. When it spends more time ON than OFF, it appears brighter.

Imagine you're signaling with a flashlight to a friend across a dark courtyard. If you flash it on for 1 second and off for 9 seconds repeatedly, your friend sees a dim, weak signal. But if you flash it on for 9 seconds and off for only 1 second, the signal appears much brighter. PWM works the same way, but instead of seconds, it uses microseconds, switching thousands of times per second.

Your HERO board can generate PWM signals on specific pins marked with a tilde (~) symbol. These pins have special hardware that can maintain precise timing while your main program continues running. The analogWrite() function controls this hardware, accepting values from 0 (always off, completely dark) to 255 (always on, full brightness). A value of 127 means the LED is on exactly half the time, creating a 50% brightness effect.

This technique revolutionized electronics because it's incredibly efficient. Instead of wasting energy as heat (like old incandescent dimmer switches), PWM delivers full power in precise bursts, making it perfect for everything from LED lighting to motor speed control to audio amplifiers.

Wiring Your Fading LED

  1. Connect the LED's long leg (anode) to pin 9 on your HERO board. Pin 9 is PWM-capable, marked with the ~ symbol.
  2. Connect a 220-ohm resistor from pin 9 to the LED's anode. This resistor protects both the LED and your board from excess current.
  3. Connect the LED's short leg (cathode) to GND on your board. This completes the circuit.