Make a touch-sensitive lamp

Touch the metal pin on the touch module to switch a lamp on and off, like a touch lamp at home.
  • Level:Beginner
  • Duration: 30 min
  • Ages 10+
  • Platform: Arduino

Tools for this lesson

Helpful for this build. Open a tool to see tips and more projects that use it.

Make a touch-sensitive lamp – EEMENTOR STEM™ lesson

Touch lamps switch with a tap of your finger. The metal touch module from the Sensor Kit notices when you touch its metal pin, and the UNO uses that to switch an LED on and off with every touch.

What you'll learn

  • How a touch sensor responds to your body
  • How to toggle an output with one input
  • How to ignore a long touch so it switches only once

What you need

  • UNO R3 board and USB cable (UNO R3 Starter Kit)
  • Metal touch module (45-in-1 Sensor Kit)
  • 1 white LED and 1 resistor, 220 Ω (UNO R3 Starter Kit)
  • Breadboard and jumper wires (UNO R3 Starter Kit)

Wiring

Breadboard wiring diagram: Make a touch-sensitive lamp
Breadboard view. Check each connection against the wiring table below.
From To
Touch module AO Not used
Touch module G GND
Touch module + 5V
Touch module DO UNO pin 2
UNO pin 8 220 Ω resistor, then the LED's long leg
LED short leg GND

Sensor modules from different makers can label and order their pins differently. Always follow the labels printed on your module.

The sketch

Some versions of this module switch their output to HIGH when they trigger, others to LOW. The sketch reads the output when it starts (keep things quiet and still for a moment) and treats any change from that resting state as a trigger, so it works with both.

const int TOUCH_PIN = 2;
const int LED_PIN = 8;

int restState;            // output when nobody is touching
bool lampOn = false;
bool wasTouched = false;

void setup() {
  Serial.begin(9600);
  pinMode(TOUCH_PIN, INPUT);
  pinMode(LED_PIN, OUTPUT);
  delay(500);
  restState = digitalRead(TOUCH_PIN);
}

void loop() {
  bool touched = digitalRead(TOUCH_PIN) != restState;

  if (touched && !wasTouched) {   // the moment a touch starts
    lampOn = !lampOn;
    digitalWrite(LED_PIN, lampOn);
    Serial.println(lampOn ? "Lamp on" : "Lamp off");
    delay(300);                   // ignore the rest of this touch
  }
  wasTouched = touched;
}

How it works

Your body picks up tiny electrical signals from the mains wiring around you. When you touch the metal pin, the transistor on the module amplifies that signal, and the comparator switches DO. The sketch only reacts to the start of a touch, so holding your finger on the pin doesn't make the lamp flicker.

Try this next

  • Make a long touch (over one second) dim the LED with analogWrite() on pin 9.
  • Count how many times the lamp has been switched on.
  • Use the RGB LED from the UNO kit and change colour with each touch.

Troubleshooting

  • Nothing happens: adjust the module's knob until the signal LED reacts to your touch.
  • It switches by itself: keep the module away from long wires and chargers, and adjust the knob.
  • It toggles twice per touch: increase the delay(300).

Build it yourself

Everything used in this lesson comes in one kit, with parts tested by our engineers.

UNO R3 Starter Kit | Arduino IDE Compatible | Free Tutorials $56.00

Shop the kit