Feb 25, 2021 By Team YoungWonks *
How can one write a program with the Raspberry Pi Pico using an LED and a button? With the Raspberry Pi Pico now available in the market, this is a relevant query now. In this blog, we shall take a look at using the LED and button with the Raspberry Pi Pico.
What is Raspberry Pi Pico?
Raspberry Pi refers to a series of small single-board computers developed in the United Kingdom by the Raspberry Pi Foundation in association with Broadcom. The boards have been made keeping in mind the promotion of teaching basic computer science to kids. The latest offering by Raspberry Pi is the Raspberry Pi Pico, a new flexible IoT board. Essentially, it is a microcontroller board built on silicon and designed at the Raspberry Pi Foundation.
Priced at merely $4, Raspberry Pi Pico is smaller than the average Pi and indicates how the foundation is now looking to branch out into microcontrollers and custom silicon.
Raspberry Pi Pico pinout
In order to use it well, it is recommended to get well acquainted with the Raspberry Pi Pico pinout. The diagram below explains this.
Raspberry Pi Pico projects
As a microcontroller, the Raspberry Pi Pico can be used in many projects, be it Internet of Things (IoT), Adafruit Neopixel projects, data logging, small to medium scale robotics, projects needing interfacing with cameras, analog sensing (using environment sensors) and more.
Using LED and button with Raspberry Pi Pico to write a program
Once the Raspberry Pi is set up, we can look to write a program with the Raspberry Pi Pico using an LED and a button. For this we shall need the following: a Raspberry Pi Pico on breadboard, a push button, an LED, two resistors and three wires.
Next let’s build a circuit using the circuit diagram provided here on the screen.
One can also use this wiring diagram for reference.
Now we begin by placing the LED on the breadboard and a button as well.
Then you connect a resistor from the negative end of the LED to the ground.
Next you connect a GPIO pin to the positive leg of the LED.
Then you connect one end of the button to the ground through a resistor and the other end to BCC or power of 3.3 volts using a wire.
After this we connect the leg of the wire connected to ground to the GPIO pin that has been configured as an input.
Next we move to the coding part of it. First we import libraries that allow us to configure the pins on the Pico.
We also use the time library that will come in handy when we have to link the LED.
We set up 16 as the output in our example but one can use the Raspberry Pi Pico pinout diagram (shared earlier in this blog) like any other GPIO pin.
Here we have configured pin 15 as the input from the button.
We then have a while true loop where we constantly print the value of the button. This allows us to debug the program in case it doesn’t work.
Whenever the button goes high, you can toggle the LED.
We also use the sleep option so that it doesn’t detect any other button presses and also helps in debouncing (the debounce function forces a function to wait a certain amount of time before running again).
Now whenever you press the button, the LED turns on.
And if you press it again, it turns off.
You can watch this video to take a quick look at how one can work with the newly launched Raspberry Pi Pico using an LED and a button:
*Contributors: Written by Vidya Prabhu; Lead image by: Leonel Cruz