Bird
0
0
Arduinoprogramming~30 mins

Alarm system with sensor and buzzer in Arduino - Mini Project: Build & Apply

Choose your learning style9 modes available
Alarm system with sensor and buzzer
📖 Scenario: You want to build a simple alarm system using an Arduino board. The system will detect when a sensor is triggered and then activate a buzzer to alert you.
🎯 Goal: Create an Arduino program that reads a sensor input and turns on a buzzer when the sensor is triggered.
📋 What You'll Learn
Use a variable called sensorPin set to pin 2 for the sensor input
Use a variable called buzzerPin set to pin 9 for the buzzer output
Create a variable called sensorState to store the sensor reading
Write code to read the sensor state using digitalRead(sensorPin)
Turn on the buzzer by setting buzzerPin HIGH when the sensor is triggered
Turn off the buzzer by setting buzzerPin LOW when the sensor is not triggered
Print "Alarm ON" when the buzzer is on and "Alarm OFF" when it is off
💡 Why This Matters
🌍 Real World
Alarm systems are used in homes and offices to detect intruders or unsafe conditions and alert people.
💼 Career
Understanding how to read sensors and control outputs is a key skill for embedded systems and IoT device programming.
Progress0 / 4 steps
1
Set up sensor and buzzer pins
Create two integer variables called sensorPin and buzzerPin. Set sensorPin to 2 and buzzerPin to 9.
Arduino
Hint

Use int to declare the pins as numbers.

2
Configure pins in setup
Write the setup() function. Inside it, set sensorPin as input and buzzerPin as output using pinMode().
Arduino
Hint

Use pinMode(pin, mode) to set pin modes.

3
Read sensor and control buzzer
In the loop() function, create an integer variable called sensorState and set it to the value read from sensorPin using digitalRead(sensorPin). Then use an if statement to turn the buzzer on by writing HIGH to buzzerPin if sensorState is HIGH, otherwise turn it off by writing LOW.
Arduino
Hint

Use digitalRead() to get sensor value and digitalWrite() to control buzzer.

4
Print alarm status
Inside the loop() function, after controlling the buzzer, add if statements to print "Alarm ON" when sensorState is HIGH and "Alarm OFF" when it is LOW using Serial.println(). Also, add Serial.begin(9600); in the setup() function.
Arduino
Hint

Use Serial.begin(9600); in setup() and Serial.println() to print messages.