Cirkit Designer LogoCirkit DesignerYour all-in-one circuit design IDE Launch Cirkit Designer App

Wi-Fi Controlled Door Monitoring System with NodeMCU and Relay

View and Edit Project In Cirkit DesignerImage of Wi-Fi Controlled Door Monitoring System with NodeMCU and Relay

Circuit Documentation

Summary

This circuit is designed to control a relay based on the state of a magnetic door switch using a NodeMCU V3 ESP8266 microcontroller. The relay is activated when the door is closed and deactivated when the door is open. The circuit is powered by a 5V power supply unit (PSU).

Component List

  1. 1 Channel Relay 5V

    • Description: A relay module that can be controlled by a 5V signal to switch higher power loads.
    • Pins: VCC, GND, IN, NC, COM, NO
  2. NodeMCU V3 ESP8266

    • Description: A microcontroller with built-in Wi-Fi capabilities, used for controlling the relay and reading the state of the magnetic door switch.
    • Pins: A0, GND, VU, S3, S2, S1, SC, S0, SK, 3V3, EN, RST, Vin, D0, D1, D2, D3, D4, D5, D6, D7, D8, RX, TX
  3. 5V PSU

    • Description: A power supply unit that provides a stable 5V output to power the circuit.
    • Pins: 5V+, GND, 5V OK, PE, N, L
  4. Door MC-38

    • Description: A magnetic door switch used to detect the open or closed state of a door.
    • Pins: S, GND

Wiring Details

1 Channel Relay 5V

NodeMCU V3 ESP8266

5V PSU

Door MC-38

Code Documentation

const int relayPin = D6; // Relay In
const int magnet_switch = D7; // Magnet switch
const int debounceDelay = 50; // Debounce delay in milliseconds

void setup() {
  // initialize the relayPin pin as an output:
  pinMode(relayPin, OUTPUT);
  // initialize the magnetic switch pin as an input:
  pinMode(magnet_switch, INPUT_PULLUP);

  Serial.begin(9600);
}

void loop() {
  // Read the initial state of the magnetic switch
  int switchState = digitalRead(magnet_switch);
  
  // Wait for the debounce delay
  delay(debounceDelay);
  
  // Read the state of the magnetic switch again
  int switchStateDebounced = digitalRead(magnet_switch);
  
  // If the state is stable (no bouncing)
  if (switchState == switchStateDebounced) {
    if (switchState == LOW) {
      Serial.println("Switch Closed");
      digitalWrite(relayPin, HIGH); // Relay ON when door is closed
    } else {
      Serial.println("Switch Opened");
      digitalWrite(relayPin, LOW); // Relay OFF when door is opened
    }
  }
}

Code Explanation

setup()

loop()