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 Runs in your browser. Free, and no account needed.
The parts retail for about $30. On Mokxi they're free.
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);
}
Parts list
4 parts, plus the jumper wires. They retail for about $30; on Mokxi they're free.
- 1 × Arduino Uno R3
- 1 × Half breadboard
- 1 × Resistor, 1M Ω
- 1 × Touch foil
How it is wired
2 connections, pin by pin, read from the circuit itself. Each line is one set of pins joined together, by a jumper wire or a breadboard strip.
- Arduino Uno R3 pin 4; Resistor, 1M Ω pin 1
- Arduino Uno R3 pin 2; Resistor, 1M Ω pin 2; Touch foil pin FOIL
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.