The first read
Three wires, no library, and a sketch that prints the switch's position twice a second. pinMode makes the pin a plain input, because the pull-down on the board already holds it LOW.
Three wires
GND to GND. VCC to your board's own logic supply, 5V on an Uno and 3V3 on the others. SIGNAL to a digital pin. NC stays unconnected, because nothing on the board is attached to it.
The pins in the sketch are ordinary inputs that do nothing else while the board starts: D2 on an Uno, GPIO 25 on an ESP32, GPIO 4 on an ESP32-S3, GP15 on a Pico. They are the same pins as the TK04 push button's book, so either block drops into the same wiring.
If your block came with a TinkerBlock cable, it carries all four pins, and the NC wire simply ends at a pin that goes nowhere.
The sketch
Two calls do the work. pinMode(SWITCH_PIN, INPUT) makes the pin an input with
no pull of its own; the block's pull-down is enough. digitalRead(SWITCH_PIN)
returns HIGH when the switch is on and LOW when it is off.
The loop reads the pin and prints what it found, then waits half a second. Every line is a report of the position at that moment, whether or not anything changed.
What you should see
With the switch off, the serial monitor at 115200 prints:
SIGNAL is LOW: the switch is OFF
SIGNAL is LOW: the switch is OFFPress the switch once. The LED lights, and within half a second the lines change to ON and stay ON with your hand off it. Press it again and they go back to OFF.
Now press your board's reset button with the switch on. The first line after the restart already says ON. The sketch has no memory of its own; the switch kept the state for it. Surviving a reset makes use of that.
If the lines never change, go to when it reads wrong.
The code
No library. pinMode sets the pin as an input and digitalRead returns HIGH or LOW. Change SWITCH_PIN to the pin you wired SIGNAL to.
/*
Latching button - first read TK05 / /p/tk05
Wiring. Count from the square pad on the TinkerBlock board, switch
side up, header at the bottom:
GND -> GND
VCC -> 5V on an Uno; 3V3 on an ESP32, ESP32-S3 or Pico
(never 5V on a 3.3 V board: SIGNAL is VCC when on)
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.
*/
// The GPIO number SIGNAL is wired to.
// Uno: 2. ESP32: 25. ESP32-S3: 4. Pico: 15.
const int SWITCH_PIN = 4;
void setup() {
Serial.begin(115200);
pinMode(SWITCH_PIN, INPUT); // the board's pull-down holds it LOW
}
void loop() {
if (digitalRead(SWITCH_PIN) == HIGH) { // switch on: SIGNAL is VCC
Serial.println("SIGNAL is HIGH: the switch is ON");
} else { // switch off: 0 V
Serial.println("SIGNAL is LOW: the switch is OFF");
}
delay(500);
}INPUT, not INPUT_PULLUP: the 10 kΩ pull-down on the block already holds SIGNAL at 0 V when the switch is off, and an internal pull-up would only fight it. The pin is read, never driven: never set it as an OUTPUT.
The same read in MicroPython, for an ESP32, an ESP32-S3 or a Pico. Pin.IN makes the pin an input, and value() returns 1 or 0.
"""
Latching button - first read, MicroPython TK05 / /p/tk05
Wiring. Count from the square pad on the TinkerBlock board, switch
side up, header at the bottom:
GND -> GND
VCC -> 3V3 (never 5V: SIGNAL is VCC when the switch is on,
and these are 3.3 V pins)
NC -> nothing (unconnected on the board)
SIGNAL -> GPIO 25 on an ESP32, GPIO 4 on an ESP32-S3,
GP15 on a Raspberry Pi Pico
Thonny
Run > Configure interpreter MicroPython (ESP32) or
MicroPython (Raspberry Pi Pico)
Save it to the board as main.py to run it on every power-up.
Nothing to install: machine and time are built in.
"""
from machine import Pin
import time
# The GPIO number SIGNAL is wired to. ESP32: 25. ESP32-S3: 4. Pico: 15.
SWITCH_PIN = 4
switch = Pin(SWITCH_PIN, Pin.IN) # the board's pull-down holds it LOW
while True:
if switch.value() == 1: # switch on: SIGNAL is VCC
print("SIGNAL is HIGH: the switch is ON")
else: # switch off: 0 V
print("SIGNAL is LOW: the switch is OFF")
time.sleep_ms(500)No pull argument: the pull-down on the block holds the pin, so the pin needs none of its own. There is no Uno here, because an Uno cannot run MicroPython. Stop it with Ctrl-C in Thonny's shell.
When it does not work
Look at the LED first. If it never lights, VCC is not reaching the switch: check the VCC wire, then count from the square pad. If the LED lights and the sketch still says OFF, SIGNAL is on a different pin from the one in SWITCH_PIN.
The pin is floating: SIGNAL is not actually connected to it, so the pull-down on the block cannot hold it. Reseat the SIGNAL wire, check it is in the pin the sketch names, and check GND. A right-angle header can be lifted out of a breadboard row by a cable's weight.
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 LED on the block works either way, because it does not depend on the sketch.
Any ordinary digital input works. These are the same pins the TK04 push-button book uses, so the two blocks can be swapped without rewiring, and none of them is a pin the board reads at reset to decide how to start. Keep off those, and off the ESP32's flash pins.
The sketch says ON twice a second. Most sketches want to hear it once, when it changes.
A level, not an event →Edit this page — content/books/latching-button/the-first-read.mdx
Questions about this product
See what other owners have asked, and read their solutions.
Latching Button
Loading 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.