Built-in project ยท Arduino Uno R3

Touch sensor on pin 2 lights the LED on 13, on an Arduino Uno

A capacitive touch module on pin 2: a finger on the pad drives its output HIGH and lights the LED on pin 13. The whole build, an Arduino Uno and 1 more part, 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.

Beginner About 10 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.

The circuit runs here once the page has loaded.

What you'll learn

  • Capacitive touch sensing
  • A module that gives a clean HIGH or LOW
  • Reading a digital input
  • Printing only on a change of state
  • Difficulty: Beginner
  • Time: About 10 minutes
  • Board: Arduino Uno R3
  • Parts: 3

How it works

The TTP223 pad senses the small change in capacitance that a finger adds. A chip on the module turns that into a plain HIGH or LOW on its SIG pin, which connects to pin 2.

The sketch reads pin 2 every 10 ms and copies it to the LED on pin 13. It prints touched or not touched only when the state changes. Click the pad in the simulator to touch it.

Parts list

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

Kits that can build it

This starter kit has every part in the circuit. Each kit's page lists what else it can build.

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
Touch sensor, TTP223 pin GNDArduino Uno R3 pin GND
Touch sensor, TTP223 pin SIGArduino Uno R3 pin 2
Touch sensor, TTP223 pin VCCArduino Uno R3 pin 5V

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 sensor on pin 2 lights the LED on 13.
//
//   touch module SIG -> pin 2   VCC -> 5V   GND -> GND
//
// The pad senses the small capacitance a finger adds; the chip on the module turns it into HIGH or LOW.
//
// Click the pad to touch it.

const int SENSOR = 2;
const int LED_PIN = 13;
int last = -1;

void setup() {
  pinMode(SENSOR, INPUT);
  pinMode(LED_PIN, OUTPUT);
  Serial.begin(9600);
  Serial.println("Mokxi Uno: touch sensor on pin 2, the LED on pin 13");
}

void loop() {
  int now = digitalRead(SENSOR) == HIGH ? 1 : 0;
  digitalWrite(LED_PIN, now ? HIGH : LOW);
  if (now != last) {
    last = now;
    Serial.println(now ? "touched" : "not touched");
  }
  delay(10);
}

Code walkthrough

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

setup sketch.ino

Makes SENSOR (pin 2) an input and LED_PIN (pin 13) an output. It starts the serial port at 9600 baud and prints a hello message.

void setup() {
  pinMode(SENSOR, INPUT);
  pinMode(LED_PIN, OUTPUT);
  Serial.begin(9600);
  Serial.println("Mokxi Uno: touch sensor on pin 2, the LED on pin 13");
}

loop sketch.ino

Sets now to 1 when pin 2 reads HIGH and writes it to the LED. If now differs from last, it prints touched or not touched. A 10 ms delay ends each pass.

void loop() {
  int now = digitalRead(SENSOR) == HIGH ? 1 : 0;
  digitalWrite(LED_PIN, now ? HIGH : LOW);
  if (now != last) {
    last = now;
    Serial.println(now ? "touched" : "not touched");
  }
  delay(10);
}

Troubleshooting

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

  • Nothing happens when you touch the pad. This project
    Check SIG on pin 2, VCC on 5V and GND on GND. The SENSOR value must be 2.
  • The LED is on when not touched. This project
    The sketch expects HIGH when touched. A module set up the other way would need the test flipped.
  • It never triggers, or triggers by itself. Touch sensor
    Power it with 2.0 V to 5.5 V and share ground. Keep your hand and the wires away from the pad while it powers up, since it sets its baseline at startup. Avoid long wires.
  • The output is HIGH when you expect LOW. Touch sensor
    Pads on the board are jumpers for mode. The default is momentary and active high. Soldering the jumpers can change it to toggle or active low. Check your board and its mode.
  • 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.