Magnetic Contact Switch Sensor for Arduino Wiki - DFRobot

Introduction

This magnetic switch (reed switch) consists of two magnetic reeds (usually composed of iron and nickel) sealed in a plastic case. The two magnetic reeds are overlapped but there is a small gap between them. When a suitable external magnetic field comes within 2cm, the two magnetic reeds will contact and conduct, thereby sensing the change in the position of the object. This magnetic control switch has a compact structure and a small weight, which can be installed in a limited space, and is extremely suitable for using in miniaturized equipment. The switching elements of the reed switch are hermetically sealed in an inert gas atmosphere, so that they are never exposed to the external environment and feature a long service life. The switch can be widely used in door/window position limiting device, proximity switches, measuring instruments, automation/security/transportation equipment, smart homes, etc.

Features

Specification

Tutorial

Requirements

Connection Diagram

Here we use the I/O9 and 13 ports of Arduino UNO for demonstration

Sample Code

 * @copyright  Copyright (c) 2010 DFRobot Co.Ltd (http://www.dfrobot.com)
 * @licence  The MIT License (MIT)
 * @author  [huyujie](yujie.hu@dfrobot.com)
 * @version  V1.0
 * @date  2021-05-13
 */
int ledPin = 13;
int magnet_inputPin = 9;

    void setup() {
      pinMode(ledPin, OUTPUT);
      pinMode(magnet_inputPin, INPUT);
    }

    void loop(){
      int val = digitalRead(magnet_inputPin);
      if (val == HIGH) {
         digitalWrite(ledPin, HIGH);
      } else {
         digitalWrite(ledPin, LOW);
      }
    }

Expected Results

When the magnetic switch is close to the magnet, the LED of UNO 13 pin will light up. And the LED will light off when the magnetic switch moves away from the magnet.

FAQ

For any questions, advice or cool ideas to share, please visit the DFRobot Forum.

More Documents