Arduino Door Buzzer Alarm

MechatronicsLAB
3 min readJun 7, 2021

--

This project adds an LED and a buzzer to the previous How Test Reed Switch in Arduino |The Ultimate Beginner’s Guide project. If the reed switch detects a magnetic field, the LED and buzzer activate and disable if the magnetic field is no longer present. This example could easily be adjusted for a simple door alarm that activates when a magnet on the door framework is disconnected and another magnet on the door itself indicates someone has opened the door.

Parts List

if you want to complete Arduino Door Buzzer Alarm tutorial, you have to need the following component

  1. Arduino — — — amazon/ banggood/ aliexpress
  2. reed switch — — — amazon/ banggood/ aliexpress
  3. LED — — — — — amazon/ banggood/ aliexpress
  4. Piezo Buzzer — — — — — amazon/ banggood/ aliexpress
  5. Breadboard — — — — amazon/ banggood/ aliexpress
  6. Connecting Wires — amazon/ banggood/ aliexpress

Schematic Diagram

Arduino Door Buzzer Alarm
Arduino Door Buzzer Alarm

Arduino — — — — Reed switch Connection

3.3-V — — — — -VCC
GND — — — — GND
Digital pin 7 — — — D0 pin

Arduino — — — — LED

GND — — — — negative terminal
Digital pin 8 — — — positive terminal

Arduino — — — — buzzer

GND — — — — negative terminal
Digital pin 9 — — — positive terminal

Programming

int ReedSwitchPin = 7;
int RawValue = 0;

// LED
int LEDPin = 8;
int LEDState = 0;

// Buzzer
int BuzzerPin = 9;
int BuzzerFreq = 300;

void setup()
{
pinMode(ReedSwitchPin, INPUT);
pinMode(LEDPin, OUTPUT);
Serial.begin(9600);
Serial.println(“Reed Switch Door Buzzer ….”);
}

void loop()
{
RawValue = digitalRead(ReedSwitchPin);
if (RawValue == 0)
{
Serial.print(“Reed Switch Detects Magnet … “);
LEDState = 1;
tone(BuzzerPin, BuzzerFreq);
}
else
{
Serial.print(“No Magnet Detected …”);
LEDState = 0;
noTone(BuzzerPin);
}

// 1 is off, 0 is activated by magnet
Serial.print(“ , RawValue: “);
Serial.println(RawValue);

// Control LED
digitalWrite(LEDPin,LEDState);
}

Upload your Arduino program and start the Serial Monitor. The output from the Arduino to the serial monitor should be the same as before, but the buzzer and the LED should be activated when the magnet is near the reed switch and remained activated while the magnet is detected. Pull the magnet away from the reed switch and turn off the LED and buzzer.

Program Analysis

int ReedSwitchPin = 7;

Assigning the D0 pin on the reed switch to the Arduino digital pin 7 to output the state of the magnetic field detection.

int RawValue = 0;

Initialization of RawValue, which holds the read value from the reed to 0.

int LEDPin = 8;

Pin 8 on the Arduino has been assigned to the positive terminal of the LED

int LEDState = 0;

The on/off state of the LED has been initialized to off.

int BuzzerPin = 9;

Pin 9 on the Arduino has been assigned to the positive terminal of the buzzer.

int BuzzerFreq = 300;

The frequency of the tone that the buzzer will produce is set to 300 hertz.

void setup()

pinMode(ReedSwitchPin, INPUT);

pinMode(LEDPin, OUTPUT);

Serial.begin(9600);

Serial.println(“Reed Switch Door Buzzer ….”);

In the setup() function, the pin connected to the LED has been designated as an OUTPUT pin so that it can provide a voltage, and the initialization message has been updated.

void loop()

In the main loop() function:

RawValue = digitalRead(ReedSwitchPin);

if (RawValue == 0)

Serial.print(“Reed Switch Detects Magnet … “);

LEDState = 1;

tone(BuzzerPin, BuzzerFreq);

Serial.print(“No Magnet Detected …”);

LEDState = 0;

noTone(BuzzerPin);

If the reed switch detects a magnetic field, the LED state is set to on, and a tone is generated using the buzzer.

Serial.print(“ , RawValue: “);

Serial.println(RawValue);

digitalWrite(LEDPin,LEDState);

The LED is turned on or off based on the LEDState variable

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

MechatronicsLAB
MechatronicsLAB

Written by MechatronicsLAB

0 Followers

Our main focus will be on Mechatronics Technology where we will work in #Industrial_Automation, #PLC, #IoT, #Raspberry_Pi, #Arduino, #Microcontroller

No responses yet

Write a response