Raspberry Pi of Goemon

Raspberry Pi3 learning blog

Beginner Electric work

How to deal with switch bounce

Post:04/10/2016 Update:

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
  2. Bounce time
  3. Code
  4. Result
  5. Reference
Sponsored Link

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.


3. Code

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.


4. Result

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.


5. Reference


Sponsored Link

Sponsored Link

-Beginner, Electric work

Related Posts


Utilize an internal pull-down resister

In the previous post, I checked how a pull-down resister functioned. When it’s there the system worked as expected. However once it’s taken off, then LED became unstable. Actually, Raspberry Pi 3 contains an internal pull-down resister and you can enable it by coding. Today, I’m gonna show you how you can enable an internal resister and how it works. Don’t worry, it’s very simple 🙂


AD converter: LED responsive to room brightness

If you use a toggle switch, it’s enough  that you just monitor the HIGH/LOW signal. But what about monitoring brightness/darkness? If you allocate “0” to the night as it’s dark and “1” to the noon as it’s bright, what would you allocate to the morning and evening? Maybe you have no solution because brightness/darkness is not digital value, which can be expressed 0/1, but analogue value. Analogue value is infinitely continuous. For example, there is 0.5 between 0 and 1, there is 0.25 between 0 and 0.5……. But if you want to handle those analogue values with computer, what do …


Blink LED on Raspberry Pi 3

Blink LED is a very first step for beginners. You can learn how to connect each electric component and control it with minimum numbers of components and a small chunk of code. Table of contents GPIO pin layout Hardware setup Code Result Reference The necessary parts and tools are here. The selection of resistor depends on LED’s Vf (forward voltage). You can usually find the value on its specification/catalog. Name Qty Resistor 1 330 Ω Red LED 1 Vf = 2.0 V Breadboard 1 Breadboard jumper wire (male-female) 2 Nippers 1 Sponsored Link 1. GPIO pin layout GPIO (General Purpose Input/Output) …


Install Raspbian on Raspberry Pi 3

The very first thing you have to do is to install OS on your Raspberry Pi. Today I install Raspbian on which various programs for learning are available. Table of contents Format SD card Download NOOBS Hardware setup Raspbian installation & setting Connect to Internet OS update What you need Micro SD card (larger than 8GB) *class 10 is recommended PC connected to Internet Mouse Keyboard Micro USB power supply PC monitor (HDMI or DVI-D) Monitor cable (monitor <=> Raspberry Pi 3) Sponsored Link 1. Format SD card If your SD card is new, you can skip this step. Mine …


How to use your tablet/smartphone as screen

Raspberry Pi is small and portable in itself. Also you can easily carry a charger for it. However, you are unlikely to carry a screen with you. It’ll be super convenient if you can use your smartphone/tablet as screen for Raspberry Pi. Today is about how to make your tablet/smartphone screen for Raspberry Pi. Please note that this method is possible only when both of your mobile devices and Raspberry Pi are connected to a same network. Table of contents Check Raspberry Pi IP address Set up SSH Install & set up VNC server on Raspberry Pi Install & set up …