Debouncing Circuits with Microcontrollers

Debouncing via Software

Correcting Bounce with Software

In this project, we will write a software sketch to identify and correct the effects of button bounce on the chipKIT™ microcontroller boards.

5.36K
×
Debouncing via Bounce Library

Using Libraries to Debounce Button Circuits

In this project, when the button is pressed the LED shines and the computer receives the number of times the button has been pressed.

6.19K
×
Debouncing via RC Filter

Using Resistors and Capacitors to Debounce

For this project, we will use a resistor and a capacitor to debounce a circuit.

8.06K
×
Using Force Buttons

Create a more complex and sophisticated button circuit that will activate when pressed with the right amount of force.

5.30K
×

Using Force Buttons

Using Force Buttons

Introduction

In one of the previous projects “Button-Controlled LEDs,” you created a simple circuit/sketch that would turn a LED on and off according to input received from a pushbutton. Now we will be extending that project by creating a more complex and sophisticated button circuit that will activate when pressed with the right amount of force.

The circuit for this project will consist of two sub-circuits. The first section will be the force sensor, it will utilize a new component called a force-sensitive resistor, which unlike normal resistors, this resistor will change value according to the amount of force applied to it. The force sensor section will output an analog signal that varies in voltage according to applied force (from 0V to 5V).

The second sub-circuit will be a threshold comparator. If the voltage from the force sensor exceeds a set threshold level, the comparator will turn on. This will be used to change the variable voltage analog signal from the force sensor, into a strictly on/off digital signal for the chipKIT board to read. The comparator sub-circuit will also introduce another new component called an operational amplifier.

Inventory:

Qty Description Typical Image Schematic Symbol Breadboard Image
1 220 Ω resistor
3 10 kΩ resistors
1 LED
1 FSR402 Force-sensitive resistor

(range 1 kΩ to 100 kΩ)

1 TCA0372 Operational Amplifier

Force Sensor:

Force-Sensitive Resistor

Like mentioned previously the force sensor section of this project will utilize a component called a force sensitive resistor. Figure 1 shows the FSR402 resistor we will be using for this project

While this component may not look like the fixed ceramic resistors you are familiar with from previous projects, it will act very similar with one exception. With this type of resistor, applying force (i.e., pressing down) on the circular disk will decrease the components resistance value (the disk is the only sensitive portion of the resistor, so bending the clear plastic lead does not effect on the overall resistance value).

The FSR402 resistor will range in value 100 kΩ (when no force is applied), to as low as 1 kΩ if you were to press down on the disk firmly.

Figure 1. Force-sensitive resistor.

To build the force sensor, we need a way to convert the change of resistance in the force-sensitive resistor(referred to as an FSR for the rest of this project), to a variable voltage signal for our comparator. To do this we will use a common resistor configuration known as a voltage divider (depicted in Fig. 2). If you are unfamiliar with voltage dividers, the link to the right provides more information.

Since we already know the FSR's range of values, to determine the range of output voltages we only have to consider the maximum and minimum resistance values. Using the general form equation for a voltage divider, the maximum and minimum output voltages are as follows:

Max resistance (no force):

$\Large 5V* \frac{R_{f}}{R_{1} + R_{f}} = 5V * \frac{100k\Omega}{100k\Omega + 10k\Omega} = 4.5V$

Min resistance (max applied force):

$\Large 5V* \frac{Rf}{R_{1} + R_{f}} = 5V * \frac{1k\Omega}{1k\Omega + 10k\Omega} = .45V$

You can see from the equations that when no force is applied to the resistor, the output voltage is very close to the source voltage. So, as you apply force, the voltage output will drop. This will create a usable signal for the comparator portion of the circuit.

Figure 2. Force sensor.

Comparator

