Background
A capacitive sensor gives us a good excuse to explore hardware interrupts. Interrupt-programming is so essential to programming microcontrollers that the MCU vendors dedicate silicon on the chip to use hardware interrupts.
A capacitive sensor is like a pushbutton but better π.
A capacitive sensor works by counting how long it takes to charge and discharge a capacitor and then comparing them to the equivalent charge/discharge time when a human is in proximity.
Hardware Connections
Making the DIY capacitive sensor.
To build the sensor, weβll construct a conductive surface separated by a ground plane via an insulator. Take 2 pieces of aluminium foil (the conductor) and sandwich them together, separating them by a piece of paper (the insulator).
- Cut out aluminium foil 10cmx10cm. This is the ground plane.
- Clip an alligator clip to this
- Layer a paper on top
- Cut out another piece of aluminium foil 5cm x 5cm.
- Attach an alligator clip to the top foil
- Have a region where the bottom and top foils overlap. This is where youβll be touching (and discharging) the capacitor.
The hardware connections simply consist of connecting the top conductive plate to PC1 in parallel with a 100k.
The Software
This project consists of a single main.c.
charge_cycle_count() tells you how many times the ISR fired. If it surpassed a specific threshold, do not illuminate the LEDs. Itβs the opposite, kinda strange.
charge_cycle_count is a global since itβs used in the ISR and main(). Making it a global means we donβt have to pass it in as an argument. Also, itβs volatile. This tells the compiler to not optimize it away. Notice how charge_cycle_count is initialized in main() but not used. If itβs not assignedvolatile, the compiler will optimize it away.
The Showcase
I was able to receive different values for a tap, light and aggressive press. This is like a normal pushbutton but better!
