Built-in project · Arduino Uno R3

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 About 20 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.

Touch lamp, LED on pin 12 · Arduino Uno R3live0.000 s 0.00x
Press Run, then hold the foil
The circuit itself, running here on the simulator. Press what can be pressed; click anything else to open it in the editor.

What you'll learn

  • Capacitive touch sensing with a resistor and a pin pair
  • Using the CapacitiveSensor library
  • Choosing a threshold from measured data
  • Printing on a timer with millis()
  • Difficulty: Beginner
  • Time: About 20 minutes
  • Board: Arduino Uno R3
  • Parts: 6

How it works

A piece of foil on pin 2 works as a touch sensor. The foil and the wire make a tiny capacitor. A 1 megohm resistor connects pin 4 to pin 2. The CapacitiveSensor library changes pin 4 and counts how long pin 2 takes to follow.

A finger on the foil adds capacitance, so the count climbs. When the count goes over THRESHOLD, set to 100, the LED on pin 12 lights.

The serial monitor prints the count four times a second. Touch the foil and watch it jump, then pick a threshold between the touched and untouched numbers.

Parts list

6 parts, plus the jumper wires. They retail for about $30; on Mokxi they're free.

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 pinConnects to
Resistor, 220 Ω pin 1Arduino Uno R3 pin 12
Resistor, 1M Ω pin 1Arduino Uno R3 pin 4
Resistor, 1M Ω pin 2Arduino Uno R3 pin 2
Touch foil pin FOILArduino Uno R3 pin 2
LED, yellow pin CArduino Uno R3 pin GND
LED, yellow pin AResistor, 220 Ω pin 2

The code

The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.

sketch.ino · Arduino Uno R3
// 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);
}

Code walkthrough

The key functions of the sketch, and what each one does.

setup sketch.ino

Makes pin 12 an output and starts the serial port with a hint to hold the foil.

void setup() {
  pinMode(LED_PIN, OUTPUT);
  Serial.begin(9600);
  Serial.println("Mokxi Uno: touch lamp, LED on pin 12. Hold the foil.");
}

loop sketch.ino

Takes a reading with sensor.capacitiveSensor(30), lights the LED if it is over THRESHOLD, and prints the count every 250 ms. It then waits 10 ms.

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);
}

Troubleshooting

The usual mistakes with this circuit, and the fix for each.

  • The LED is always on or always off This project
    Watch the printed counts and move THRESHOLD between the touched and untouched numbers.
  • The count does not change This project
    Check the 1M resistor goes between pins 4 and 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.
  • The LED does not light, or it lights once and then dies. LED
    LEDs have polarity. The longer leg (anode) goes toward the positive side. Always add a series resistor, about 220 ohms to 1 k ohm on a 5 V pin.
  • A blue or white LED stays dark on 3.3 V. LED
    Blue and white LEDs drop about 3 V, so 3.3 V leaves almost nothing for the resistor. Use a 5 V supply, or a smaller resistor and a red or green LED.

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.