collision sensor/Reading it/06. The first read
Reading it · 06 of 10

The first read

Three wires, no library and a sketch that prints what the pin says five times a second. At rest it prints LOW, with the lever pushed in 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 on a hit, 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, so a bumper held in at power-up cannot change how it starts.

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, and the same as the TK04 push button's: 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 a bumper held in while the board starts cannot stop it starting. Any other plain digital pin works too.

The sketch

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

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. A robot cannot afford that, and the next sketch drops it.

What you should see

The serial monitor at 115200 prints a line five times a second. Push the lever in with a finger until it clicks, hold it, let go:

LOW   clear
LOW   clear
HIGH  hit
HIGH  hit
LOW   clear

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 BUMPER_PIN to the pin you wired SIGNAL to.

collision_first_read.ino
/*
  Collision Sensor - first read                          TK17 / /p/tk17

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

    GND    -> GND
    VCC    -> 5V on an Uno; 3V3 on an ESP32, ESP32-S3 or Pico
              (on a hit, 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 BUMPER_PIN = 4;

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

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

  if (level == HIGH) {
    Serial.println("HIGH  hit");
  } else {
    Serial.println("LOW   clear");
  }
  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 the lever is in. 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 push the lever or not.

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

It prints HIGH with nothing touching the lever.

Check that SIGNAL is not on a 3V3 or 5V pin by mistake, and that VCC and SIGNAL are not swapped. Then look at the lever: if the LED is lit, something on the robot is resting on it. Then the sketch: a test for LOW, copied from an active-low bumper's sketch, gets this block exactly backwards.

A quick flick of the lever sometimes does not show up.

The sketch reads the pin once every 200 ms and spends the rest of the time inside delay(). A touch 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 hit counter that goes up by three, and the metal inside the switch that makes it.

One bump counts as several

Edit this page — content/books/collision-sensor/the-first-read.mdx

Community

Questions about this product

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

Ask a question ↗

Collision Sensor

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