Simple LED Setup
Introduction
(light-emitting diode) have a wide range of uses since they are cheap, easy to integrate into your system, and straightforward to program. On top of this, they generate little to no heat and have low power consumption, which makes them ideal to use with microcontrollers.
In this guide, you will learn how to make a blink in one-second intervals.
Before continuing this guide it is recommended that you have read- or have basic knowledge about .
Supplies
- board
- Wires
- 220 Ohm
- (generic)
Circuit
Code A
// Simple LED setup
// This code will make your LED blink in one second intervals
// constants will not change value
// a pin is assigned to the LED
const int LEDPin = 2;
void setup() {
// initialize the LED as an output
pinMode(LEDPin, OUTPUT);
}
void loop() {
// turn on the LED
digitalWrite(LEDPin, HIGH);
// let the LED be on for one second
delay(1000);
// turn off the LED
digitalWrite(LEDPin, LOW);
// let the LED be off for one second
delay(1000);
}
Code B
void setup() {
setPin(2, OUTPUT);
}
void loop() {
digitalWrite(2, HIGH);
sleep(1000);
digitalWrite(2, LOW);
sleep(1000);
}
Challenges
Now that you have a basic understanding of how to program a LED, try making a traffic light with three LEDs (green, yellow, red) that changes color in the right order. You might want to use the 'delay()' function to make the LED's light up a certain amount of time before turning it off again.