Imago Dei Arduino Lab
Menu

Light it up

Your first circuit: make an LED glow.

Easy⏱ about 20 min

The goal

Wire an LED to the Arduino and turn it on with code.

You'll learn

  • ■Which way around an LED goes (it only works one way)
  • ■Why every LED needs a resistor
  • ■How to upload a program to the Arduino
  • ■What setup() and loop() mean

1You'll need

2Wire it up

Unplug the USB cable before you wire anything. Look closely at the LED: one leg is longer. The long leg is + (positive) and the short leg next to the flat edge is − (negative).

Safety first
Never connect an LED straight across 5V and GND with no resistor. It will burn out in a flash (literally).

Pins used on the Arduino

USBPWRUNOARDUINOATmegaLSCLSDAAREFGND213112~11~10~987~6~54~32TXRXIOREFRESET3.3V5VGNDGNDVINA0A1A2A3A4A5DIGITAL (PWM ~)POWERANALOG IN
  1. 1Pin 13 · Resistor → LED long leg (+)
  2. 2GND · LED short leg (−)
  3. All GND pins are connected together, so any GND works.

Step by step

  1. 1Push the LED into the breadboard so the two legs are in two different rows (for example, rows 10 and 11).
  2. 2Push one leg of the 220 Ω resistor into the same row as the LED's long leg. Put the other resistor leg in an empty row a few rows away.
  3. 3Run a jumper wire from the empty end of the resistor to Arduino pin 13.
  4. 4Run a jumper wire from the LED's short leg row to an Arduino GND pin.
  5. 5Double-check: pin 13 → resistor → LED long leg → LED short leg → GND. Then plug in the USB cable.

Connection checklist

FromTo
☐Arduino pin 13220 Ω resistor
☐Other end of resistorLED long leg (+)
☐LED short leg (−)Arduino GND

3The code

Click Copy code, paste it into a new sketch in the Arduino IDE (replacing everything), then click Upload.

LED on
// Session 1 - Light it up
// Turns an LED on and leaves it on.

int ledPin = 13;   // the pin our LED is wired to

void setup() {
  pinMode(ledPin, OUTPUT);     // this pin will send power OUT
  digitalWrite(ledPin, HIGH);  // HIGH = 5 volts = LED on
}

void loop() {
  // nothing to do here - the LED just stays on
}

4How it works

setup() runs once

Everything inside setup() happens one time when the Arduino powers on. We use it to tell the Arduino that pin 13 is an output, then switch it on.

loop() runs forever

After setup() finishes, loop() repeats over and over, thousands of times per second. Ours is empty, so the LED just stays on. In the next tutorial we'll put code in here.

HIGH and LOW

digitalWrite(pin, HIGH) puts 5 volts on the pin. digitalWrite(pin, LOW) puts 0 volts on it. That's the whole secret of turning things on and off.

Why the resistor?

An LED lets through as much current as it can get, which is enough to destroy it. The 220 Ω resistor limits the current to a safe amount. Think of it as a speed bump.

5Try this

  • AChange HIGH to LOW, upload again. What happens?
  • BMove the resistor wire from pin 13 to pin 7 and change ledPin to 7. Does it still work?
  • CSwap the LED around (long leg to GND). Why doesn't it light?
  • DAdd a second LED on another pin and turn both on.

6Stuck?

LED doesn't light.+

Check the LED direction: long leg goes toward the resistor and pin 13. Make sure the resistor and LED legs are in the same breadboard row.

Upload fails with a port error.+

Go to Tools → Port and pick the one labeled Arduino Uno. If none shows up, try a different USB cable or port.

The little L light on the board turns on but my LED doesn't.+

Good sign: pin 13 is working. The problem is the wiring. Re-check every connection in the table above.

Next: Blink! →