Raspberry Pi Pico
Digital Inputs and Outputs
Edited: April 2024
Digital Output Signal — LED
​
Now we know the basics and how to control the onboard LED on the Pico board, we will look into sending and receiving an external signal to and from the Pico. Here, we'll look at how to control an external LED connected to the Pico board by sending out a digital signal from the board.
​
Components we need:
-
Raspberry Pi Pico board
-
Breadboard
-
LED
-
Resistor (50 - 330 ohms)
-
Male - Male jumper wires
​
Wiring
-
Fix your Raspberry Pi Pico board and LED on the breadboard.
-
Connect the shorter leg of your LED to any GND pin of the Pico.
-
Connect one end of your resistor to the anode leg of the LED, then the other end of the resistor to GPIO15 of the Pico and the cathode leg of the LED to any GND pins of the Pico, as shown below:
Code
​
Below are sample codes to blink the external LED connected to our Pico using the picozero library.
​
Turn an LED on and off:
​
from picozero import LED
from time import sleep
led = LED(15)
​
led.on()
sleep(1) # pause for one second
led.off()
​
​
​
Toggle an LED to turn it on or off:
​
from picozero import LED
from time import sleep
led = LED(15)
while True:
led.toggle()
sleep(1) # pause for one second
​
​
​
​
Alternatively, you can use the blink() method:
​
from picozero import LED
led = LED(15)
led.blink() # on for 1 second then off for one second
​
​
​
Run the code, you should see the LED start to blink.
Digital Input Signal — Pushbutton
​
We saw above how we can send out a digital signal to control connected peripherals such as turning on and off an LED. Now, let's see how we can receive a digital signal from a push button.
Let's connect a push button to a Raspberry Pi Pico and detect when they are pressed.
​
Components we need:
-
Raspberry Pi Pico board
-
Breadboard
-
Push button
-
LED
-
Resistor (50 - 330 ohms)
-
Male - Male jumper wires
​
Wiring
-
Fix your Raspberry Pi Pico board and the push button on the breadboard.
-
Connect the button to the Pico with one leg to Pin GP14 and the other to any GND Pin of the Pico, as shown below:
Code:
​
Create a new file in Thonny and add the code below:
​
from picozero import Button
from time import sleep
button = Button(14)
while True:
if button.is_pressed:
print("Button is pressed")
else:
print("Button is not pressed")
sleep(0.1)
​
​
​
Now, let's connect an LED to the Pico and then use the signals from the button to turn on and off the LED accordingly.
​
​
Circuit
Add LED to your previous push button circuit.
​
-
Connect one end of your resistor to the anode leg of the LED, then the other end of the resistor to GPIO15 of the Pico, and the cathode leg of the LED to any GND pins of the Pico, as shown below:
Code
​
Create a new file in Thonny and add the code below:
​
from picozero import Button, pico_led
button = Button(14)
button.when_pressed = pico_led.on
button.when_released = pico_led.off
​
​
​
Run your code: When you press the button, the LED should come on and then go off when you release the button.