In my previous tutorial "Getting started With Raspberry Pi Pico" on raspberry pi pico, I taught you step-by-step how to get started with raspberry pi pico, Which software or IDE is needed for programming the pico board and the most important part i.e. how you can install Micro Python on your pico. I also showed you how you can blink the on board and an external LED with your raspberry pi pico. Here in today's tutorial, I am going to teach you how you can interface buttons with your raspberry pi pico.
After understanding this tutorial carefully one will be able to:
- Interface buttons with raspberry pi pico and can control other devices with it.
- How to use the raspberry pi pico GPIO pins as inputs and how to read the state (High or Low)of any of GPIO pins.
- Understand why interrupts are necessary.
- Turn on and off the on-board or external LED with the button.
- Use the internal pull-up and pull-down resistors of raspberry pi pico
Hardware Requirements:
- Raspberry pi pico
- Bread board
- Push button
- Resistors(330 ohm/0.25W, 10K ohm/0.25W)
- jumper wires(M-to-M)
- LED(size = 5mm, color = Red)
Software Requirements:
Thonny(Python IDE for beginners)
Link is: https://thonny.org/
Theory:Button:
A button is a mechanical switch that is used to break or maintain the flow of current in a circuit. In button there are two terminal of metal. In normal state(when no pressure is applied on the button) these metal contacts do not touch each other. This state of the button is called 'OFF'. When we press the button, these terminal come in contact or simply touch each other. Now this state of button in which both terminal are in physical touch is called 'ON'. A button sometimes is also called as pushbutton because you have to push it to start the current flow. A button in a circuit is represented by given symbols.
Step-1: Make a simple test circuit to understand the operation of button practically
When You push the the button with your finger, both metal terminal come in contact with each other and current starts flowing and LED starts glowing. As soon as you release the button both metal terminal break the physical contact and the LED stops glowing. A 330 Ohm resistor is used in the circuit to limit the flowing current in the LED.
Step-2: Connect a button to Raspberry Pi Pico
Fix your raspberry pi pico board on a full size bread board and then also fix the button on the same bread board as Shown below.
Now connect the pin-13(GND) pin of the raspberry pi pico to the -ve(blue) rail of the bread board. Connect the left terminal of the button to the -ve rail of the bread board and right terminal to the Pin-19(GPIO-14) of your pico as shown in the following circuit.
Step-3: Write a micropython program to read the state of the button
Open a new script in thonny and type the given program and then save the script. In the first line of the program you have to import the machine module to use the GPIO pins and time module to create delays in the program. Now create a button object for pin class. Here you have to set the pin name. No. 14 indicates GPIO Pin-14 andPin.IN
is used to configure the pin mode as Input. For configuring the pin as output you should use Pin.OUT
instead of Pin.IN
.
Pin.PULL_UP
is used to activate the internal pull up resistor. You can also usePin.PULL_DOWN
to activate the internal pull down. Create an infinite loop to run again and again so that you can read the state of the button any time. For creating infinite loop use while True. Now you have to read the state of the button so for that you use button.value()
this function returns 1(True) if the button pin is high(3.3 V) or 0(False) if the button pin is low(0 V). You need to check that button is pressed or not for that you can use the if( control statement) and == (comparison operator). If this condition is true then you have to send the message "Button is pressed". Use print() function for displaying the message. If condition is not true then you have to print the message "Button is not pressed". In the last line you have to give some delay for each test so you have to use time.sleep()
function. Now your program is completed and looks as given below:
from machine import Pin
import time
button = Pin(14, Pin.IN, Pin.PULL_UP)
while True:
if button.value() == 0:
print("Button is Pressed")
else:
print("Button is not Pressed")
time.sleep(0.1)
Now
click on run to run your program. See the output in the shell and then press the button. As you press the button you will see "Button is pressed" message in the shell.
Step-4: Controlling an external LED using button.
In step 3 you have learned how to read the button state. In this step you have to control an external LED with push button and pico so you have to modify your circuit as shown below:
Add some more lines in your code that you have created in step 3. Now your code will look like as shown below:
from machine import Pin
import time
button = Pin(14, Pin.IN, Pin.PULL_UP)
led = Pin(15, Pin.OUT)
while True:
if button.value() == 0:
led.value(1)
else:
led.value(0)
time.sleep(0.1)
Click on run option. You will see that LED is not glowing, now press the button and as soon as you press the button the LED starts glowing and continuously glows until you do not release the button.
Step-5: Toggle the LED using the button:
The world toggle mean change the state. Here you have to toggle your LED with the push button. Write the program given below for toggling the LED with your button:
from machine import Pin
import time
button = Pin(14, Pin.IN, Pin.PULL_UP)
led = Pin(15, Pin.OUT)
while True:
if button.value() == 0:
led.toggle()
time.sleep(0.5)
Run the above program. You will see when you press the button the state of the LED changes i.e. if it was off then it turns on or if it was on then it turns off. A bad thing happens if You keep the button pressed the LED toggles after 0.5 seconds but here you have to toggle the LED only when it is pressed once and there is no matter you keep the button pressed, the led should not toggle. If You press the button more faster the led should toggle faster but this does not become possible due to use of delay. The solution of this problem is that you should use the Interrupts. I am not explaining interrupts here because this is the subject for an another tutorial.
Comments