Capacitive sensors with the Teensy 3.0

The Teensy 3 is not only an Arduino, it's an ARM too. And a neat built-in feature is the capacitance sensor available on 12 of io pins. I've always been a fan of capacitance for measuring things, so having a stable, fast and efficient way to do this with no external components is amazing.

This will probably make my water level sensor obsolete, if it has as low drift.

So let's see what we can do with it. The first step is to just see if we can get values out:

void setup() { Serial.begin(38400); } void loop() { Serial.println(touchRead(A9)); delay(100); }

Looking at the serial console I can see lots of 700s, which corresponds to 14pF, which is respectable given the protoboard.

Second step: make a simple slider:

The copper triangles are cut from a piece of flashing, the whole thing is hot-glued to a piece of PET packaging I had lying around.

The aluminium bar is connected to ground to see if I can improve the noise levels. It didn't help. What I probably would need to do is form multiple 'fingers' with alternating grounds. But that's not something I'm going to try and cut out of a piece of flashing.

The code is very similar, but this time we want to look at the relative capacitance between two pins:

void setup() { Serial.begin(38400); } void loop() { int a = touchRead(A9); int b = touchRead(A8); Serial.println((a-b)*1000/(a+b)); delay(100); }

Sliding my finger from one end to the other gives me:

-91, -89, -100, -109, -110, -107, -118, -123, -115, -109, -101, -96, -92, -80, -75, -60, -50, -38, -25, -15, -9, 8, 9, 22, 20, 37, 32, 42, 41, 52, 45, 48, 50, 49, 66, 71, 77, 84, 91, 99, 105, 111, 112, 123, 128, 127, 133, 132, 130, 127, 123, 125, 121, 112

Unfortunately I'm seeing a lot of noise when I don't earth myself.

To detect finger presence we could just threshold the total capacitance:

if (a+b > 2000) Serial.println((a-b)*1000/(a+b));

Though this requires coming up with a number (e.g. 2000 = 40pF) once you have assembled your system. There is a probably a clever way to do this by looking at the long term averages.