If you have seen previous tutorial, you can see push button only works when it is pressed. When it is released it will off. So we want, when button is pressed, it should turn ON LED and remain ON until I pressed button again to turn it OFF.
There is another problem with this, that is Button Debounce. When we are pressing a button, it connects for a few millisecond (give signal it is connected) and bounce back for few millisecond (give signal it is disconnected). These thing happen few times vice versa until push button get finally connected. It happens very quickly we can’t even notice, but our micro-controller notice this and do some task as soon as it gets connected signal but it do another task as soon as it gets disconnected signal.
So we will solve this problem with our python script, which will run smoothly in our raspberry pi 4. This is how you will debounce push button in raspberry pi 4 and you don’t need to hold button to keep LED light ON.
For doing this project you will need,
- Raspberry pi 4,
- Dual Fan Heat Sink aluminium Armor Case. (Optional)
- Push Button,
- 220 ohms Resistor,
- Jumper Wire.
Circuit for Debounce push button with Raspberry Pi 4:
Do connection as shown in diagram.
Python script for debounce push button with raspberry pi 4:
from gpiozero import LED,Button from time import sleep led = LED(25) button = Button(2) previous_state = 1 current_state = 0 while True: if button.is_pressed: if previous_state != current_state: led.on() current_state = 1 print("LED is ON!!") sleep(0.15) else: led.off() current_state = 0 print("LED is Off!!") sleep(0.15)
This code it self explanatory. But I will explain it briefly.
First we will import LED and Button from gpiozero library. As we are going to work with these two.
from gpiozero import LED, Button
Now we are importing sleep from time. We are importing this because whenever we want to pause or hold process for particular amount of time, we can use this.
from time import sleep
Here we are creating LED object called led that refer to GPIO 25 and Button object called button that refer to GPIO 2.
led = LED(25) button = Button(2)
Here we are creating a variable which will track state of button.
previous_state = 1 current_state = 0
Our script will run forever in loop
This will check if button is pressed, if it is pressed it will ahead and run further code, if it is not pressed it will do nothing.
This line will check condition if previous state is not equal to current state.
if previous_state != current_state:
if previous state is not equal to current state. It will turn ON LED and make current state equal to 1.
led.on() current_state = 1 print("LED is ON!!") sleep(0.15)
Here I am taking 0.15 seconds sleep time. If this is not working properly for you can increase this time. For example sleep(0.30)
if previous state is equal to current state. It will turn OFF LED and make current state equal to 0.
else: led.off() current_state = 0 print("LED is Off!!") sleep(0.15)
make sure you set same time of sleep in both the condition.
How to upload the script:
Open thonny python IDE. Write your code. Click save. Give proper name to the file and location. Click on Run.
So finally we have debounce the push button with raspberry pi 4.
Till then keep learning keep making.