Raspberry Pi Pico Digital input using Push button & Control Led
Table of Contents
Raspberry Pi Pico Digital Input:
Raspberry Pi Pico Digital input using Push button- In this article, I am going to explain how to use a Push button with the Raspberry Pi Pico. The purpose of this example is to help you understand, how to read a digital input on any GPIO pin of the Raspberry Pi Pico board. This is really an important example so make sure you don’t skip any information. If you learned how to read or detect the button click then you can read all the types of digital sensors, for example, PIR sensor, infrared obstacle sensor, Microwave Sensor, and so on. In simple words any sensor that gives you 1 or 0 as the output signal. So, in this example pushbutton is like a sensor that detects the user input.
Previous Articles:
Raspberry Pi Pico Pinout & Specs
Raspberry Pi Pico MicroPython and Thonny IDE Installation.
Raspberry Pi Pico Led examples.
Raspberry Pi Pico Oled Display Module SSD1306
Raspberry Pi Pico ADC Analog Sensor
Raspberry Pi Pico Temperature Sensor.
Raspberry Pi Pico Ultrasonic Sensor.
Raspberry Pi Pico PIR Motion Sensor.
Raspberry Pi Pico LDR Sensor, Day & Night Detection.
Amazon Links:
Other Tools and Components:
Super Starter kit for Beginners
PCB small portable drill machines
*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!
So, to make it more interesting I am going to control an LED, so, each time the button is pressed the LED will change its state. Let’s go ahead and take a look at the circuit diagram.
Raspberry Pi Pico Push Button Circuit:
One side of the Push button is connected with 3.3V pin of the Raspberry Pi Pico while the other side of the push button is connected with the digital input GP14 pin of the Pico board.
The LED connection with the Pico Board remains exactly the same. Now, let’s take a look at the programming.
Raspberry Pi Pico Push Button Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
from machine import Pin import time led = Pin(28, Pin.OUT) button = Pin(14, Pin.IN, Pin.PULL_DOWN) while True: if button.value(): led.toggle() time.sleep(0.5) |
The purpose of this program is to toggle the LED. As the LED is an output device so that’s why I set it as output “OUT” and as Push button is an input device so that’s why I set it as input “IN”. Reset of the code is pretty straightforward. If a button click is detected then simply toggle the LED means if the LED is OFF then turn it ON or if the LED is ON then turn it OFF. Now, let’s go ahead and run this code.
When Button is not pressed.
When the button is pressed.
You can see each time I press the button, the LED changes its state.
Next article, Raspberry Pi Pico SSD1306 Oled display Module interfacing, and programming.