touch sensor/Wiring it/07. The first touch
Wiring it · 07 of 9

The first touch

Three wires, no library, and a sketch that prints a line each time the pad is touched and released. VCC goes to your board's logic supply, SIGNAL to a plain input: D2 on an Uno, GPIO 25 on an ESP32, GPIO 4 on an ESP32-S3, GP15 on a Pico. The red LED on the block lights with every touch.

Three wires

Three wires
Your board
Wires
3
VCC to
3V3
SIGNAL to
GPIO 4
GND to GND, VCC to 3V3, SIGNAL to GPIO 4. SIGNAL is the chip's output, driven from VCC, so on a 3.3 V board VCC has to be 3V3, never 5V. GPIO 4 is a plain input that the ESP32-S3 does not read at boot.

GND to GND. VCC to your board's logic supply: 5V on an Uno, 3V3 on the three 3.3 V boards. SIGNAL to a digital pin. NC stays unconnected.

The input pin is the same as in the push-button book: D2 on an Uno, GPIO 25 on an ESP32, GPIO 4 on an ESP32-S3, GP15 on a Pico. None of them is read by the chip at boot or tied up with its flash memory. Any other plain digital pin works too.

Plug in with your hand clear of the pad. The chip learns the pad in its first half second, and a finger there becomes part of what it thinks is nothing.

The sketch

pinMode(TOUCH_PIN, INPUT) makes the pin an input with nothing of its own switched on. The block's chip drives SIGNAL HIGH and LOW itself, so the pin needs no pull-up. digitalRead(TOUCH_PIN) returns HIGH while the pad is touched.

The loop reads the pin as fast as it can and prints only when the reading differs from the last one, so each touch is two lines rather than a stream. No debounce is needed: the chip decides once per look at the pad and drives the line cleanly.

What you should see

The serial monitor at 115200 prints a pair of lines for each touch:

HIGH  touched
LOW   released
HIGH  touched
LOW   released

The red LED on the block lights for exactly as long as a line says HIGH. If it lights and the monitor stays quiet, the fault is the pin number or the SIGNAL wire. If neither changes, it is VCC or GND.

The sketch compiles for an ESP32-S3 and an Uno. Here is the same block on an Uno and on a Pico-format board, lighting an XL LED block when touched:

The TK43 on a LEGO baseplate beside an XL LED block, both wired to an Arduino Uno. A finger on the gold pad lights the blue LED.
The same block with a Pico 2 on a baseplate: a touch on the pad lights the XL LED.

The code

No library. pinMode makes the pin an input and digitalRead returns HIGH while the pad is touched. The sketch prints only when the reading changes. Change TOUCH_PIN to the pin you wired SIGNAL to.

touch_first_read.ino
/*
  Touch Sensor - first read                              TK43 / /p/tk43

  Wiring. Count from the square pad on the TinkerBlock board, pad
  up, header at the bottom:

    GND    -> GND
    VCC    -> 5V on an Uno; 3V3 on an ESP32, ESP32-S3 or Pico
              (touched, SIGNAL gives your pin whatever VCC is)
    NC     -> nothing   (unconnected on the board)
    SIGNAL -> D2 on an Uno, GPIO 25 on an ESP32, GPIO 4 on an
              ESP32-S3, GP15 on a Raspberry Pi Pico

  Arduino IDE
    Tools > Board                 your board, e.g. ESP32S3 Dev Module
    Tools > Port                  the one that appears when you plug in
    Tools > USB CDC On Boot       Enabled   (ESP32-S3 only)
    No library needed.

  Keep your hand off the pad while the board powers up.
*/

// The GPIO number SIGNAL is wired to.
// Uno: 2. ESP32: 25. ESP32-S3: 4. Pico: 15.
const int TOUCH_PIN = 4;

int last = LOW;

void setup() {
  Serial.begin(115200);
  pinMode(TOUCH_PIN, INPUT);    // the chip drives SIGNAL both ways
}

void loop() {
  int reading = digitalRead(TOUCH_PIN);

  if (reading != last) {        // print only when it changes
    if (reading == HIGH) {
      Serial.println("HIGH  touched");
    } else {
      Serial.println("LOW   released");
    }
    last = reading;
  }
}

INPUT, with no pull-up: the chip drives SIGNAL both ways itself. Keep your hand off the pad while the board powers up, because the chip spends its first half second learning what untouched looks like. The sketch compiles for an ESP32-S3 and an Uno.

View on GitHub · blocks/tk43-tp223-touch-sensor/arduino/touch_first_read/touch_first_read.ino @ v1.0

When it does not work

The red LED lights but the monitor prints nothing.

The chip saw the touch and drove SIGNAL HIGH, so the fault is after it. Check TOUCH_PIN names the pin SIGNAL is really on, that the wire is on SIGNAL (the fourth hole) and not NC (the third), and that GND is shared.

Nothing happens, not even the LED.

VCC or GND is missing, or the cable is one pin over. Count from the square pad: GND, VCC, NC, SIGNAL. If the wiring is right, unplug the board, keep your hand off the pad, and plug it in again: a finger on the pad at power-up spoils its baseline.

It prints touched as soon as it starts.

Something is on the pad or pressing against it: a wire lying across it, a finger holding the board. Clear it and reset. The chip never times a touch out, so it stays HIGH for as long as the thing stays.

The serial monitor stays empty on my ESP32-S3.

Set Tools > USB CDC On Boot to Enabled and upload again. Without it the S3's USB port does not bring up a serial port at boot, so the sketch runs with nowhere to print. The red LED lighting tells you the block itself is fine.

Where this goes next

One touch on, the next off, with the memory in the sketch.

A touch that latches →

Edit this page — content/books/tp223-touch-sensor/the-first-touch.mdx

Community

Questions about this product

See what other owners have asked, and read their solutions.

This page covers several products. Choose yours to see the right discussions.

Discuss this article

Ask about this page. The answer stays here, on the page it belongs to, for whoever hits the same wall next.

Browse Modules and blocks on the forum →