Home › Wiring › LED with Arduino Uno

LED with Arduino Uno

How to connect an LED to an Arduino Uno: the wiring, a sketch that works, and a simulator to try it in your browser before you build it.

LED wired to an Arduino Uno in Zen Circuit Lab

5 mm red · Vf ≈ 1.9 V @ 20 mA

Wiring

LED pinArduino Uno
pin 1D9 ~ (through a 220 Ω resistor)
pin 2GND

Every LED needs a series resistor: 220 Ω on a 5 V Arduino gives about 13 mA; on a 3.3 V ESP32 about 6 mA.

Run it in the labFree, no account needed. The same wiring, with the sketch already on the board.

Code

From the project Arduino automatic night light: LDR and a PWM LED. It also compiles in the Arduino IDE with the libraries it includes.

// Automatic night light: an LDR and a 10 kΩ resistor make a divider on A0 (bright = high reading).
// The darker it gets, the brighter the LED on D9 (PWM). Fully on below about 5 lux, off in daylight.
const int LDR = A0, LAMP = 9;
const int DARK = 150, BRIGHT = 700;      // readings at night and in a lit room
unsigned long shownAt = 0;

void setup() {
  Serial.begin(9600);
  pinMode(LAMP, OUTPUT);
}

void loop() {
  int light = analogRead(LDR);
  int duty = map(constrain(light, DARK, BRIGHT), DARK, BRIGHT, 255, 0);
  analogWrite(LAMP, duty);
  if (millis() - shownAt >= 1000) { shownAt = millis(); Serial.print("Light "); Serial.print(light); Serial.print("  lamp "); Serial.print(duty * 100 / 255); Serial.println(" %"); }
  delay(20);
}

Also used in

For teachers

The project runs in the browser on a laptop or a phone, so every student can build and test it before the class touches a real board. Assign it on the teacher's page and set what counts as done: a prediction, a hidden fault found, or a passing test sheet.

Related