Intel® Edison Hands-on Day 4: Touch Switch

Just using the button to control the light seems not cool enough. In this section we will try a touch sensor and a relay to control the LED. Let’s make it!

Components required

Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

Step 1: Connection

Capacitive Touch Sensor → Digital Pin 2

Relay Module → Digital Pin 12

Notice that
relay can be considered as a electrically controlled switch. You can even use it to control the electrical appliances with high voltage and high current in your home.

For the sake of safety, however, we would recommend NOT to hack in the high voltage stuffs at the first time.

Step 2: How to Use Relay?

Let’s talking about the relay.

You can use a digital pin to directly control the switch.

There is four out connections in this relay module, which is COM(Common), NO(Normally Open), NC(Normally Closed), N/A(Not applicable)

COM = Common, NC and NO always connect to this pin, it is the moving part of the switch.

NC = Normally Closed, COM is connected to this when the digital pin is LOW.

NO = Normally Open, COM is connected to this when the digital pin is HIGH.

Step 3: Coding

int touchPin = 2;                  //The pin of the touch sensor
int relayPin = 12; //The pin of the relay int relayState = HIGH; // relayState stores the state of relay int touchState; // touchState stores the state of touch sensor int lastTouchState = LOW; // lastTouchState stores the last state of touch sensor long lastDebounceTime = 0; long debounceDelay = 50; // debounce time void setup() { pinMode(touchPin, INPUT); pinMode(relayPin, OUTPUT); digitalWrite(relayPin, relayState); } void loop() { //read the touch sensor state into the reading variable. int reading = digitalRead(touchPin); // if the state of touch sensor changes, stamp the time. if (reading != lastTouchState) { lastDebounceTime = millis(); } // Wait 50ms to confirm the change really happens // If the new state of the touch sensor is HIGH, then change the state of the relay. if ((millis() - lastDebounceTime) > debounceDelay) { if (reading != touchState) { touchState = reading; if (touchState == HIGH) { relayState = !relayState; } } } digitalWrite(relayPin, relayState); // record the last state of the touch sensor lastTouchState = reading; }

After uploading the sketch, and you can touch the sensor to control the LED.

Step 4: Principle(Digital Input—Digital Output)

Apparently, touch sensor is the input device, while the relay is the output one. Meanwhile, the relay can control other devices.

Step 5: Code Review

Set the touch sensor to input and the relay

to output

pinMode(touchPin, INPUT);

pinMode(relayPin, OUTPUT);

use digitalWrite() to read the state of the touch sensor:

int reading = digitalRead(touchPin);

When you push the button, they may be bounce apart one or more times before making steady contact in a short time. Just like the illustration picture above.

Without debouncing, pressing the button once can appear to the code as multiple presses. Makes use of the millis() function to keep track of the time when the button is pressed.

if the state of touch sensor changes, stamp the time via millis() function:

if (reading != lastTouchState) {

lastDebounceTime = millis();


millis() returns the number of milliseconds since the Arduino board began running the current program.

Wait 50ms to confirm the change really happens. If it really happens, change the state of the relay. In

if ((millis() - lastDebounceTime) > debounceDelay) {

if (reading != touchState) {

touchState = reading;

if (touchState == HIGH) {

relayState = !relayState;




Be the First to Share


    • CNC Contest

      CNC Contest
    • Make it Move

      Make it Move
    • Teacher Contest

      Teacher Contest

    4 Discussions

    Great Instructable! I just built it as I received a developer kit from the intel sweeps. but i am new to arduino and would like to make the relay only on for a select amount of time or only whilst the button (capacitive touch) is activated. could you assist me in this. I assume it could be as easy as writing a time limit to the change state of the relay command. or possibly another check for cap pin state?

    2 replies

    Though you didnt comment back yet, I was able to work it out. Again, Thanks for your ible!!!


    Reply 4 years ago on Introduction

    Congrats. It's great to see you solve it yourself.

    Sorry just got the notification.

    Great work!


    4 years ago on Introduction

    Another great project! I love the simple nature of these tutorials you've posted. They look great for beginners.