Magnetic Contact Switch Sensor with Arduino.

Do you know how magnetic contact switch sensor works and how to use it?

Introduction

A reed switch is an electromagnetic switch used to control the flow of electricity in a circuit. They are made from two or more ferrous reeds encased within a small glass tube-like envelope, which become magnetized and move together or separate when a magnetic field is moved towards the switch. The switch effectively works like a gate, or a bridge, in an electric circuit so when the two reeds are in contact, electricity can flow around the circuit operating a device. Unlike mechanical switches they do not require something or someone to physically flick them on or off, they are controlled completely by invisible magnetic fields.

Video

This video will show you how magnetic contact switch sensor works and simple project that can be made using it.

Hardware Preparation

This is the hardware connection and the components needed.

Photo 2019 08 09 11 54 57
Figure 1 : Hardware connection.

Sample Code

Arduino

#define SENSOR 2
#define PIEZO 8
#define NOTE_G4 392
#define NOTE_C5 523
#define NOTE_G5 784
#define NOTE_C6 1047
int Sound[] = {NOTE_G5, NOTE_C6};
int SoundNoteDurations[] = {12, 8};
#define playSound() playMelody(Sound, SoundNoteDurations, 2)
char inChar;
String inString;
// variables will change:
int sensorState = 0; // variable for reading the sensor status
void setup() {
pinMode(PIEZO, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(SENSOR, INPUT_PULLUP);
}
void loop() {
// read the state of the pushbutton value:
sensorState = digitalRead(SENSOR);
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (sensorState == HIGH) {
playSound();
} else {
noTone(PIEZO);
}
}
void playMelody(int *melody, int *noteDurations, int notesLength)
{
pinMode(PIEZO, OUTPUT);
for (int thisNote = 0; thisNote < notesLength; thisNote++) {
int noteDuration = 1000 / noteDurations[thisNote];
tone(PIEZO, melody[thisNote], noteDuration);
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
noTone(PIEZO);
}
}
view raw Magnetic_Sensor hosted with ❤ by GitHub

References :

  1. What is a reed switch and which magnets operate them?

Thank you

Thank you for reading this tutorial and we hope it helps your project development. If you have any technical inquiry, please post at Cytron Technical Forum.

1 thought on “Magnetic Contact Switch Sensor with Arduino.”

Leave a Comment

Your email address will not be published.

Share this Tutorial

Share on facebook
Share on whatsapp
Share on email
Share on print
Share on twitter
Share on pinterest
Share on facebook
Share on whatsapp
Share on email
Share on print
Share on twitter
Share on pinterest

Latest Tutorial

Object Sense With Servo Using Maker Pi RP2040
Build Otto DIY Robot Using Maker Pi RP2040
Raspberry Pi RP2040 vs STM32F1 vs SAMD21G18 vs ESP32-S2
The Easiest Way to Print Temperature Tower Using Cura 4.9.1 Plugins
IoT on Raspberry Pi Pico using CircuitPython and Adafruit IO
Tutorials of Cytron Technologies Scroll to Top