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
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 clearThe 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 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.
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.
"""
Collision Sensor - first read, MicroPython TK17 / /p/tk17
Wiring. Count from the square pad on the TinkerBlock board, switch
at the top, header at the bottom:
GND -> GND
VCC -> 3V3 (never 5V: on a hit, SIGNAL gives your pin VCC)
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.
BUMPER_PIN = 4
bumper = Pin(BUMPER_PIN, Pin.IN) # no pull: the block has its own
while True:
if bumper.value() == 1:
print("HIGH hit")
else:
print("LOW clear")
time.sleep_ms(200) # five reads a secondThere is no Uno here: an Uno cannot run MicroPython. No pull argument: the block's own pull-down holds SIGNAL LOW, so Pin.PULL_UP would only fight it. Stop it with Ctrl-C in Thonny's shell.
When it does not work
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.
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.
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.
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.
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
Questions about this product
See what other owners have asked, and read their solutions.
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.