PAGE 106 OF 150 · MICROCONTROLLERS & LEDS
Blinking an LED with a Microcontroller
A first program toggles a pin on and off.
LED SYMBOLTriangle points from anode to cathode; the arrows show light leaving the part.
Blinking from code
To blink an LED, set a pin as an output, switch it high, wait, switch it low, wait, and repeat. A half-second wait each way gives one blink per second.
- Connect the LED and a 220 to 330 ohm resistor between the pin and ground.
- Many classic boards have a built-in LED on pin 13.
Better timing
A delay stops the program doing anything else. Instead check the millis counter and toggle the LED when the set time has passed, which lets buttons and sensors stay responsive.
REMEMBER
Use millis instead of delay if the program has other jobs.Try it yourself
Sketch the circuit on paper first, calculate the values, then build it on a solderless breadboard using a low-voltage supply. Measure with a multimeter before you make anything permanent.
Explore LED clocks, stopwatches and electronics at cosycom.com.