A touch that latches
Touch once and a light comes on, touch again and it goes off. The block only says HIGH while a finger is on the pad, and its chip's own toggle mode is switched off in copper, so the on-or-off lives in the sketch: one variable, flipped each time SIGNAL changes from LOW to HIGH.
The block forgets
This block's output is momentary. SIGNAL is HIGH while a finger is on the pad and LOW the moment it lifts. The chip can do toggling itself, but only with its TOG pin held high, and on this board TOG is soldered to GND. So a light that should stay on after the touch has to be remembered by the sketch.
Four touches, and the finger is on the pad for well under half of the three
seconds. The ledOn lane stays where each touch left it. The block said
nothing between touches; the variable is the memory.
One variable, flipped on the change
The sketch is the first read with the printing taken out. Where it used to
print on every change, it now flips ledOn on each change to HIGH and writes
the result to LED_PIN.
- Flip on the change, not on the level. Flipping on every pass of
loop()while the pin is HIGH would flip thousands of times per touch and land at random. - No debounce. A push button needs one, because its metal contacts chatter for a few milliseconds. The touch chip decides once per look at the pad and drives SIGNAL cleanly, so the first change is the only one.
The second pin drives a TK01 XL LED: D9 on an Uno, GPIO 4 on an ESP32, GPIO 5 on an ESP32-S3, GP14 on a Pico. Its first blink shows the two wires. Without one, watch the serial monitor print on and off.
What the variable can do that a latch cannot
A toggle in software forgets at every reset. In return it can do more than on and off: count touches, time how long the finger stays, treat a long touch differently from a short one, or cycle through modes. Timing a touch starts late by up to a fifth of a second on the first touch after a rest, as asleep and awake explains, which is fine for telling a tap from a hold.
The code
The first read with one change: instead of printing every change, each change to HIGH flips ledOn and writes it to a second pin. A TK01 XL LED on that pin shows it. No library, and no debounce.
/*
Touch Sensor - a toggle 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
A TK01 XL LED on a second pin shows the state (optional):
GND -> GND
SIGNAL -> D9 on an Uno, GPIO 4 on an ESP32, GPIO 5 on an
ESP32-S3, GP14 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.
*/
// The GPIO number the touch block's SIGNAL is wired to.
// Uno: 2. ESP32: 25. ESP32-S3: 4. Pico: 15.
const int TOUCH_PIN = 4;
// The GPIO number the TK01's SIGNAL is wired to.
// Uno: 9. ESP32: 4. ESP32-S3: 5. Pico: 14.
const int LED_PIN = 5;
int last = LOW;
bool ledOn = false; // the toggle's memory
void setup() {
Serial.begin(115200);
pinMode(TOUCH_PIN, INPUT); // the chip drives SIGNAL both ways
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW);
}
void loop() {
int reading = digitalRead(TOUCH_PIN);
if (reading != last) {
if (reading == HIGH) { // a finger just landed: flip
ledOn = !ledOn;
digitalWrite(LED_PIN, ledOn ? HIGH : LOW);
Serial.println(ledOn ? "on" : "off");
}
last = reading;
}
}ledOn is the whole memory of the toggle. The block forgets the moment the finger lifts, and its red LED goes out with it, but the TK01 stays as you left it. Every reset starts it off. The sketch compiles for an ESP32-S3 and an Uno.
View on GitHub · blocks/tk43-tp223-touch-sensor/arduino/touch_toggle/touch_toggle.ino @ v1.0When it does not work
The sketch is flipping on the level, not the change: every pass of loop() while SIGNAL is HIGH flips it again, thousands of times a second. Flip only when the reading differs from the last one and is HIGH, as the sketch does.
The sketch is testing for the change to LOW. On this block HIGH is touched, so the change to HIGH is the moment the finger lands. Toggling on release is a valid choice, but it feels late.
The variable lives in the microcontroller's memory, which starts empty at every power-up and reset. To remember across a power cut, save it: EEPROM on an Uno, Preferences on an ESP32.
Yes. Leave the LED lines as they are and watch the serial monitor, which prints on or off at every touch. Any LED with a resistor on the LED_PIN pin works the same way as the TK01.
The short list of reasons, split in two by the red LED.
When a touch does nothing →Edit this page — content/books/tp223-touch-sensor/a-touch-that-latches.mdx
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.