Simple Alarm System Using Maker Nano

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

Introduction

Security alarms are systems designed to detect intrusion into buildings or other areas such as homes or schools. Security alarms are used for protection against theft or damage to property, as well as personal protection against intruders.

Video

This video will show you how we make a simple alarm system using magnetic sensor on Maker Nano.

Hardware Preparation

This is the hardware connection and the components needed.

Sample Code

This is the sample code using Arduino IDE.

#define SENSOR 3
#define PIEZO 8
#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);
pinMode(SENSOR, INPUT_PULLUP);
}
void loop() {
// read the state of the Magnetic Sensor value:
sensorState = digitalRead(SENSOR);
// check if the pushbutton is pressed. If it is, the Magnetic Sensor 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
simple_alarm.ino
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.

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

Raspberry Pi Pico VS Arduino MKRZero
Raspberry Pi Pico VS Teensy LC
A New Challenger in MCU Platform – Raspberry Pi Pico
Raspberry Pi Pico VS Arduino UNO
Raspberry Pi Pico VS micro:bit V2
Scroll to Top