MC-38 Magnetic Sensor Anti Theft Alarm
The MC38 is a magnetic switch. When a magnet (the wireless part of the sensor) approaches it, the sensor acts like a switch and the two pins are connected.
This sensor is used to detect closed or open doors, windows and alarm systems.
Note
Magnetic switches are available in two types, Normally Open and Normally Close. In this tutorial we use a Normally Open switch that closes as the magnet approaches the sensor (the two sensor pins are connected).
Required Materials
Hardware Components
Software Apps
Interfacing MC-38 Magnetic Sensor Anti Theft Alarm with Arduino
Step 1: Circuit
The following circuit shows how you should connect Arduino to MC-38 module. Connect wires accordingly.
Step 2: Code
Upload the following code to your Arduino. This code displays the status of the switch in the serial window.
/*
MC-38 Magnetic Sensor Anti-Theft Alarm
modified on 26 Sep 2020
by Mohammad Reza Akbari @ Electropeak
Home
*/
const int magnet_switch = 2; // Magnet switch
const int ledPin = 13; // LED pin
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(magnet_switch, INPUT_PULLUP);
Serial.begin(9600);
}
void loop() {
if (digitalRead(magnet_switch) == LOW) {
Serial.println("Switch Closed");
digitalWrite(ledPin, HIGH);
while (digitalRead(magnet_switch) == LOW) {}
}
else {
digitalWrite(ledPin, LOW);
}
}
After running the code, you will see the following image in the serial monitor.