Toggle Nav

Interfacing MC-38 Magnetic Reed Switch with Arduino

Table of Contents

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.

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

Arduino UNO R3 × 1
MC-38 Magnetic Sensor Anti Theft Alarm × 1
Male to Female jumper wire × 1

Software Apps

Arduino IDE

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

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);


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.

Liked What you see?

Get updates and learn from the best

More To Explore

Leave a Reply

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