Hardware and Timer Interrupts
This chapter covered the following concepts:
- There are tradeoffs between polling inputs and using interrupts.
- Different Arduinos have different interrupt capabilities. The Due can interrupt on any I/O pin, but other Arduinos have particular interrupt-enabled pins.
- Buttons can be debounced in hardware using an RC circuit and a Schmitt trigger.
- The Arduino can be made to respond to inputs asynchronously by attaching interrupt functions.
- You can install a third-party timer library to adder timer interrupt functionality to the Arduino.
- You can combine timer interrupts, hardware interrupts, and polling into one program to enable pseudo-simultaneous code execution.
- Arduino Uno
- A-B USB Cable
- Piezo Buzzer
- Common Cathode RGB LED
- 10kΩ Resistor
- 100Ω Resistor
- 150Ω Resistor
- 220Ω Resistors (x3)
- 10uF Electrolytic Capacitor
- 74HC14 Hex Inverting Schmitt Trigger
- Jumper Wires
- The text description of 12-3 on page 264 incorrectly says that the “resistor decreases the discharge time.” It should say that the “resistor increases the discharge time.”
- Figures 12-3 and 12-6 (the hardware debouncing schematics) don’t match up exactly with the wiring diagrams shown in figures 12-8 and 12-9. The original schematics show the 10k pullup resistor connected at the junction of the 100ohm resistor and capacitor. The wiring diagrams, on the other hand, show the 10k pullup resistor connected at the junction of the 100ohm resistor and the pushbutton. The circuit will work the same either way, but for consistency, you can find an updated version of the schematics that matches the wiring diagram and demo video here:
- On page 265, the explanation of hysteresis could be more clear. This is a better one:
Most microcontrollers are already designed to handle slowly rising or falling digital input voltages. When looking at the datasheet of the ATMega microcontroller, for example, you’ll find that the input pin low and high voltage thresholds are different. When an input signal is transitioning from high to low, it must drop below 0.2Vcc to register as a logic high. When an input signal is going from low to high, it must rise above 0.7Vcc to be registered as a logic high. Vcc represents the supply voltage of the chip (5V in our setup). This gap ensures that the value does not flutter during the transition step, and is called hysteresis. To observe this effect directly (before sending your signal to the microcontroller), you can utilize a Schmitt Trigger in your circuit.
Color Wiring Diagrams
Follow along with this video tutorial about external interrupts and hardware debouncing:
Watch a demo of the hardware-interrupted Arduino with button debouncing created in the chapter:
Watch a demo of the “Sound machine” created in the chapter:
All code is licensed via the GNU GPL v3. Code is maintained and updated on GitHub. The download zip linked above always contains the most recent version of the code examples that have been pushed to the GitHub Code Repository.
Taking it Further
Interrupts allow you build interesting new asynchronous projects. Here are some project suggestions that you can now complete:
- Build a reaction timer game that tests how quickly you can press a button when a light turns on.
- Build a cyclometer for your bike that determines speed by triggering a magnetic reed switch every time a magnet on your tire spoke spins by it.
- Build a feedback control system for a DC motor that uses a rotary encoder to determine motor speed.
- Integrate a blinking status LED into your existing projects that doesn’t interfere with the timing of the rest of your program.
References & Credits
- Figure 12-5: Schmitt Trigger Inverter Datasheet Pinout Credit: Courtesy of STMicroelectronics. Used with Permission, www.st.com (source)