Door Sensor with Relay – Arduino Tutorial

1,869 views

Introduction

Controlling a 5V SPDT relay module with an MC-38 magnetic reed switch using an Arduino Uno microcontroller is a simple yet powerful way to automate various tasks and control electrical devices remotely. With the help of this technology, one can easily switch on or off an AC appliance, control lighting, or monitor the status of a device. The magnetic reed switch acts as a sensor that detects the presence of a magnet and sends a signal to the Arduino board to control the state of the relay.

The Arduino board is programmed to read the state of the magnetic reed switch using the digitalRead() function, and then control the state of the relay using the digitalWrite() function. The state of the relay can be monitored using the serial monitor on the Arduino IDE.

Hardware Components

You will require the following hardware for Door Sensor Relay with Arduino.

S.noComponentValueQty
1.Arduino UNO1
2.USB Cable Type A to B1
3.Door Sensor1
4.Relay1
5.Warning Light Bright Waterproof1
6.Power Adapter12V1
7.DC Power Jack1
8.Power Adapter for Arduino9V1
9.Jumper Wires1

Door Sensor Relay with Arduino

  1. First, connect the relay module and the magnetic reed switch to the Arduino board according to the following connections:
  • Connect the relay module’s IN1 pin to digital pin D2 on the Arduino board.
  • Connect one end of the magnetic reed switch to digital pin D3 on the Arduino board.
  • Connect the other end of the magnetic reed switch to the GND pin on the Arduino board.
  1. Next, open the Arduino IDE and start a new sketch.
  2. In the sketch, define the pin number of the relay and magnetic reed switch:
const int relayPin = 2;
const int reedSwitchPin = 3;
  1. In the setup() function, initialize the serial monitor and set the pin modes of the relay and magnetic reed switch as OUTPUT and INPUT respectively:
void setup() {
  pinMode(relayPin, OUTPUT);
  pinMode(reedSwitchPin, INPUT);
  Serial.begin(9600);
}
  1. In the loop() function, use an if-else statement to check the state of the magnetic reed switch. If the switch is closed, turn the relay on and print the status “Relay ON” to the serial monitor. If the switch is open, turn the relay off and print the status “Relay OFF” to the serial monitor:
void loop() {
  if (digitalRead(reedSwitchPin) == LOW) {
    digitalWrite(relayPin, HIGH);
    Serial.println("Relay ON");
  } else {
    digitalWrite(relayPin, LOW);
    Serial.println("Relay OFF");
  }
}
  1. Finally, upload the code to the Arduino board and open the serial monitor to see the status of the relay. When the magnetic reed switch is closed, the relay should turn on and the status “Relay ON” should be displayed on the serial monitor. When the magnetic reed switch is open, the relay should turn off and the status “Relay OFF” should be displayed on the serial monitor.

Schematic

Make connections according to the circuit diagram given below.

Door-Sensor-Relay-Circuit

Wiring / Connections

ArduinoDoor SensorRelay
5VVCC
GNDGNDGND
D3INP
D13GIOP

Installing Arduino IDE

First, you need to install Arduino IDE Software from its official website Arduino. Here is a simple step-by-step guide on “How to install Arduino IDE“.

Code

Now copy the following code and upload it to Arduino IDE Software.

const int DOOR_SENSOR_PIN = 13; // Arduino pin connected to door sensor's pin
const int RELAY_PIN       = 3;  // Arduino pin connected to the IN pin of relay

int doorState;

void setup() {
  Serial.begin(9600);                     // initialize serial
  pinMode(DOOR_SENSOR_PIN, INPUT_PULLUP); // set arduino pin to input pull-up mode
  pinMode(RELAY_PIN, OUTPUT);             // set arduino pin to output mode
}

void loop() {
  doorState = digitalRead(DOOR_SENSOR_PIN); // read state

  if (doorState == HIGH) {
    Serial.println("The door is open");
    digitalWrite(RELAY_PIN, HIGH); // turn on relay
  } else {
    Serial.println("The door is closed");
    digitalWrite(RELAY_PIN, LOW);  // turn off relay
  }
}

Working Explanation

The Arduino board is programmed to read the state of the magnetic reed switch connected to it, which is done by using the digitalRead() function. The digitalRead() function reads the digital value of a given pin, in this case, the pin connected to the magnetic reed switch. If the magnetic reed switch is closed, it sends a signal to the Arduino board and the digitalRead() function returns a low value. If the magnetic reed switch is open, it does not send a signal and the digitalRead() function returns a high value.

Based on the value returned by the digitalRead() function, the Arduino board is programmed to control the state of the relay connected to it, this is done by using the digitalWrite() function. The digitalWrite() function sets the digital value of a given pin, in this case, the pin connected to the relay. If the digitalRead() function returns a low value, indicating that the magnetic reed switch is closed, the digitalWrite() function sets the value of the relay pin to high, turning the relay on. If the digitalRead() function returns a high value, indicating that the magnetic reed switch is open, the digitalWrite() function sets the value of the relay pin to low, turning the relay off.

Applications

  • Home Automation
  • Industrial Control
  • Building Automation
  • Remote Control of AC Appliances
  • Automatic Lighting Control
  • Security Systems
  • Robotics and Automated systems

Conclusion.

We hope you have found this Door Sensor Relay Circuit very useful. If you feel any difficulty in making it feel free to ask anything in the comment section.