For next section of the circuit we will utilize a component called an operational amplifier to create a special type of comparator called a “Schmitt trigger.” Normal comparators will trigger if an input signal rises above a given threshold level. Schmitt triggers differ slightly from this in that they have two separate thresholds (both an upper and lower threshold). Schmitt triggers will activate if the input signal rises above the upper threshold, and only deactivate if the input signal falls below the lower threshold (this is due to a property called hysteresis). The reference material to the right shows an example of both a single and dual threshold comparator (and the overall advantages of the dual threshold system.

Operational Amp:

Figure 3. TCA0372 Opamp.

Like its namesake, an opamp is an integrated circuit (IC) component that is designed to perform a mathematical operation on a signal. If you are unfamiliar with integrated circuits, they are simply self-contained circuits, comprised of many sub-components that are built from a single piece of semiconductor material. Microchips in computers and digital electronics are all examples of ICs.

Many times when designing electrical circuits, input signals have to be processed first before we can do anything useful with them (similarly, sometimes we have to process output signals as well). A great example of this is a microphone. Like most sensors, microphones produce signals that have incredibly low voltages (in the micro to millivolt range). Before the signal can be used, it has to be amplified to a level recognizable by other components in the circuit (like the analog input of our chipKIT board). This task is commonly done with opamps, where the amp scales the input signal (basically multiplying the signal by a constant value) to a point where it is usable.

Some of the common tasks performed by opamps are:

  • Signal scaling: Either increase or decrease the gain of a signal (multiplying by a constant).
  • Comparison (inequalities).
  • Taking the difference between two signals (subtraction).
  • Summing of two signals (addition).
  • Differentiating a signal (finding the signals rate of change).
  • Integrating a signal (finding the area of a signal, i.e. if you were to graph a signal over time, integration would find the area of the shape formed in that graph).

The reference material to the right discusses the general model of an opamp, and the components functionality. Op amps are almost never used by themselves, but are configured to accept feedback loops. With different types of feedback loops, all of the above mentioned tasks can be accomplished. The reference material explains the basic theory behind feedback, and how it be constructively used.

Figure 4. Schmitt trigger threshold comparator.

Figure 4 shows the circuit diagram of our Schmitt trigger circuit. The op amp in this circuit is configured to accept a positive feedback loop, which is what leads to the dual threshold property (a detailed explanation of feedback, hysteresis, and thresholds can be found in the related material).

Setting up the circuit:

Now with the theory properly discussed we can start constructing our circuit. Notice from figure 5 that, the TCA0372 is a dual op amp (meaning that there are two amps within the single IC package). This project we will only need one of the two amps. (For multiple amp ICs like this it is very common for Vcc, and Vee pins to be shared between all amps). Take a few seconds to look over the pin out of the op amp IC in Fig. 5.

Figure 5. Top view of a TCA0372 op amp (with pinout).

Figure 6 shows a circuit schematic representation of our final Schmitt trigger circuit. You probably noticed that $V_{ee}$ is tied to ground instead of a negative voltage. This is done because chipKIT boards do not have negative voltage supplies. This just limits output bounds of the comparator to between 0V and 5V.

It is also important to point out a non-ideal quality of opamp saturation. When the output signal saturates, it is very close to the rail voltage but not exactly (with a 5V rail, the output signal would be around 4.5V).

The threshold levels of the Schmitt trigger are dependent on attenuation in the feedback path and the saturation voltage.

$\large Feedback \ Attenuation * Positive \ Saturation \ Voltage = Upper \ Threshold$

$\large Feedback \ Attenuation * Negative \ Saturation \ Voltage = Lower \ Threshold$

With that being said, you would think that the thresholds with +5V Vcc and 0V Vee, would be 2.5V and 0V. In practice it tends to be around 2.25V and .5V due to the non-ideal quality of saturation.

Figure 6. Overall circuit schematic.
Figure 7. Overall circuit.
  1. Connect the GND pin of the chipKIT board to a bus strip; this bus strip will be referred as the ground bus strip.
  2. Connect the 5V pin of the chipKIT board to a bus strip next to the ground bus strip. This strip will be referred to as the 5V bus strip.
  3. Place the TCA0372 op amp into the breadboard so that the IC spans the valley between columns in the breadboard, (this allows each pin of the chip to occupy its own row). Make sure to match the notch in the IC with how it is shown in Fig. 16 (it is very common to place this component upside down, so pay particular attention to its orientation).
  4. Place one end of a 10 kΩ resistor into a single row in the breadboard. This row will now be called Node A. Then connect the other end of the resistor to the row that pin 1 (output) of the opamp occupies (the pin out and associated numbering of the opamp is shown in Fig. 14). Now with a small wire connect pin 7 (non-inverting input) of the opamp to Node A.
  5. Place a 10 kΩ resistors so that one end of the resistor is connected to Node A. Then connect the other end of the resistor to the ground bus strip.
  6. Place one end of a 10 kΩ resistor into a row in the breadboard (this row will now be referred to as Node B). Now connect the other end of the resistor to the 5V bus strip.
  7. Place the FS resistor into the breadboard so that one of the terminals is connected to Node B. Then connect the other terminal to the Ground bus strip.
  8. Connect pin 8 (inverting input) of the op amp to Node B.
  9. Connect pin 1 (output) of the opamp to pin 6 of the chipKIT board.
  10. Place an LED into the breadboard so that both legs of the LED occupied separate rows.
  11. Connect the Anode (the long leg) of the LED to pin 7 of the chipKIT board.
  12. With a 220 Ω resistor, connect one end of the resistor to the cathode of the LED, and then connect the other end of the resistor to the ground bus strip.
  13. Connect pin 2 (Vcc) of the op amp to the 5V bus strip.
  14. Connect pin 4(Vee) of the op amp to the ground bus strip.

Software:

Software for this sketch will be very simple. The chipKIT board will poll pin 1 of the op amp every 5 milliseconds to look for a change in voltage output from the Schmitt trigger comparator. The chipKIT board will then turn the LED on whenever the Schmitt trigger output is asserted HIGH. (This is similar to the software sketch for the “Button-Controlled LED” project).

                  const int btnGlb = 6; const int ledGlb = 7;
                  
                  void setup() {
                      // Set pin modes for Force button and LED
                      pinMode(btnGlb, INPUT); pinMode(ledGlb, OUTPUT);
                  
                  }
                  
                  void loop() {
                      
                    // Poll button
                    int btnState = digitalRead(btnGlb);
                    
                  
                    if (btnState == HIGH){
                     digitalWrite(ledGlb,HIGH);
                    } else{
                      digitalWrite(ledGlb, LOW);
                      
                    }
                    
                    delay(5);
                    
                  }
                

Test Your Knowledge!

Once the sketch is uploaded, pressing the FSR should easily light up the LED. After going through the project, you should be able to modify and tweak aspects of the circuit on your own. Particularly:

  • Change the sensitivity of the Schmitt trigger (i.e., change the threshold levels). In the circuit for the project, the fed back signal was attenuated by 50%, which caused an upper threshold of ~2.25V and lower threshold of ~.5V. Changing the amount of signal being fed back, directly affects the threshold levels. The R1 and R2 resistors in Fig. 15 are just another voltage divider attenuating the output signal being fed back. See what will happen to the sensitivity of the force button if you increase R2's resistance or replace R1 with a smaller resistor.

    (When resistors are connected in series their values add, so you could potentially create an equivalent resistor value of 30k by connecting three 10k resistors in series).

  • Invert the output (in hardware and software), you should be able to invert the output to the LED in software quite easily (the LED will turn on when no force is applied, and turn off when force is applied) by just changing a few lines of code. Can you do the same thing in hardware? Remember the voltage divider equation for the force sensor (Rfs/ Rfs + 10k). (Inverting the input to the comparator will also invert its output). How could you rearrange the FS resistor so that the LED output inverts?

Applying your knowledge further: High gain switch

Can you recreate this circuit using negative feedback instead of positive feedback? Remember output of an op amp can never exceed the rail voltage. So if an opamp circuit had an incredibly high gain, it could potentially drive the output into saturation whenever it received the slightest amount of input (looking very close to a digital on/off signal)

Below is the op amp in a “non-inverting” configuration.

The equation that governs the closed loop gain for this configuration is:

$\Large(\frac{R_{2}}{R_{1}} + 1)Input = Output$

Can you substitute this configuration for the Schmitt trigger configuration? Some key points to consider:

  • Multiple ground symbols on a schematic do not indicate multiple grounds. It is just a short hand notation to make schematics easier to read. (In the context of the project , all ground symbols would be connected the Ground bus strip).
  • Using 10 kΩ and 220 Ω resistors you should be able to get a very high gain with this configuration. One of each type of resistor should give you a gain of 46 (i.e., the output signal is 46 times larger than the input signal).
  • You will have to invert the input into the opamp circuit (if you don’t the output will remain HIGH constantly). Again remember the voltage divider equation for the force sensor. How does changing the placement of the FSR effect the equation?

Summary:

Force button schematic PDF.

Core Concepts:
  • Force-sensitive resistors
  • Operational amplifiers
  • Voltage division
  • Comparators
  • Schmitt trigger
  • Op amp applications and theory
  • Positive feedback
  • Negative feedback
  • Saturation
  • Open loop gain
  • Closed loop gain
  • Negative voltage

  • Other product and company names mentioned herein are trademarks or trade names of their respective companies. © 2014 Digilent Inc. All rights reserved.
  • Circuit and breadboard images were created using Fritzing.