The first read
Three wires, no library, and a sketch that prints each time the door changes. 3V3 (VCC) goes to your board's logic voltage and SIGNAL to the pin the Push Button and Reed Switch books use: GPIO 4 on an ESP32-S3, GPIO 25 on an ESP32, D2 on an Uno, GP15 on a Pico. The one line that matters is the pull-down in setup().
Three wires
GND to GND. 3V3 (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 sensor's two wires go under the terminal's two screws, either way round.
The pins are the Push Button, Hall Effect and Reed Switch books' pins, so one wiring serves all of them. None is a strapping pin, so a door that is shut when the board powers up, and holds the pin HIGH, cannot change how the chip boots.
On an Uno the pin has no internal pull-down. Put a 10 kΩ resistor between SIGNAL and GND, on a breadboard, before you rely on a reading.
The sketch
setup() sets the pin to a pull-down, and nothing else is needed: the
board has no resistor of its own. In loop(), digitalRead gives HIGH
while the sensor 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 one line at the start, whichever state
the door is in: door closed (magnet near) or door open. Take the magnet
away from the sensor, and it prints door open. Bring it back and it prints
door closed (magnet near). The red LED on the board is lit exactly while
the door-closed line is the current one.
The LED tells you something the monitor cannot. If it is lit and the monitor says open, the sensor is closed and your pin is not reading it: check the pin number and the pull-down line.
The code
No library. digitalRead returns HIGH while the sensor is closed (magnet near) and LOW while it is open; the sketch prints only when that changes. Change DOOR_PIN to the pin you wired SIGNAL to.
/*
Door Sensor - first read TK107 / /p/tk107
Wiring. Count from the square pad on the TinkerBlock board, parts
up, header at the bottom:
GND -> GND
3V3 -> 3V3 on an ESP32, ESP32-S3 or Pico; 5V on an Uno.
Your board's logic voltage: this pin powers nothing,
it is the level a closed sensor puts on SIGNAL.
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
The sensor: its two wires go under the two screws of the terminal
block. Either way round; the sensor is a plain switch.
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 DOOR_PIN = 4;
int last = -1; // nothing read yet
void setup() {
Serial.begin(115200);
// The board has no pull-down and no pull-up. While the sensor is
// open, SIGNAL is connected to nothing and floats; a closed sensor
// puts VCC on it. So the pin must pull itself LOW: INPUT_PULLDOWN.
// INPUT_PULLUP would be wrong: open would read HIGH, closed would
// read HIGH, and the two could never be told apart.
#ifdef INPUT_PULLDOWN
pinMode(DOOR_PIN, INPUT_PULLDOWN);
#else
// An Uno or Nano has no internal pull-down. Fit a 10 kOhm resistor
// from SIGNAL to GND, or this pin will read noise while open.
pinMode(DOOR_PIN, INPUT);
#endif
}
void loop() {
int level = digitalRead(DOOR_PIN);
// The first pass always prints: that is the state at power-up.
if (level != last) {
if (level == HIGH) {
Serial.println("door closed (magnet near)");
} else {
Serial.println("door open");
}
last = level;
}
delay(10);
}The pull-down is the first thing setup() does: INPUT_PULLDOWN where the chip has one, and INPUT with a comment asking for 10 kΩ outside on an Uno. INPUT_PULLUP would be wrong.
View on GitHub · blocks/tk107-door-sensor/arduino/door_first_read/door_first_read.ino @ v1.13The same read in MicroPython, for an ESP32, an ESP32-S3 or a Pico. door.value() is 1 while the sensor is closed and 0 while it is open.
"""
Door Sensor - first read, MicroPython TK107 / /p/tk107
Wiring. Count from the square pad on the TinkerBlock board, parts
up, header at the bottom:
GND -> GND
3V3 -> 3V3 (your board's logic voltage: this pin powers
nothing, it is the level a closed sensor puts
on SIGNAL)
NC -> nothing (unconnected on the board)
SIGNAL -> GPIO 4 on an ESP32-S3, GPIO 25 on an ESP32,
GP15 on a Raspberry Pi Pico
The sensor: its two wires go under the two screws of the terminal
block. Either way round; the sensor is a plain switch.
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 SIGNAL is wired to.
# ESP32: 25. ESP32-S3: 4. Pico: 15.
DOOR_PIN = 4
# The board has no pull-down and no pull-up. While the sensor is
# open, SIGNAL is connected to nothing and floats; a closed sensor
# puts VCC on it. So the pin must pull itself LOW: Pin.PULL_DOWN.
# Pin.PULL_UP would be wrong: open would read 1, closed would read
# 1, and the two could never be told apart.
door = Pin(DOOR_PIN, Pin.IN, Pin.PULL_DOWN)
last = None # nothing read yet, so the first pass always prints
while True:
level = door.value()
if level != last:
if level:
print("door closed (magnet near)")
else:
print("door open")
last = level
time.sleep_ms(10)There is no Uno here: an Uno cannot run MicroPython. The pull-down is the pin's own: Pin.PULL_DOWN. Stop it with Ctrl-C.
View on GitHub · blocks/tk107-door-sensor/micropython/door_first_read.py @ v1.13When it does not work
Then the sensor, VCC and GND are fine and the level is not reaching the pin you read. Check SIGNAL is on the pin DOOR_PIN names, and that it is the right-hand header pin, not NC beside it.
The sensor never closed, or the board has no VCC or GND. Count from the square pad: GND, 3V3, NC, SIGNAL. Then hold the magnet against the sensor, flat against its face, and look again.
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.
Either the pull-down is missing, so the pin floats while the door is open, or the sensor's wire is loose under its screw. Check the sketch still has the pull-down line, then pull gently on each wire.
Edit this page — content/books/door-sensor/the-first-read.mdx
Questions about this product
See what other owners have asked, and read their solutions.
Door 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.