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

The first read

Three wires, no library and a sketch that prints what the pin says five times a second. Released it prints LOW, pressed it prints HIGH, and the red LED on the block lights with it.

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. VCC is the voltage your pin receives when the button is down, so on a 3.3 V board it 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 in every sketch in this 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, so the block cannot stop the board from starting. Any other plain digital pin works too.

If your block came with a TinkerBlock cable, it carries all four wires, and the NC wire simply ends at a pin that goes nowhere.

The sketch

Two calls do the work. pinMode(BUTTON_PIN, INPUT) makes the pin an input with nothing of its own switched on, because the block already has its pull-down. digitalRead(BUTTON_PIN) returns HIGH or LOW: HIGH is pressed.

The delay(200) makes it read five times a second, which keeps the serial monitor readable. It also means the sketch is deaf for 200 ms at a time, the same trade the XL LED's first blink makes. Blinking without stopping is about why that matters, and the next sketch here drops it.

What you should see

The serial monitor at 115200 prints a line five times a second:

LOW   released
LOW   released
HIGH  pressed
HIGH  pressed
LOW   released

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

The code

No library. pinMode makes the pin an input, and digitalRead returns HIGH or LOW. Change BUTTON_PIN to the pin you wired SIGNAL to.

push_button_first_read.ino
/*
  Push Button - first read                               TK04 / /p/tk04

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

    GND    -> GND
    VCC    -> 5V on an Uno; 3V3 on an ESP32, ESP32-S3 or Pico
              (pressed, 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.
*/

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

void setup() {
  Serial.begin(115200);
  pinMode(BUTTON_PIN, INPUT);   // the block has its own pull-down
}

void loop() {
  int level = digitalRead(BUTTON_PIN);

  if (level == HIGH) {
    Serial.println("HIGH  pressed");
  } else {
    Serial.println("LOW   released");
  }
  delay(200);                   // five reads a second, to keep it readable
}

INPUT, not INPUT_PULLUP: the block has its own 10 kΩ pull-down, and the chip's pull-up would fight it. HIGH means pressed on this block. The delay(200) is only to keep the output readable; the next sketch drops it.

When it does not work

It prints LOW whether I press or not.

Look at the red LED first. If it stays dark when you press, VCC or GND is not connected. If it lights, SIGNAL is on a different pin from the one BUTTON_PIN names: the number is the GPIO number printed beside the pin, not its position along the header.

It prints HIGH when I am not pressing.

Check that SIGNAL is not on a 3V3 or 5V pin by mistake, and that VCC and SIGNAL are not swapped. Then check the sketch: a test for LOW, copied from a sketch for a pull-up board, gets this block exactly backwards.

A quick tap sometimes does not show up.

The sketch reads the pin once every 200 ms and spends the rest of the time inside delay(). A tap shorter than that can begin and end between two reads. That is fine for a first look; the counting sketch in the next article reads the pin continuously.

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

A press counter that goes up by three, and the metal inside the switch that makes it.

Why one press counts as several

Edit this page — content/books/push-button/the-first-read.mdx

Community

Questions about this product

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

Ask a question ↗

Push 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