Today, I will deal with switch bounce. Then I will toggle the switch to control LED (OFF/ON). Now let’s start!
|Table of contents|
1. Switch bounce
Switch bounce is a phenomenon that occurs when you use a physical contact switch such as a tact switch. When you push a switch, you may think it gets immediately ON. However, that’s not correct in the micro-scale world. When a switch is toggled, metal contacts of a switch come together and close. During this time, metal contacts are bouncing and that causes noise. This noise is usually big enough for your hardware to detect and react. Thus, switch bounce may cause malfunction.
When it comes to the setup introduced in the previous post (Utilize an internal pull-down resister), the tact switch can make switch bouncing. It can happen during positive edge, which is the moment when GPIO 24 turns from LOW to HIGH. As you can see in the diagram below, the switch bounce could cause malfunction of LED (ON => OFF => ON), even though you will intend to turn it on once. Therefore you may want to deal with this.
2. Bouncing time
To deal with the switch bounce of my setup, I set a “bouncing time”. Bouncing time should be set to deal with switch bouncing. Your system will not to detect wrong signal (bouncing )for certain time after it detects 1st positive edge (1st signal). By setting a bouncing time, you can avoid wrong detection. I set a bouncing time as 200 msec for my setup.
Here is the code.
In the line 16, GPIO.add_event_detect(24, GPIO.RISING, callback=my_callback, bouncetime=200), this is interpreted that when GPIO 24 detects a positive edge, my_callback is called and accordingly the bouce time is set for 200 msec.
In the line 18, you make sure that the initial status of LED is LOW (= off) when you run the code.
From the line 4, a function, “my_callback” is defined. Only GPIO 24 is monitored in this function (line 6).
The line 7 ensures that whenever “my_callback” is called, the status of LED is changed. Namely, whenrever you push the tact switch, you change the stsatus of LED.
The line 20 is an infinitive loop and keeps this program running. Without this, this program will finish soon after running.
import RPi.GPIO as GPIO
from time import sleep
ledState = not ledState
if ledState == GPIO.HIGH:
GPIO.setup(25, GPIO.OUT, initial=GPIO.LOW)
GPIO.setup(24, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) #enable an internal pull-down resister
GPIO.add_event_detect(24, GPIO.RISING, callback=my_callback, bouncetime=200)
ledState = GPIO.LOW
I successfully toggle the tact switch. In the previous post (Control LED by tact switch), I need to hold the switch down to keep LED ON. However, in this setup, I can toggle it by just pushing the switch as the movie below. One issue was that when I held down the switch for certain seconds, switching did not work as I expected. For example, OFF => ON (hold down for certain seconds) => OFF, I sometimes recognized this kind of behavior just because I held the switch down. I’m not sure if I need to adjust my code or this is because of the tact switch.