Capacitive Touch Sensor: Learn Electronics with a Spooky Halloween Project

Uploaded by nerdkits on 15.10.2009

Hi. There are two reasons why Halloween is considered a favorite holiday here at NerdKits.
First, it's the only time of the year where it's considered socially acceptable to wear
a cape. And second, it's the only time of the year where our nerdy contraptions are
considered a sought-after trick. Since Halloween projects often involve springing unexpected
trap on the user, many late October projects often use some sort of proximity sensor to
detect the presence of a victim. Many commercial products out there, as well as a lot of DIY
projects, end up using a complicated distance sensor, sometimes an IR sensor or an LED photodiode
pair. In this project, we are going to show you
how you can use a NerdKit, some tin foil, and a little bit of electronics knowledge
to create a proximity sensor. We will then apply that knowledge to this spooky candy
bowl that adds an element of trick when kids reach inside for their treat.
The technology behind our proximity sensor is not at all new. What we've essentially
built is a capacitive touch sensor, much the same as the capacitive touch sensor you'll
find on your computer's track pad or on your smart phone. These devices rely on the fact
that humans are mostly water, and water is very good at directing electric fields. When
your body gets near it, it changes the direction of the electric field, and therefore changes
the capacitance enough to be detected. Track pads use fancy tricks to figure out where
on the pad are you touching, but we are going to use the same technology to figure out the
presence of a human hand. Let's go over the basics of the system. Say
we had a simple RC circuit just like this one with a resistor and a capacitor in parallel
and a switch going up to 5 volts. Let's look at what happen to this voltage at this node
when I open and close the switch. When I close the switch, its going to charge the capacitor
up to 5 volts, and when I open the switch, its going to let the capacitor discharge through
this resistor over time. Now how long it takes to discharge has to do with this resistance
and this capacitance, it's actually the product of them, it's the RC time constant of the
system. So if I was to keep opening and closing this switch over and over, it would charge
the capacitor and then let it discharge, and the time it takes to discharge would be exactly
the same each and every time as long as I don't change this resistance or this capacitance.
In our system we use the microcontroller to charge and discharge the capacitor and to
time how long it takes to discharge. But the capacitor is actually made up of two sheets
of aluminum foil. When you move your hand between the sheets of aluminum foil, it changes
the capacitance enough that you can tell by how long it takes for the voltage to drop
because it takes longer for the capacitance to discharge.
So let's see how we do this on our NerdKit. We're using component of the ATmega168 that
we have not use before, the analog comparator. The detailed functionality is explained in
the data sheet, but what it basically does is to compare two analog voltages and output
a 1 or a 0, depending on which one is higher. You can feed these voltages in to 2 specific
pins on the MCU, but the analog comparator also allows you to assign one of them to a
reference voltage known as the bandgap voltage. This voltage happens to be about 1.2 volts.
We take the other pin of the analog comparator and connect our resistor and aluminum foil
contraption to it. We set up the analog comparator to fire an interrupt whenever the voltage
at this node becomes less than the reference bandgap voltage. In the interrupt handler,
we record the current count of a timer. Then we change this pin to an output pin and set
it to a high voltage, and then, turn the pin to an input pin so that we are not driving
it with the MCU at all. This has the same effect as disconnecting the switch. The capacitor
will then begin to discharge until the voltage becomes lower than the reference voltage and
the interrupt fires again. Using the timer, we can see the difference on how long it takes
to discharge the node and be able to tell when a hand is near our sensor.
Let's go over this oscilloscope and see our circuit in action. Here we'll see the voltage
between the two pieces of aluminum foil, the voltage has held high by the MCU and then
was allowed to drop as the capacitor discharges. When it drops low enough, the interrupt fires,
and the cycle begins again. Now see what happens when I move my hand near the sensor. Notice
how it takes longer for the capacitor to discharge because of the increased capacitance.
There are many ways you could use this idea but we choose to make a glowing candy bowl
that would glow when someone reaches their hand in. The physical construction is very
straightforward: we taped one piece of aluminum foil behind the face of the Jack-o-Lantern
and put another piece underneath it. Then we just drilled a few holes for the LEDs.
All ten LEDs are driven by a single PWM output, so we can make them brighter the further you
reach your hand in. We hooked the LEDs up in pairs: 2 LEDs and a 680 ohm resistor in
series are driven with a 2N7000 transistor. The PWM pin controls the gate of the MOSFET
and turns the LEDs on and off.
Let's take a look at the harder parts of the code. We have an interrupt handler in order
to time the discharge of the capacitor through a resistor. We will later set the interrupt
to fire when the analog comparator crosses the bandgap voltage. The interrupt just records
the current time on the timer, changes the pin to a high output pin to charge the pin,
and then at the end, resets the timer before setting the pin back to an input pin so that
the next time they interrupt fires, it can record the time since the voltage started
to drop. This part of the code averages the times over 1000 samples. Things are happening
fast enough on the system that we can afford to average over many samples and things still
seem instantaneous. Averaging over many samples helps us reduce noise. Once enough samples
have been averaged, the interrupt handler flips a flag, and alerts the main loop that
a new averaged reading is ready for use.
All that's left to do is set it out and watch the reaction. I hope you have learned something
about how you can take a simple electronics concept and turn it into something you can
use to scare the neighborhood children.
For more information about this project, or more videos like this one, visit us at