Touch Sensing
Since the mbed platform doesn't come with any buttons or sensors, it is difficult to create meaningful examples or experiments that use human input. Adding a physical button would be easy to do, but we wondered if we could do more with less. This page describes how one would use a small capacitor in conjunction with an ADC (AnalogIn) and a GPIO (DigitalIn) to create a touch sensitive button. The idea was adapted from a Microchip Appnote - Capacitive touch using only an ADC.
You need - a jumper cable and a small value capacitor (100pF works fine). Connect the jumper cable across pins P19 and P20 of the mbed. Connect the capacitor between P18 and P19 of the mbed as shown.
Principle: the touch sensitive "button" works by charging up the capacitor to the supply voltage and then testing the voltage using an ADC pin. To trigger the button, one must touch both leads of the capacitor. The pseudo-code below explains the process.
- Assign P20 as an AnalogIn, P19 as a DigitalIn and P18 as a DigitalOut (for conveniently located ground)
- Pull up P19 to charge capacitor to 3.3V
- Float P19 to retain charge on capacitor
- Immediately sense voltage on P20.
- If the user has touched both leads, the capacitor would have discharged through skin resistance to ground.
- If not, the charge on the capacitor will remain pretty much the same.
Other thoughts:
The original aim of this exercise was to perform touch sensing without any external capacitor. As the Microchip AppNote mentions, you should be able to store and share charge on the sample-and-hold capacitor internal to the ADC. The datasheet mentions that the maximum capacitance on the ADC inputs is 15pF. However, the ADC pins are all multiplexed with digital functionality as well. Does selecting the ADC functionality through ADxCR turn off the digital part and expose the sample and hold capacitor ? Or is the sample and hold capacitor only exposed when conversion is started ?
3 comments
You need to log in to post a comment
We can reuse the precharging port as analog input, by allocating and deallocating c++ classes dynamically.