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.

5 mm red · Vf ≈ 1.9 V @ 20 mA
Wiring
| LED pin | Arduino Uno |
|---|---|
| pin 1 | D9 ~ (through a 220 Ω resistor) |
| pin 2 | GND |
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
- Arduino traffic light with a pedestrian button
- Arduino reaction-time game on an I²C LCD
- Arduino Simon memory game: four LEDs, four buttons, a buzzer
- Arduino parking sensor: HC-SR04, three LEDs and a beeper
- Arduino automatic plant watering: soil sensor, relay and pump
- Arduino motion alarm: PIR sensor, siren and arm button
- Traffic light with a pedestrian button
- Reaction-time game
- Simon memory game
- Ultrasonic parking sensor
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.