door sensor/Reading it/06. The first read
Reading it · 06 of 10

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.

Three wires and the sensor
Your board
VCC to
3V3
SIGNAL to
GPIO 4
In setup()
INPUT_PULLDOWN
GND to GND, VCC to 3V3, SIGNAL to GPIO 4, and pinMode(DOOR_PIN, INPUT_PULLDOWN) in setup(). Not the 5V pin: a closed sensor puts VCC straight onto GPIO 4, and the ESP32-S3's pins run at 3.3 V. GPIO 4 is not a strapping pin and has an internal pull-down, unlike GPIO 34 to 39 of the ESP32.

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_first_read.ino
/*
  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.13

When it does not work

The LED lights but the serial monitor never changes.

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.

Nothing happens, not even the LED.

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.

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 door open and door closed over and over.

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.

Where this goes next

What HIGH and LOW mean for your door, and how to check.

Which way is open →

Edit this page — content/books/door-sensor/the-first-read.mdx

Community

Questions about this product

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

Ask a question ↗

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.

Browse Modules and blocks on the forum →