reed switch/Reading it/06. The first read
Reading it · 06 of 9

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

Three wires
Your board
VCC to
3V3
SIGNAL to
GPIO 4
pinMode
INPUT
GND to GND, VCC to 3V3, SIGNAL to GPIO 4. Not the 5V pin: a closed switch puts VCC straight onto GPIO 4, and the ESP32-S3's pins run at 3.3 V. GPIO 4 is not a strapping pin, so a magnet left on the glass at power-up cannot change how the board starts.

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 TK41 on a LEGO baseplate, wired to an Uno-format board beside an XL LED block. A magnet held to the glass lights the TK41's red LED, and the sketch lights the XL LED.
The same two blocks with a Pico 2. The magnet at the glass lights the red LED on the TK41 and the XL LED with it.

The code

reed_first_read.ino

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.0

When it does not work

The LED lights but the serial monitor never changes.

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.

Nothing happens, not even the LED.

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.

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.

It prints closed and open several times for one magnet.

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.

Where this goes next

Why it lets go farther out than it closed.

Closes near, opens far →

Edit this page — content/books/reed-switch/the-first-read.mdx

Community

Questions about this product

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

Ask a question ↗

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.

Browse Modules and blocks on the forum →