#2Beginner

Two LEDs Blinking Alternately

4.6(102)
303 completed
Two LEDs Blinking Alternately
Project that creates a flip-flop effect by sequentially turning on and off two LEDs. Teaches how to control multiple output pins.

Video

Circuit Diagram

Two LEDs Blinking Alternately - Circuit

Source Code

Arduino (C++)
1// ─────────────────────────────────────────
2//   BlueGrays · bluegrays.com
3//   Two LEDs Blinking Alternately
4// ─────────────────────────────────────────
5// Lights two LEDs in turn every 200 ms (flip-flop effect). Shows how to
6// control more than one output pin.
7//
8// Pins:
9//   D8 -> LED 1 (with a 220 ohm resistor)
10//   D9 -> LED 2 (with a 220 ohm resistor)
11
12void setup() {
13  pinMode(8,OUTPUT);     // LED 1 output
14  pinMode(9,OUTPUT);     // LED 2 output
15}
16
17void loop() {
18  // LED 1 on, LED 2 off
19  digitalWrite(8,HIGH);
20  digitalWrite(9,LOW);
21  delay(200);
22  // Swap: LED 1 off, LED 2 on
23  digitalWrite(8,LOW);
24  digitalWrite(9,HIGH);
25  delay(200);
26}
27

FAQ

Can two LEDs be connected to the same resistor?▾

No, each LED must have its own resistor. If a single resistor is shared among them, the LEDs will not share the current equally; one will glow brighter or won’t light up at all. In this project, there is a separate 220 Ω resistor for each LED.

How does the flip-flop effect work?▾

When one LED is set to HIGH, the other is set to LOW; after a brief pause, their states switch. This causes the two LEDs to light up alternately. Writing opposite values to the two pins at the same time is the basis of this effect.

Which pins can I use?▾

On the Uno, all digital pins from 2 to 13 can be used to drive LEDs. Since pins 0 and 1 are used for USB serial communication, it is recommended that you leave them unused. If you change the pin, be sure to update the pin number in the code as well.

The LEDs are very dim. Why is that?▾

The `pinMode(pin, OUTPUT)` line is often omitted; a pin that isn't set to output will cause the LED to glow dimly. Also, if the resistance value is too high (e.g., 10 kΩ), the LED will glow dimly; use 220–330 Ω.

Two LEDs Blinking Alternately | BlueGrays