AtTiny85 based capacitive sensor LED switch

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

viondesign.com

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;
    delay(500);
  } else if (isOn == false && brightness == 255) {
    isOn = true;
    delay(500);
  }

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

  delay(10);

}

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? 🙂

Ciao!

Jeko

Another important thing with capacitive sensors is grounding.

The Creepy Doll

Here at Mikamai, we often organise events and hackathons. After the last hackathon, someone left an old doll, and it was kinda creepy… so why not make it even creepier?

I decided to put two red LEDs instead of the eyes, and a vibration sensor to turn on the LEDs when you shake the doll. Everything is powered by an AtTiny85 and a single CR2032 battery.

Materials and tools

image

Materials:

image

Tools:

  • A sharp cutter
  • Hot glue gun
  • A soldering iron and solder
  • Needle and thread, to sew it back
  • optional – a Dremel to drill the eyes

Prototyping and testing the circuit

image

Using the AtTiny 85 is a great choice for those who are familiar with Arduino, because you have the possibility to use the Arduino IDE to program it. This means that you can prototype the circuit with Arduino before deploying it to the AtTiny.

So I wrote this code, and I built a simple testing circuit with Arduino.

#import <Arduino.h>


int led = 0; // LEDs pin
int button = 2; // Tilt sensor pin
int brightness = 0;    // how bright the LED is
int fadeAmount = 5;    // how many points to fade the LED by
int storedVal = 0;     // used to save the tilt sensor state

void setup() {
  pinMode(button, INPUT_PULLUP); // initialize the button pin a pullup input, so I don't have to use an external pullup resistor.
  pinMode(led, OUTPUT); // initialize the digital pin as an output.
}


void loop() {

  int sensorVal = digitalRead(2); // Read the sensor state

  if (sensorVal != storedVal) { //if the sensor value has changed, blink the eyes
    storedVal = sensorVal; // store the sensor state
    fadeEyes(); // call the eyes led fade function
  } else {
    digitalWrite(led, LOW); // otherwise, turn the led off
  }

  delay(10); // a small delay for debouncing the sensor
}

void fadeEyes() {

  for (int i = 0; i < 768; i++) { //cycle 3 times

    analogWrite(led, brightness); // set the brightness of led pin:
    if (brightness == 255) { // at maximum brightness, wait 5 seconds
      delay(5000); 
    }
    // change the brightness for next time through the loop:
    brightness = brightness + fadeAmount;

    // reverse the direction of the fading at the ends of the fade:
    if (brightness == 0 || brightness == 255) {
      fadeAmount = -fadeAmount;
    }

    // wait for 30 milliseconds to see the dimming effect
    delay(100);

  }

  digitalWrite(led, LOW);
}   

The code is pretty simple: it waits for a changement in the tilt sensor state, and when it happens, it starts a little loop fading the leds brightness

When the code works on Arduino, you are ready to deploy it on your AtTiny85

Moving to AtTiny85

Programming an AtTiny with an Arduno can be tricky, but fear not! I made a very simple tutorial on how to do that with the latest Arduino IDE and Arduino Uno/diecimila or Arduino Leonardo/Yun. Just follow these steps, and you can easily use this sketch on the AtTiny85

Testing the AtTiny85 based circuit

image

image

Now we can move the programmed AtTiny on a new breadboard, following this scheme

There is no resistor on the LED because I’m using a 3.3V coin battery. The circuit should start working as soon as you plug the battry: when the tilt sensor is shook, the LED fades 🙂

Making the circuit

image

Now that everything is tested, we can make the circuit. I decided to use a stripboard because even if it’s a little big, the result will be more stable and short circuit proof. Start placing the components on the stripboard and soldering them, the circuit is supersimple so this should be a very easy task.

I’ve put the LEDs in parallel, I know that I should have used a small resistor, but I was more focused on the simplicity of the project than on its life expectation, plus, it will be powered by a 3.3V battery, so I didn’t really care about it 🙂

The only precaution was to put some tape on the led wires to avoid short circuits.

Now you can test the circuit alone. If everything is fine, you can move to the next step

The doll surgery

image

With the cutter, remove some stitches from the doll and remove the head’s stuffing. Than drill the holes on the eyes. I used a Dremel for this task, but you can even use the cutter. Now insert the circuit, starting from the eyes. When the led pupils are in the holes, pour a lot of hot glue from the inside of the head, so you will be sure that the LEDs will stay at their place. Then put some more stuffing, the circuit, some more stuffing, the tilt sensor and the rest of the stuffing. In this way you are sure that everything won’t accidentally move and you will reduce the risk of short circuits to the minimum.

I suggest to use some velcro or a couple of buttons to close back the doll, so you can change the battery whanever you want

You’re done!

image