The first read
Three wires, no library, and a sketch that prints each time something enters the slot and leaves it. VCC goes to your board's logic voltage, SIGNAL to the pin the Push Button, Hall Effect and Reed Switch books use: GPIO 4 on an ESP32-S3, GPIO 25 on an ESP32, D2 on an Uno, GP15 on a Pico.
Three wires
GND to GND. VCC to your board's logic voltage: 3V3 on an ESP32, an ESP32-S3 or a Pico, 5V on an Uno. SIGNAL to the pin this book uses throughout. NC stays unconnected.
The pins are the Push Button, Hall Effect and Reed Switch books' pins, so one wiring serves all four blocks. None of them is a strapping pin, which matters here: with the slot clear at power-up the comparator holds SIGNAL LOW, and a strapping pin held LOW can change how an ESP32 starts.
The sketch
pinMode(SENSOR_PIN, INPUT) and nothing else: the pull-up is on the board.
In loop(), digitalRead gives HIGH while the slot is blocked and LOW
while it is clear. The sketch keeps the last reading in last and prints
only when the new one differs, so the monitor shows one line per change
instead of a hundred a second. Each change to HIGH adds one to passes.
What you should see
At 115200 the serial monitor prints clear once at the start. Push a piece
of card into the slot: the red LED goes out and it prints blocked (pass 1). Pull it out: the LED comes back and it prints clear.
The sketch compiles for an ESP32-S3 and an Uno. Here is the block working with the owner's own sketches, which light an XL LED block while the slot is blocked:
Why this is not yet a speed sensor
The loop looks at SIGNAL every 10 ms. A hand is slow enough for that. A slotted disc on a motor is not: at a few hundred revolutions a minute its pulses are shorter than 10 ms, and most of them fall between two looks. A tachometer counts on an interrupt instead.
The code
No library. digitalRead returns HIGH while something blocks the slot and LOW while it is clear; the sketch prints only when that changes, and counts each time the slot is blocked. Change SENSOR_PIN to the pin you wired SIGNAL to.
/*
Infrared Speed Sensor - first read TK61 / /p/tk61
Wiring. Count from the square pad on the TinkerBlock board, parts
up, header at the bottom:
GND -> GND
VCC -> 3V3 on an ESP32, ESP32-S3 or Pico; 5V on an Uno.
Your board's logic voltage: the block pulls SIGNAL
up to VCC with 10k.
NC -> nothing (unconnected on the board)
SIGNAL -> GPIO 4 on an ESP32-S3, GPIO 25 on an ESP32,
D2 on an Uno, 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 pin SIGNAL is wired to.
// Uno: 2. ESP32: 25. ESP32-S3: 4. Pico: 15.
const int SENSOR_PIN = 4;
int last = -1; // nothing read yet
unsigned long passes = 0; // times something entered the slot
void setup() {
Serial.begin(115200);
// INPUT: the board drives SIGNAL both ways, LOW from its
// comparator and HIGH through its own 10k pull-up.
pinMode(SENSOR_PIN, INPUT);
}
void loop() {
int level = digitalRead(SENSOR_PIN);
if (level != last) {
if (level == HIGH) {
passes++;
Serial.print("blocked (pass ");
Serial.print(passes);
Serial.println(")");
} else {
Serial.println("clear");
}
last = level;
}
delay(10);
}pinMode is INPUT: the board drives SIGNAL both ways, LOW from its comparator and HIGH through its own 10 kΩ. The sketch looks every 10 ms, which is fine for a hand and too slow for a motor. It compiles for an ESP32-S3 and an Uno.
View on GitHub · blocks/tk61-ir-speed-sensor/arduino/ir_speed_first_read/ir_speed_first_read.ino @ v1.5The same in MicroPython: read SIGNAL every 10 ms, print when it changes, count the times it is blocked.
"""
Infrared Speed Sensor - first read, MicroPython TK61 / /p/tk61
Wiring. Count from the square pad on the TinkerBlock board, parts
up, header at the bottom:
GND -> GND
VCC -> 3V3 (the block pulls SIGNAL up to VCC with 10k)
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 is built in.
"""
from machine import Pin
import time
# The GPIO number SIGNAL is wired to. ESP32: 25. ESP32-S3: 4. Pico: 15.
SENSOR_PIN = 4
sensor = Pin(SENSOR_PIN, Pin.IN) # the board drives SIGNAL both ways
last = -1
passes = 0
while True:
level = sensor.value()
if level != last: # print only when it changes
if level:
passes += 1
print("blocked (pass", passes, ")")
else:
print("clear")
last = level
time.sleep_ms(10)Pin.IN with no pull: the board has its own. Save it as main.py to run it without Thonny. Stop it with Ctrl-C.
View on GitHub · blocks/tk61-ir-speed-sensor/micropython/ir_speed_first_read.py @ v1.5When it does not work
Then the block is fine and the level is not reaching the pin you read. Check SIGNAL is on the pin SENSOR_PIN names, and that it is the right-hand pin, not NC beside it.
The block has no power, or the beam is not reaching the sensor. Count from the square pad: GND, VCC, NC, SIGNAL. Then look into the slot for a label, dust or a burr over either window.
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 card's edge crossed the beam slowly, and the comparator, which has no hysteresis, flipped at each wobble. For a message this does no harm; the tachometer's sketch ignores edges that come too close together.
Edit this page — content/books/ir-speed-sensor/the-first-read.mdx
Questions about this product
See what other owners have asked, and read their solutions.
Infrared Speed 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.