Arduino(20): Use MC-38 reed switch to monitor the doors/windows open or close

By purchasing sensors and parts, I saw that the seller has a device that can detect the opening or closing of doors and windows. This is also a device commonly used by security companies to detect whether the doors and windows of homes have been opened. This device itself is an application of electromagnetic switches. Electromagnetic contact switches are reed switches housed in a plastic shell. They can be easily applied to doors, windows or drawers to detect whether the device is open or closed. The switch is divided into two parts: the switch itself (usually open) and the magnet. Let's also implement the application of this sensor.



[Material]

  • Arduino Uno x 1
  • MC38 reed switch x 1
  • LED red, green 1 each
  • 220K resistance x 3
  • Buzzer x 1
  • Connection line x N

[Wiring diagram]




[Code]

int ledGreen=10;
int beeper=9;
int ledRed=8;
int switchReed=6;

void setup(){
  pinMode(ledGreen, OUTPUT);
  pinMode(ledRed, OUTPUT);
  pinMode(beeper, OUTPUT);  
  pinMode(switchReed, INPUT);
  Serial.begin(9600);
}

void loop(){
  // Determine whether the electromagnetic switch is in contact: Yes
  if (digitalRead(switchReed)==HIGH){
    digitalWrite(ledRed, LOW);
    digitalWrite(ledGreen, HIGH);
    digitalWrite(beeper, LOW);    
    Serial.println("Window Closed");
  }
  else {
    digitalWrite(ledRed, HIGH);
    digitalWrite(ledGreen, LOW);
    digitalWrite(beeper, HIGH);
    Serial.println("Window Open");
  }
  delay(1);
}

[Result]



[Reference]


Post a Comment

Previous Post Next Post