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

Circuit

The Arduino circuit created in TinkerCAD
The generic ’s that you get in your starter kit have two legs on them. If you look closely at them, you can see that one is longer than the other. The long leg is the positive side whereas the short leg is the negative side. For this circuit, you only need two wires and a . Start by connecting the positive LED leg to a pin. Now connect a 220 Ohm to your negative LED-leg and another wire to connect the and ground.

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.

Articles mentioning this page