In this tutorial, we will learn how to use a push button in order to turn a led on and off with Raspberry Pi Pico using MicroPython. Previously, we learned how to use GPIO pins for the Pi Pico board as output pins but in this tutorial, we’ll see how the same GPIO pins can also be used to acquire the digital inputs from the module. Accordingly, we will also show how to interface a push button with Raspberry Pi Pico. We will read the state of a push Button using the input pin of pic and turn on the Led consequently.
Table of Contents
Push Button Interfacing Raspberry Pi Pico Tutorial Prerequisites
Before we start this lesson make sure you are familiar with and have the latest version Python 3 in your system, have set up MicoPython in Raspberry Pi Pico, and have a running Integrated Development Environment(IDE) in which we will be doing the programming. We will be using the same Thonny IDE as we have done previously when we learned how to blink and chase LEDs in micro-python. If you have not followed our previous tutorial, you check here:
If you are using uPyCraft IDE, you can check this getting started guide:
Using GPIO Pins of Raspberry Pi Pico as Digital Input / Output
GPIO pins act as both input and output pins with an exception for a few. We will be using the GPIO Pins as digital input and as digital output pins in our Raspberry Pi Pico, this time when connecting the push button and the LED respectively. One digital pin will be connected to the push button and another one for the LED. We will be taking in the digital input from the push button and acquiring the digital output from the LED.
Below is the diagram showing the GPIO pins for Raspberry Pi Pico which we will be using to connect the push button and the LED.
As shown in the above diagram we will be using GPIO 14 to connect the LED and GPIO 13 to connect the push button.
You should read the following article to know more about GPIO pins of Raspberry Pi Pico.
Push Button Interfacing Raspberry Pi Pico MicroPython
We will read the state of the push button. The push button will give two logical states either high or low. We will connect the push button with the Raspberry Pi Pico board using a resistor.
There are two types of resistors that can be used in this project. Pull-up and pull-down resistors. We will be using Pull- down resistors which work on the principle that whenever the push button is pressed, the input to GPIO pin will be logic high state (1) and otherwise logic low state (0). If it is a logic state high that means 1 the LED would switch on and if the logic state is low that means 0 then the LED is off. It works vice-versa if we use pull-up resistors instead.
Assembling the Circuit of Push button with Raspberry Pi Pico. Now we will connect the circuit with the LED and push button. The following components are required.
- Raspberry Pi Pico
- One LED
- 220 ohm resistor
- 10k ohm resistor
- Connecting wires
The below picture shows the schematic for Raspberry Pi Pico with a push button and LED. Assemble your circuit as follows:
Schematic of Raspberry Pi Pico with a Push Button and an LED
In the above schematic, we can see that GPIO14 is connected with the anode pin of LED, and the cathode pin is connected with the common ground through the 220-ohm resistor.
The push-button has four terminals. One terminal is powered by 3.3 volts from Raspberry Pi Pico and the other terminal is connected by GPIO13 and the 10k ohm resistor which acts as a pull-down resistor. The other end of the resistor is connected with the common ground.
When the push button is pressed, a logic state of high (1) will be passed on GPIO13 and the push button input will be in a high state. When the button is released a logic state of low (0) will be passed on GPIO13 and the push button input will be in a logic state LOW. We will read these two states of the push button and turn on and turn off the LED accordingly.
Control LED with Button MicroPython Script
We will create a script to control an LED with a push button for Raspberry Pi Pico in MicroPython. Follow the steps in the particular order:
Open Thonny IDE. Create a new file by clicking on the ‘New File’ icon or go to File > New.
The following code reads the state of the push button and lights up the LED accordingly. Copy the following code in the new file that you just opened and save it as a main.py file in Thonny IDE.
from machine import Pin from time import sleep print('Microcontrollerslab.com') led = Pin(14, Pin.OUT) # 14 number in is Output push_button = Pin(13, Pin.IN) # 13 number pin is input while True: logic_state = push_button.value() if logic_state == True: # if push_button pressed led.value(1) # led will turn ON else: # if push_button not pressed led.value(0) # led will turn OFF
After you have copied the following code onto the file click on ‘Save’ icon to save your program code on your PC. Save your file by giving it a name main.py and save according to your preference by giving the directory.
After we have saved the code press the Run button to upload the code to your Raspberry Pi Pico module. Before uploading code make sure the correct board is selected.
Now if you press the push button, the LED will glow and if you leave the push button, a LED will remain off.
How Code Works?
Firstly, we will import the library for the Pin class from the machine module. To interact with the input/output GPIOs we will import the machine module that contains classes to interact with the GPIOs. We will import the Pin class to interact with the pins.
from machine import Pin
MicroPython Define Digital Output
To set a GPIO on or off, we will set it as an output. The first argument in the Pin() class is the pin number on which we are configuring the output. The output is on GPIO14 which is connected to the LED. The second argument shows the pin mode e.g. digital input or digital output mode. As we are configuring pin 14 as the digital output we gave, specifies it as ‘Pin.Out’. This is stored in the object ‘led’.
led = Pin(14, Pin.OUT)
MicroPython Define Digital Input
To get the value of a GPIO, we will create a Pin object and set it as an input using ‘Pin.IN’ as the second argument in our Pin() class. The input is on GPIO13 which we specified in the first argument and is connected to the push button. This is stored in the object ‘push_button’.
push_button = Pin(13, Pin.IN)
The push button acts as the input and the LED as the output.
We use an infinite loop so we can perform our project endlessly and it does not come to a halt.
while True: #infinite loop
Read Push Button State
In order to obtain the value of the input GPIO, we use the function value() on the Pin object without passing any argument inside it. In this case, push_button.value() was used in our program code. The value we would obtain from this is getting stored in the variable logic_state which denotes the logic state of the push button.
logic_state=push_button.value() #storing the value of the push button state in the variable logic_state
If-Else Statement MicroPython
In this step, we are using an if-else loop. This line states if the push button logic state is TRUE that means 1
if logic_state==True: #if state of button is logic 1
To control the output GPIO, we will use the value() function on the Pin object and pass 1 as an argument. This means that the output which is the LED has a logic state HIGH hence it will be ON.
led.value(1) #LED is ON
This means that the output which is the led has a logic state LOW hence the LED will be off.
else: led.value(0) #LED is OFF
In this whole if-else loop, if the logic state of the push button is high meaning the push button is pressed, the LED will be ON and if the logic state of the push button is low meaning the push button was released, then the LED will be OFF.
In this tutorial, we learned how to use GPIO pins as both inputs and outputs in the same project by controlling the on/off mechanism of an LED using a push button in Micro-python for Raspberry Pi Pico.