This is the first of what hopefully will be several posts that are focused on electronics projects. I have been developing an interest in electronics, and Arudino in particular, along side the model trains.
As described in a recent post about building an odometer for the train, when including a switch care must be taken to avoid "switch bounce" or the mechanical rapid bouncing as the switch is triggered or released. If the program is fast enough, these bounces will be seen as separate events when you are expecting it to be a single event. This can be addressed either in the software or through circuit design. Since I wanted to use an interrupt for the trigger and am not sure of how fast the railcar might go, I decided to use the circuit approach.
I had three circuit design alternatives and wanted to test them out for their effectiveness.
Option #1: Simplest (and winner)
In the Garden Railroad story that inspired the odometer, he uses a 10 nF capacitor in parallel with the reed switch.
Option #2: Standard
This was a standard RC (resistor-capacitor) circuit. The circuit is a simple low-pass filter (effectively filtering out the high-frequency bouncing back and forth). I first saw this in a book
Arduino Workshop by John Boxall. The above link to
switch bounce has a good description of how this works. I used R = 10 KΩ, C = 100 nF.
Option #1: Most complex
I saw this one discussed on
a forum post about debouncing switches. It was actually the first description I saw for a hardware solution. It uses an RC circuit like option #2 and adds a diode and resistor is parallel. The diode prevents current from flowing backwards but I'm not sure why that is an issue. RC circuit has R = 10 KΩ, C = 100 nF. Second resistor is 470 Ω.
Schematic
Test Rig
I built all three circuits on the same breadboard and used push button switches to test with. The simple program counts how many times each switch is pressed and prints it out. It is easy to compare against how many times I know I pressed each one. Option #3, the most complex, actually didn't do that well counting; it reported many more events than presses of the switch. The other two matched the number of times I pressed the button. So I decided to go with the simplest one, Option #1 (why make things hard?). I'll be using the approach for my future circuits too.
Comments
Post a Comment