Close

Capacitive Sensing Firmware and Circuit

A project log for Light Up Raccoons

A little coin cell powered raccoon with capacitive sensing to light up its eyes

savoSavo 02/01/2023 at 17:570 Comments

Once I had settled on the idea of the capacitive sensing circuit board art, I looked into how to achieve capacitive sensing ideally using the ATtiny85s I had laying around. After some research I stumbled upon the TinyTouchLib by cpldcpu (Tim) which accomplishes that exactly that.

To make use of it was pretty straightforward, I simply had to change the pin allocation in the library's files and use the following code in my main file. First to initialize the capacitive sensing system:

tinytouch_init();

Then as part of my main loop to see when the pad was touched (start of contact, not held) and then turn on the LED for a short period.

if (tinytouch_sense() == tt_push) {
  digitalWrite(eyesPin, HIGH);
  delay(eyesOnTime);
  digitalWrite(eyesPin, LOW);
} 

Without getting too into the weeds, the way the library works is that it charges the ADC's hold capacitor to the supply voltage, and then it connects to a pin. Once connected to a pin the charge on the hold capacitor is shared between itself and that pin based on the new, larger, capacitance. Once this is done, an ADC reading is taken and the changes in this ADC reading over time are interpreted as changes in capacitance that correspond to contact.

The test hardware was simple, just the ATtiny on a breadboard connected to an LED with current limiting resistor and a jumper used as the "pad" for contact. After some tweaking of constants related to the contact threshold, the code was finished. This was one of my few projects where the code was fully tested before I made the final system!


Discussions