We’ll start by watching the button. Load button.c onto your Nucleo, or your own toggleLED.c if you finished the homework. We want to see the button’s signal and the signal of the LED your program toggles on the oscilloscope at the same time. Look at your breadboard. Before you connect the AD2, discuss at your table which point in your circuit each probe should touch, and where the minus leads connect, and why. Put the button on CH1 and that LED on CH2. Then check your plan against Figure 2.4.1 and wire the AD2.
Figure2.4.1.AD2 wiring for observing button bounce. CH1 (orange) touches the button’s signal row, CH2 (blue) touches the LED’s row, and the minus leads (white stripe) go to the GND rail.
In Waveforms, configure the trigger as shown in Figure 2.4.2: Source: Channel 1, Type: Edge, Condition: Falling, Level: 1 V. Set the time base to 20 µs/div. (Depending on your Waveforms version, you may need Normal or Auto trigger mode.)
Figure2.4.2.Waveforms trigger settings for capturing switch bounce. Source: Channel 1 (button signal), Condition: Falling edge, Level: 1 V. Time base 20 µs/div is a good starting point; adjust if your bounce is shorter or longer.
Figure2.4.3.CH1 (orange) shows the button and CH2 (blue) shows the LED. Switch bounce captured on CH1: a single button press produces many rapid HIGH/LOW transitions before the signal settles low. CH2 shows the LED toggling on each bounce, not just once. Bounce duration varies by switch, typically 50 µs to a few ms.
Now we’ll try the analog fix: a small capacitor across the switch. The circuit and the arithmetic behind it are written out in Subsection 2.5.1; here we’ll just fit the part and look at what it does to the trace.
In Lab 2’s Technical Study you learn how to select an appropriately sized capacitor for a given switch. For today the 0.1 µF or 0.15 µF capacitor works well.
Figure2.4.4.Reading metal film capacitor markings. The three fields encode value, tolerance, and maximum voltage. Value is two digits and a decimal point; the decimal point can be an n (nano), a p (pico), or a dot (micro). Example: .1J63 = 0.1 µF, ±5%, 63 V. The two photographs are what the part looks like in your kit: a small squared-off block with two legs, in blue or cream depending on the manufacturer, with the marking printed on the flat face.
Figure2.4.5.Place the debounce capacitor (0.1 µF or 0.15 µF, depending on how your kit was stocked) from D5 (PB4) to GND on the breadboard, in parallel with the button. The red dashed line shows where the capacitor connects. Identify the correct capacitor by reading its marking code (Task 1 below).
Place the capacitor across the button (one leg on the GPIO pin side, the other to GND). Capture the button waveform again with the same scope settings. How has it changed?
Look at channel 2 (LED) again. Does the LED now toggle exactly once per press? Save a screenshot showing both channels, with and without the capacitor, for your lab report.
CH1 (orange) shows the button and CH2 (blue) shows the LED. One press makes many rapid HIGH/LOW transitions on CH1 before settling, and the LED toggles on each bounce, not just once. Bounce lasts ~50 µs to a few ms, and varies by switch.
The analog fix is to connect a small capacitor across the switch, from D5 (PB4) to GND.
The voltage across a capacitor changes only as charge flows in or out, which takes time, so the pin’s voltage can no longer follow the contacts as they chatter.
The switch, the capacitor across it, and the internal pull-up that charges the capacitor while the switch is open.
Metal-film capacitor markings: value, tolerance, and max voltage. The decimal point can be an n (nano), p (pico), or a dot (micro). Example: .1J63 = 0.1 µF, ±5%, 63 V. In your kit it is the small squared-off block with two legs, blue or cream, with the marking on the flat face.
Place the debounce capacitor (0.1 µF or 0.15 µF, kits have either) across the button, from D5 (PB4) to GND, in parallel with the switch.
Figure2.4.7.Button waveform with vs. without the debounce capacitor.
Hardware, an RC filter or a digital timer: it does not tie up processor cycles. It costs another small component and its wiring, and it is hard to revise for a different switch, so it has to be designed with some tolerance.
Software, a delay: it is flexible, because if the switch changes you edit the code and reflash. It is simple, and it needs no extra components. It costs processor cycles, spent waiting for the bouncing to stop.
Today we’re using the hardware fix, the capacitor we just fitted. A software delay would have done the same job. In Lab 2’s Technical Study you’ll investigate this more.