Raspberry Pi Pico Alternating Two LEDs (Blink-Flip Pattern)
The next step after the Pico's single LED: the classic flip-flop animation, with two LEDs blinking in turn. One LED turns off as the other turns on, the most basic example of handling multiple GPIOs. Two LEDs of different colors (red on GP14, green on GP13) swap every 500 ms. With MicroPython's Pin.toggle() method, the code stays short and readable.

Video
Gambar Rajah Litar

Kod Sumber
1# ─────────────────────────────────────────
2# BlueGrays · bluegrays.com
3# Raspberry Pi Pico Alternating Two LEDs (Blink-Flip Pattern, GP14 + GP13)
4# ─────────────────────────────────────────
5# Two LEDs blink alternately every 0.5 s (flip-flop): one is on while the
6# other is off. Pin.toggle() keeps the code short.
7#
8# Pins:
9# GP14 -> Red LED (with a 220 ohm resistor)
10# GP13 -> Green LED (with a 220 ohm resistor)
11# LED katotlari -> LED cathodes -> GND
12
13from machine import Pin
14from time import sleep
15
16# ============ PIN / PIN ============
17led1 = Pin(14, Pin.OUT) # Red LED
18led2 = Pin(13, Pin.OUT) # Green LED
19
20# ============ INIT ============
21# Start with opposite states
22led1.value(1)
23led2.value(0)
24
25print("== BlueGrays: 2 LED Alternating ==")
26print("LED1 (Red): GP14 | LED2 (Green): GP13")
27
28# ============ MAIN LOOP ============
29while True:
30 # Toggle both LEDs
31 led1.toggle()
32 led2.toggle()
33 sleep(0.5) # 500 ms delay
34
Soalan Lazim
What does the `toggle()` method do?
Pinin reverses the current state: if it's on, it turns it off; if it's off, it turns it on. If one is set to on and the other to off at the start, the two LEDs switch places with each toggle.
How do I change the flashing speed?
Change the value in `sleep(0.5)` at the end of the code; `sleep(0.2)` gives faster transitions, and `sleep(1)` gives slower ones. To use `sleep_ms()`, also add `from time import sleep_ms`.
What should I do if both LEDs are on at the same time at the start?
Before the loop, initialize one LED with `on()` and the other with `off()`. Otherwise, both will start in the same state and will flash together when you use `toggle`.
Can I try this project on Wokwi?
Yes, Wokwi supports the Raspberry Pi Pico and MicroPython. You can add components and connect them to the same pins to test your code without actual hardware.
