latching button/Reading it/05. The first read
Reading it · 05 of 9

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

Three wires
Your board
Switch
VCC to
3V3
SIGNAL to
GPIO 4
Reads
LOW
GND to GND, VCC to 3V3, SIGNAL to GPIO 4, and NC to nothing. VCC is 3V3 and never 5V: with the switch on, SIGNAL is VCC, and the ESP32-S3's pins are 3.3 V pins. The switch is off, so the pull-down holds SIGNAL at 0 V and every line says LOW.

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 OFF

Press 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.ino
/*
  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.

When it does not work

It prints OFF whatever I do.

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.

It prints ON and OFF at random.

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.

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 LED on the block works either way, because it does not depend on the sketch.

Why this pin and not another?

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.

Where this goes next

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

Community

Questions about this product

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

Ask a question ↗

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.

Browse Modules and blocks on the forum