AR-2Boshlang‘ich

Two LEDs Blinking Alternately

4.6(110)
362 bajarildi
Two LEDs Blinking Alternately
Two LEDs blink quickly in turn: when one lights up the other goes off, as if they were tossing a ball back and forth. The Arduino keeps each light on for a fifth of a second and then hands over to the other. After a single LED, this is the easiest step towards controlling several lights together.

Video

Elektr sxemasi

Two LEDs Blinking Alternately - Elektr sxemasi

Manba kodi

C++ (Arduino IDE)
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

Ko‘p so‘raladigan savollar

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 Ω.