Arduino touch sensor ky036

I received a few Arduino touch sensors from DealExtreme just because they were dirty cheap and I have a few ideas where I could use them as device power switches. There is probably an easier way to build a touch sensor for Arduino without that many additional components, but let’s see what we can do with this cheap sensor for just $2.6 or less than £1.7.

As for most things from DealExtreme (and many other cheap components sites originating from China) there is no documentation, but it turns out to be very easy to use this sensor. Just hook it up to an Arduino using the following diagram:

It has 4 pins:

  • A0 – inverted signal
  • G – should be connected to ground
  • + – should be connected to 5V power supply
  • D0 – touch signal

Touch signal consist of 50Hz signal pulse with 4.8ms pulse width on the D0 output pin for as long as finger (or any other body part) touches the bent pin on the transistor.

The A0 pin produces inverted signal.

That’s all there is to it. The pulse signal provides a small bit of complexity for the Arduino code, but is easily handled by adding a 50ms timeout of HIGH signal in the D0 pin connected to digital pin 3 on Arduino:

//setup default LED on pin 13
const int ledPin =  13;
//setup touch sensor on pin 3
const int touchPin = 3;

//store the time when last event happened
unsigned long lastEvent = 0;
//store the state of LED
boolean ledOn = false;

//setup pins
void setup() {
  pinMode(ledPin, OUTPUT);
  pinMode(touchPin, INPUT);
}

void loop(){
  //read touch sensor state
  int touchState = digitalRead(touchPin);

  //only interested in the HIGH signal
  if (touchState == HIGH) {
    //if 50ms have passed since last HIGH pulse, it means that the
    //touch sensor has been touched, released and touched again
    if (millis() - lastEvent > 50) {
      //toggle LED and set the output
      ledOn = !ledOn;
      digitalWrite(ledPin, ledOn ? HIGH : LOW);
    }

    //remember when last event happened
    lastEvent = millis();
  }
}

Touching the touch sensor will light up the LED, but will not do anything as long as user keeps touching the sensor. Releasing the sensor and touching it again will turn off the LED and so on.

P.S. Loving my Rigol DS1052E digital oscilloscope. It’s a real eye opener to electronic signal debugging. Fantastic piece of kit.

If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed.

Leave a Reply

Your email address will not be published. Required fields are marked *