AtTiny85 based capacitive sensor LED switch

Not so much time ago, I met a girl, and she designs ceramic lamps.

I mean, the lamps are really, really gorgeous, and they are illuminated by a 12V LED module.

As soon as I saw one of these lamps, I decided to make some experiments with them, because they are so beautiful that the classic ON/OFF switch did not do justice to them.

Recently, I studied the Arduino capsense library for other purposes, and I thought that a capacitive sensor would have been a very nice way to turn the lamp on and off without the use of a regular switch.

The basic idea was to paint the inside of the lamp with some conductive paint and use the lamp itself as a switch.

So, here’s what I did.

First of all, the prototyping. The best thing about AtTinys is that you can use Arduino for prototyping, and then, with very small code adjustments, burn the code into the AtTiny, make the circuit supersmall, and go to production.

I started from the basic Capacitive Sensor library example.

It worked nice, but the reading was not very stable, and since I linked the LED intensity directly to the sensor reading, the LED was flickering a lot (as you can see in this video)

So I followed the instructions, and put a very small capacitor (20pF) between the sensor (an aluminium foil for prototyping) and ground. That helped a lot, but it wasn’t enough.

Then I decided to add some smoothing code to my sketch.. Open source code is soooo cool 🙂 You always find someone that can really help you 🙂

The original code takes the average reading between 100 readings, but since the AtTiny is much slower than the Arduino (1Mhz vs 16 Mhz) I decided to make an average reading between 10 values, and it worked great for me.

Ok, now the LED turns on and off when my hand is close to the sensor. But I wanted it to stay on or off, like a regular switch does. So I put this code, that works in this way: if the sensor reaches the maximum value (when I touch it), then I switch the lamp on/off and I stop the reading for a second.

So this is the final code for Arduino.

#include <CapacitiveSensor.h>

int cap_pin_out = 4;
int cap_pin_in = 2;
int lowcap = 300;  // just above reading when noting is near
int highcap = 1800; // cap reading when almost touching
CapacitiveSensor   capsense = CapacitiveSensor(cap_pin_out, cap_pin_in);  // 10M resistor between pins 1 & 2, pin 2 is sensor pin, add a wire and or foil if desired
int ledPin = 13;
int dur = 10; //duration is 10 loops
int brightness;
bool isOn = 0;

/* smoothing */
const int numReadings = 10;
int readings[numReadings];      // the readings from the analog input
int index = 0;                  // the index of the current reading
long total = 0;                  // the running total
int average = 0;                // the average

void setup()
  pinMode(ledPin, OUTPUT); // output pin
  pinMode(cap_pin_in, INPUT); // output pin
  pinMode(cap_pin_out, OUTPUT); // output pin

  // initialize all the readings to 0:
  for (int thisReading = 0; thisReading = numReadings)
    // ...wrap around to the beginning:
    index = 0;

  // calculate the average:
  average = total / numReadings;
  // send it to the computer as ASCII digits

  long start = millis();

  brightness = map(average, lowcap, highcap, 0, 255);
  brightness = constrain(brightness, 0, 255);

  if (isOn == true && brightness == 255) {
    isOn = false;
  } else if (isOn == false && brightness == 255) {
    isOn = true;

  if (isOn == true) {
    analogWrite(ledPin, 255);
  } else {
    analogWrite(ledPin, brightness);



At this point I had a couple of things left to do: first of all, to control an hi-power 12V led, instead of a 5V one, and, second, try to power the circuit with the 12V LED power supply.

To control an 12V LED, I used a TIP102, that is a really heavy duty affordable NPN darlington transistor. It’s perfect to control LED strips, hi power LEDs and LED modules. The connection is really easy, it just needs an 1K resistor and this simple scheme

Since the ATTiny can be powered at 5V, and I did not want to use two power supplies, I decided to use this 12v to 5v voltage converter. It’s very simple to use: just connect the IN Pins to the 12V and you get 5V from the output pins (yes, I know, I should have used a capacitor for stability, but the AtTiny is very forgiving and works perfectly even without it)

Ok now that everything was perfect, I burned the code to the AtTiny. If you need some instructions on how to do that, here’s a step by step tutorial I wrote some time ago.

I had to make some minor adjustments to the code: I changed the sensor pins form 4,2 to 1,2 and the LED pin from 13 to 0… and boom! we’re ready to change platform! 🙂

So here’s the schematics for the AtTiny85

The circuit is pretty simple and can be easily assembled on a protoboard.

I painted the inside of the lamp with this paint, so now the inside of the skull is conductive and can be used as a capacitive sensor.

To connect the sensor wire to this paint, I used some BareConductive electric paint

The assembled circuit was so tiny that fitted perfectly inside the skull

A little hint: when using capacitive sensor, is very important to ground the circuit, so be sure to have a ground connection between the circuit, the power supply, and the mains ground.

Here is the final result… isn’t it nice? 🙂



Another important thing with capacitive sensors is grounding.

Leave a Reply

Sort by:   newest | oldest | most voted
Brian Moreau

Hi, realise you wrote this some time ago.
I only just started attempting to get AtTiny85 to work with CapSense.
Can you confirm what pins you used only your code defines pins 4 and 2 but the rem statement says 1 & 2
Your fritzing uses pins D1 and D2
I tried other pins but was not able to get them to work, I didn’t want to use D1 since my Digispark board has onboard LED on this pin.
Do you know if it works on other pins or if not why not?