Touch lamp, LED on pin 12, on an Arduino Uno
Foil on pin 2, a 1 megohm resistor from pin 4 and the CapacitiveSensor library: a touch on the foil lights the LED on pin 12. The whole build, an Arduino Uno and 4 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 code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// Touch lamp with its LED on pin 12: a piece of foil you light with a finger.
//
// 1 M resistor from pin 4 to pin 2
// foil on pin 2
// LED pin 12, through 220 ohm to GND
//
// The foil and the wire to it make a tiny capacitor. Each time pin 4 changes,
// the CapacitiveSensor library counts how long pin 2 takes to follow it
// through the 1 M resistor. A finger on the foil adds a little capacitance,
// the count climbs, and past the threshold the LED lights.
//
// The serial monitor shows the count four times a second. Touch the foil and
// watch it jump, then set THRESHOLD between the two numbers you see.
#include <CapacitiveSensor.h>
CapacitiveSensor sensor = CapacitiveSensor(4, 2); // send pin, receive pin
const int LED_PIN = 12;
const long THRESHOLD = 100;
unsigned long lastPrint = 0;
void setup() {
pinMode(LED_PIN, OUTPUT);
Serial.begin(9600);
Serial.println("Mokxi Uno: touch lamp, LED on pin 12. Hold the foil.");
}
void loop() {
long count = sensor.capacitiveSensor(30);
digitalWrite(LED_PIN, count > THRESHOLD ? HIGH : LOW);
if (millis() - lastPrint >= 250) {
lastPrint = millis();
Serial.println(count);
}
delay(10);
}
Parts list
6 parts, plus the jumper wires. Every one is in the editor's parts bin.
- 1 × Arduino Uno R3
- 1 × Half breadboard
- 1 × Resistor, 1M Ω
- 1 × Touch foil
- 1 × Resistor, 220 Ω
- 1 × LED, yellow
How it is wired
5 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 12; Resistor, 220 Ω pin 1
- Arduino Uno R3 pin 4; Resistor, 1M Ω pin 1
- Arduino Uno R3 pin 2; Resistor, 1M Ω pin 2; Touch foil pin FOIL
- Ground: Arduino Uno R3 pin GND; LED, yellow pin C
- Resistor, 220 Ω pin 2; LED, yellow pin A
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.