Touch lamp on an Arduino Uno
The Starter Kit’s touchy-feely lamp: foil, a 1 megohm resistor and the CapacitiveSensor library light the LED at a touch. The whole build, an Arduino Uno and 2 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.
Teaching? Assign it to your class in one click.
Beginner About 15 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $30. On Mokxi they're free.
What you'll learn
- Capacitive touch sensing with a resistor and foil
- Using the CapacitiveSensor library
- Setting a threshold for a touch
- Reading the raw count to tune it
- Difficulty: Beginner
- Time: About 15 minutes
- Board: Arduino Uno R3
- Parts: 4
How it works
A piece of foil on pin 2 acts as a tiny capacitor, a few picofarads. A 1 megohm resistor joins pin 4 to pin 2. The CapacitiveSensor library changes pin 4 and counts how long pin 2 takes to follow it through the resistor.
A finger on the foil adds about a hundred picofarads, so the count climbs several times over. The library also subtracts the lowest count it has seen, so an untouched foil reads near zero.
When the count goes above THRESHOLD, set to 100, the sketch lights the Uno's own LED on pin 13. It prints the count every 250 ms.
Parts list
4 parts, plus the jumper wires. They retail for about $30; on Mokxi they're free.
- 1 × Arduino Uno R3In Starter Kit, Claw Machine Kit, Vending Machine Kit.
- 1 × Half breadboard
- 1 × Resistor, 1M ΩIn Starter Kit, Claw Machine Kit, Halloween Prop Kit.
- 1 × Touch foil
Wiring table
Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.
| Part and pin | Connects to |
|---|---|
| Resistor, 1M Ω pin 1 | Arduino Uno R3 pin 4 |
| Resistor, 1M Ω pin 2 | Arduino Uno R3 pin 2 |
| Touch foil pin FOIL | Arduino Uno R3 pin 2 |
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// Touch lamp: the Arduino Starter Kit's "Touchy-feely Lamp". A piece of foil
// on pin 2, a 1 megohm resistor from pin 4 to it, and the CapacitiveSensor
// library counting how long the foil takes to follow pin 4.
//
// 1 M resistor pin 4 to pin 2
// foil pin 2
// lamp the Uno's own LED on pin 13
//
// The foil is a tiny capacitor, a few picofarads. Each time pin 4 changes,
// the library counts loop passes until pin 2 follows it through the
// resistor, which takes about R times C. A finger on the foil adds roughly a
// hundred picofarads, so the count climbs several times over, and past the
// threshold the lamp comes on. capacitiveSensor() takes away the lowest count
// it has seen, so the untouched foil reads near zero.
//
// Things to try. Hold the foil and watch the count. Then change the foil's
// finger property in the panel: a smaller number is a lighter touch, and
// below the threshold it no longer lights the lamp.
#include <CapacitiveSensor.h>
CapacitiveSensor capSensor = CapacitiveSensor(4, 2);
const int LAMP_PIN = 13;
const long THRESHOLD = 100;
unsigned long lastPrint = 0;
void setup() {
Serial.begin(9600);
pinMode(LAMP_PIN, OUTPUT);
Serial.println("Touch lamp ready");
}
void loop() {
long sensorValue = capSensor.capacitiveSensor(30);
digitalWrite(LAMP_PIN, sensorValue > THRESHOLD ? HIGH : LOW);
if (millis() - lastPrint >= 250) {
lastPrint = millis();
Serial.println(sensorValue);
}
delay(10);
}
Code walkthrough
The key functions of the sketch, and what each one does.
setup sketch.ino
It starts the serial port at 9600 and sets the lamp pin 13 as an output. Then it prints "Touch lamp ready".
void setup() {
Serial.begin(9600);
pinMode(LAMP_PIN, OUTPUT);
Serial.println("Touch lamp ready");
}loop sketch.ino
It asks the library for a reading, using 30 samples. If the value is above THRESHOLD, the lamp on pin 13 goes on, otherwise off. Every 250 ms it prints the value. A short delay of 10 ms ends each pass.
void loop() {
long sensorValue = capSensor.capacitiveSensor(30);
digitalWrite(LAMP_PIN, sensorValue > THRESHOLD ? HIGH : LOW);
if (millis() - lastPrint >= 250) {
lastPrint = millis();
Serial.println(sensorValue);
}
delay(10);
}Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- The lamp never comes on, or is always on. This project
Watch the printed count while you touch the foil, then adjustTHRESHOLDso it sits between the untouched and touched values. - The reading does not change when you touch. This project
Check the 1 megohm resistor runs from pin 4 to pin 2, and the foil is on pin 2. - The LED is dim, burns out, or the pin reads wrong because the resistor value is off. Resistor
Read the color bands again. A 220 ohm to 330 ohm resistor suits a typical LED on 5 V. 10k is a common pull-up or pull-down value. Check with a multimeter if unsure. - A pull-up or pull-down resistor is missing, so the input floats and reads random values. Resistor
Tie the input to 5 V or ground through about 10k. Or use INPUT_PULLUP and wire the switch to ground. - The reading is zero or never changes. Touch foil
The CapacitiveSensor library needs a send pin and a receive pin with a resistor between them (about 1M). The foil goes to the receive pin. Check both pins in your code. - The values are noisy or the threshold is wrong. Touch foil
Readings vary with the wire, hand, and ground. Take a baseline with no touch, then set your threshold above it. Average several samples. - Upload fails or nothing runs after wiring pins 0 and 1 to a module. Arduino Uno R3
Pins 0 and 1 are the USB serial port. Move the module to other pins, or unplug it while uploading. Also check the right board is selected. - Pins misbehave or the board resets when you power motors or servos from it. Arduino Uno R3
Each I/O pin gives about 20 mA, 40 mA max. Power motors and servos from their own supply and join its ground to the Uno's GND.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.