Built-in project · Arduino Uno R3

Touch keys on an Arduino Uno

The red TTP229-B 16-key touch keypad on two wires, read with TTP229.h: touch a pad and its number prints on the serial monitor. 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.

Teaching? Assign it to your class in one click.

Beginner About 10 minutes to build and run. Runs in your browser. Free, and no account needed.

The parts retail for about $32. On Mokxi they're free.

Touch keys · Arduino Uno R3live0.000 s 0.00x
Click to open it in the editor
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

  • Sensors
  • Using the Touch keypad
  • Reading and changing a sketch
  • Difficulty: Beginner
  • Time: About 10 minutes
  • Board: Arduino Uno R3
  • Parts: 3

How it works

The red TTP229-B 16-key touch keypad on two wires, read with TTP229.h: touch a pad and its number prints on the serial monitor.

Arduino Uno R3 runs the sketch below. The circuit around it: Touch keypad, TTP229 16-key. Serial.begin opens the serial port at 9600 baud. millis reads the time since power-up, so the sketch can time things without stopping.

Parts list

3 parts, plus the jumper wires. They retail for about $32; 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
Touch keypad, TTP229 16-key pin SDOArduino Uno R3 pin 3
Touch keypad, TTP229 16-key pin SCLArduino Uno R3 pin 2
Touch keypad, TTP229 16-key pin VCCArduino Uno R3 pin 5V
Touch keypad, TTP229 16-key pin GNDArduino Uno R3 pin GND

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 keys: the red TTP229-B keypad prints the key you touch.
//
//   pin 2  the keypad's SCL      pin 3  its SDO
//   VCC -> 5 V, GND -> GND       the TP2 pads linked: 16 keys
//
// Sixteen pads and only two wires. The TTP229 is not I2C and not SPI: while
// a key is touched it pulses SDO low for 93 us once a scan (that is "data
// valid"), and then each clock pulse on SCL, low then high, puts the next
// key on SDO, key 1 first, low if it is touched. Sixteen pulses read the
// whole pad. TTP229.h does all of that.
//
// As shipped the board is in 8-key mode and sends only 8 bits. Linking the
// TP2 pads (the part's `keys` setting, 16 here) makes it 16; with them open,
// call ReadKey8() instead.
//
// ReadKey16() waits for a touch, and while a key is held the chip reports
// it once a scan, every 32 ms. So a key held down comes back again and
// again; the sketch prints it once, and again only after a gap (a new
// touch) or when the key changes.

#include <TTP229.h>

const int SCL_PIN = 2;
const int SDO_PIN = 3;

TTP229 ttp229(SCL_PIN, SDO_PIN);

uint8_t last = 0;
unsigned long lastSeen = 0;

void setup() {
  Serial.begin(9600);
  Serial.println("TTP229 touch keys: touch a key");
}

void loop() {
  uint8_t key = ttp229.ReadKey16();  // waits for a touch
  unsigned long now = millis();
  if (key && (key != last || now - lastSeen > 100)) {
    Serial.print("Key ");
    Serial.println(key);
  }
  last = key;
  lastSeen = now;
}

Code walkthrough

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

setup sketch.ino

Runs once when the board starts. It gets the pins and libraries ready.

void setup() {
  Serial.begin(9600);
  Serial.println("TTP229 touch keys: touch a key");
}

loop sketch.ino

Runs over and over for as long as the board has power. This is where the project does its work.

void loop() {
  uint8_t key = ttp229.ReadKey16();  // waits for a touch
  unsigned long now = millis();
  if (key && (key != last || now - lastSeen > 100)) {
    Serial.print("Key ");
    Serial.println(key);
  }
  last = key;
  lastSeen = now;
}

Troubleshooting

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

  • 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.