In this project, we will write a software sketch to identify and correct the effects of button bounce on the chipKIT™ microcontroller boards.
In this project, when the button is pressed the LED shines and the computer receives the number of times the button has been pressed.
For this project, we will use a resistor and a capacitor to debounce a circuit.
Create a more complex and sophisticated button circuit that will activate when pressed with the right amount of force.
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.
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 | ![]() |
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.
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):
Min resistance (max applied force):
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.
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.
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:
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 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).
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 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.
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);
}
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).
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: