Arduino Uno reed switch D2 to ground with internal pull-up

In this easy CircuitMagic tutorial you will build arduino door open detection with a magnetic reed switch using Arduino Uno and one simple sensor. The project needs only a few connections and a short Arduino IDE sketch.

What does the sensor do?

A reed switch is operated by a magnet. A common normally-open reed switch closes when the magnet is brought close. Arduino’s internal pull-up keeps D2 HIGH while the contacts are open.

Required components

Arduino Uno, normally-open reed switch, small magnet, jumper wires, USB cable.

Connection diagram

The diagram below is a pin-to-pin connection map: each colored line shows one separate wire between labeled terminals, not a photograph of a particular board’s header layout.

magnetic reed switch wiring diagram for Arduino Uno

Pin connections

Unplug USB power before wiring. The pin table is authoritative; always verify your board’s printed pin labels and your module’s pinout.

Arduino Uno pin / connectiontwo-wire magnetic reed switch connection
D2One reed switch lead
GNDOther reed switch lead

Arduino IDE setup

No additional library needed. Select the matching board and serial port, then upload this sketch.

Complete Arduino code

const int doorPin = 2;
void setup() {
  Serial.begin(9600);
  pinMode(doorPin, INPUT_PULLUP);
}
void loop() {
  bool closed = digitalRead(doorPin) == LOW;
  Serial.println(closed ? "Door closed (magnet near)" : "Door open (magnet away)");
  delay(250);
}

Expected output

Magnet near: Door closed. Magnet away: Door open. Open Serial Monitor at the baud rate specified by Serial.begin().

Troubleshooting

The physical orientation of the switch and magnet matters. Some packaged door contacts are normally-closed; if yours behaves in reverse, adjust the message logic.

If there is no output, double-check the board and port, USB data cable, common ground, sensor pin labels and Serial Monitor speed.

Try next

Once the basic example works, experiment with the sampling delay, add an LED indicator, or log the sensor output to a computer. See more CircuitMagic electronics projects for related beginner tutorials.

0 Comments

Leave a reply

Your email address will not be published. Required fields are marked *

*

or

Log in with your credentials

Forgot your details?