The Raspberry Pi Pico is an incredibly versatile microcontroller, perfect for diving into embedded electronics. One of the most fundamental and rewarding projects for any beginner is making an LED flash. This tutorial will guide you through connecting an external LED to your Pico and programming it with MicroPython to create a simple, rhythmic blink.
Understanding LED Connections and Resistors
Before we light up our LED, it is crucial to understand how to connect it safely. LEDs are current-sensitive devices and require a current-limiting resistor to prevent damage. There are two common ways to connect an LED to a microcontroller pin:
- Current-Sourcing Mode: The LED turns ON when the port pin outputs a logic HIGH signal (e.g., +3.3V). The current flows from the pin, through the resistor, through the LED, and to ground.
- Current-Sinking Mode: The LED turns ON when the port pin outputs a logic LOW signal (e.g., 0V). In this setup, the LED is connected to the positive supply rail, and the current flows from the supply, through the LED, through the resistor, and then sinks into the port pin when it's low.
For this project, we'll use the current-sourcing method, which is often simpler for initial setups. The diagram below illustrates an LED connected in current-sourcing mode.
Calculating the Current-Limiting Resistor
To determine the appropriate resistor value, we need to consider the Pico's output voltage, the LED's forward voltage drop, and the desired current through the LED. For a typical red LED, the forward voltage drop is around 2V, and a safe operating current is about 3mA. The Raspberry Pi Pico's GPIO pins operate at 3.3V.
Using Ohm's Law (R = V/I), the resistor value can be calculated as follows:
R = (Pico Output Voltage - LED Forward Voltage) / Desired LED Current
R = (3.3V - 2V) / 0.003A
R = 1.3V / 0.003A
R = 433 ohms
The closest standard resistor value is 470 ohms, which we will use for our circuit.
Hardware Setup: Connecting the LED to Raspberry Pi Pico
Our project will connect an external LED to General Purpose Input/Output (GPIO) pin 0 (GP0), which is physical pin 1 on the Pico board. The connection will be as follows:
- Connect one leg of the 470-ohm resistor to GP2 on the Raspberry Pi Pico.
- Connect the other leg of the 470-ohm resistor to the anode (longer leg, positive) of the LED.
- Connect the cathode (shorter leg, negative) of the LED to a GND (ground) pin on the Raspberry Pi Pico.
The circuit diagram below provides a visual representation of the Raspberry Pi Pico and the external LED you'll be connecting.
Programming the Pico: Flashing with MicroPython
With the hardware connected, it's time to program our Raspberry Pi Pico using MicroPython. This code will toggle the LED on and off every second.
I used the following code, which is straightforward and effective:
from machine import Pin
import utime
LED = Pin(2, Pin.OUT) # LED at GP2
while True: # DO FOREVER
LED.value(1) # LED ON
utime.sleep(1) # Wait 1 second
LED.value(0) # LED OFF
utime.sleep(1) # Wait 1 second
Let's break down what each part of this code does:
from machine import Pin: This line imports thePinclass from themachinemodule, which allows us to control the Pico's GPIO pins.import utime: Theutimemodule provides time-related functions, specificallysleep()for introducing delays.LED = Pin(2, Pin.OUT): Here, we initialize GP2 (pin number 2) as an output pin. We assign this configured pin object to the variableLEDfor easy reference.while True:: This creates an infinite loop, ensuring the code inside it runs continuously after being uploaded to the Pico.LED.value(1): Sets the voltage of GP2 to HIGH (3.3V), turning the LED ON.utime.sleep(1): Pauses the program for 1 second.LED.value(0): Sets the voltage of GP2 to LOW (0V), turning the LED OFF.utime.sleep(1): Pauses the program for another 1 second.
This sequence causes the LED to flash on for one second, then off for one second, repeating indefinitely. For more advanced projects and to explore the full capabilities of the Raspberry Pi Pico, you might find resources like Raspberry Pi Pico 2 or even general Raspberry Pi tutorials like Raspberry Pi 3 helpful.
Next Steps: Beyond Basic Flashing
This simple LED flashing project is just the beginning. You can expand on this by changing the delay times, adding more LEDs, or even creating complex patterns. For instance, you could implement a Morse code flasher, like the SOS signal (three short flashes, three long flashes, three short flashes), which involves varying the utime.sleep() durations. This demonstrates how basic principles can lead to more sophisticated applications.
Conclusion
You have successfully connected an external LED to your Raspberry Pi Pico 2 and programmed it to flash using MicroPython. This foundational project is essential for anyone starting with microcontrollers, providing a clear understanding of hardware connections, resistor calculations, and basic programming logic. Keep experimenting with different timings and patterns to deepen your understanding!
