An alarm system helps warn you when something happens, like detecting movement. Using a sensor and a buzzer, you can make a simple alert system.
Alarm system with sensor and buzzer in Arduino
const int sensorPin = 2; // Pin connected to sensor output const int buzzerPin = 9; // Pin connected to buzzer void setup() { pinMode(sensorPin, INPUT); // Set sensor pin as input pinMode(buzzerPin, OUTPUT); // Set buzzer pin as output } void loop() { int sensorState = digitalRead(sensorPin); // Read sensor if (sensorState == HIGH) { // If sensor detects something digitalWrite(buzzerPin, HIGH); // Turn buzzer on } else { digitalWrite(buzzerPin, LOW); // Turn buzzer off } }
sensorPin is where the sensor is connected.
buzzerPin controls the buzzer sound.
const int sensorPin = 3; const int buzzerPin = 8; void setup() { pinMode(sensorPin, INPUT); pinMode(buzzerPin, OUTPUT); } void loop() { if (digitalRead(sensorPin) == HIGH) { digitalWrite(buzzerPin, HIGH); } else { digitalWrite(buzzerPin, LOW); } }
const int sensorPin = 2; const int buzzerPin = 9; void setup() { pinMode(sensorPin, INPUT_PULLUP); // Use internal pull-up resistor pinMode(buzzerPin, OUTPUT); } void loop() { if (digitalRead(sensorPin) == LOW) { // Sensor active low digitalWrite(buzzerPin, HIGH); } else { digitalWrite(buzzerPin, LOW); } }
This program reads the sensor state every half second. If the sensor detects something (HIGH), it turns the buzzer on and prints "Alarm ON". Otherwise, it turns the buzzer off and prints "Alarm OFF". The serial monitor shows the sensor state and alarm status.
const int sensorPin = 2; const int buzzerPin = 9; void setup() { pinMode(sensorPin, INPUT); pinMode(buzzerPin, OUTPUT); Serial.begin(9600); } void loop() { int sensorState = digitalRead(sensorPin); Serial.print("Sensor state: "); Serial.println(sensorState); if (sensorState == HIGH) { digitalWrite(buzzerPin, HIGH); Serial.println("Alarm ON"); } else { digitalWrite(buzzerPin, LOW); Serial.println("Alarm OFF"); } delay(500); }
Make sure your sensor and buzzer are connected to the correct pins.
Use a resistor if your buzzer needs one to avoid damage.
Test the sensor separately to understand its output before connecting the buzzer.
An alarm system uses a sensor to detect events and a buzzer to alert you.
Use digitalRead() to check the sensor and digitalWrite() to control the buzzer.
Simple programs can help you learn how input and output work on Arduino.
