The first read
Three wires, no library, and a sketch that prints each time a magnet arrives or leaves. VCC goes to your board's logic voltage, SIGNAL to the pin the Push Button and Hall Effect books use: GPIO 4 on an ESP32-S3, GPIO 25 on an ESP32, D2 on an Uno, GP15 on a Pico. pinMode is INPUT, because the pull-down is on the board.
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 and Hall Effect books' pins, so one wiring serves all three blocks. None of them is a strapping pin, which matters on this block: a magnet left on the glass at power-up holds the pin HIGH while the board starts, and without one the 10 kΩ holds it LOW. On an ESP32-S3 GPIO 0 that LOW beats the dev board's BOOT pull-up, to about 1.65 V, and the chip may start in its download mode instead of running your sketch.
The sketch
pinMode(REED_PIN, INPUT) and nothing else: the pull-down is on the board.
In loop(), digitalRead gives HIGH while the switch is closed and LOW
while it is open. 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.
What you should see
At 115200 the serial monitor prints no magnet: switch open once at the
start. Bring a magnet to the glass: the red LED lights and it prints
magnet: switch closed. Take it away and it prints no magnet: switch open
again.
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 instead of printing:
The code
No library. digitalRead returns HIGH while a magnet holds the switch closed and LOW while it is open; the sketch prints only when that changes. Change REED_PIN to the pin you wired SIGNAL to.
/*
Reed Switch - first read TK41 / /p/tk41
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: a closed switch puts
VCC straight onto the pin.
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 REED_PIN = 4;
int last = -1; // nothing read yet
void setup() {
Serial.begin(115200);
// INPUT, not INPUT_PULLUP: the board has its own 10k pull-down.
pinMode(REED_PIN, INPUT);
}
void loop() {
int level = digitalRead(REED_PIN);
if (level != last) {
if (level == HIGH) {
Serial.println("magnet: switch closed");
} else {
Serial.println("no magnet: switch open");
}
last = level;
}
delay(10);
}pinMode is INPUT: the board's own 10 kΩ pull-down holds SIGNAL LOW, and INPUT_PULLUP would fight it. The sketch compiles for an ESP32-S3 and an Uno.
View on GitHub · blocks/tk41-reed-switch/arduino/reed_first_read/reed_first_read.ino @ v1.0When it does not work
Then the switch, VCC and GND are fine and the level is not reaching the pin you read. Check SIGNAL is on the pin REED_PIN names, and that it is the right-hand pin, not NC beside it.
The switch never closed, or the block has no VCC or GND. Count from the square pad: GND, VCC, NC, SIGNAL. Then lay the magnet along the glass, flat against it, and try both ends.
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 contacts bounce as they meet, and a magnet held at the edge of its range can close and open as it wobbles. For a message this does no harm; for counting, debounce, as the door article does.
Edit this page — content/books/reed-switch/the-first-read.mdx
Questions about this product
See what other owners have asked, and read their solutions.
Reed Switch
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.