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.
Practice
digitalRead(sensorPin) function do in an Arduino alarm system?Solution
Step 1: Understand digitalRead function
The function digitalRead(pin) checks the electrical state of the given pin, returning HIGH or LOW.Step 2: Apply to sensorPin
When used with sensorPin, it reads if the sensor is triggered (HIGH) or not (LOW).Final Answer:
It reads the current state (HIGH or LOW) of the sensor connected to sensorPin. -> Option CQuick Check:
digitalRead(sensorPin) = sensor state [OK]
- Confusing digitalRead with digitalWrite
- Thinking digitalRead sets pin state
- Assuming digitalRead controls buzzer
Solution
Step 1: Identify function to set pin output
digitalWrite(pin, HIGH) sets the pin to HIGH voltage, turning ON devices like buzzers.Step 2: Check pin number and value
Pin 9 is correct, and HIGH turns the buzzer ON.Final Answer:
digitalWrite(9, HIGH); -> Option AQuick Check:
digitalWrite(pin, HIGH) = turn ON output [OK]
- Using digitalRead instead of digitalWrite
- Setting pinMode to INPUT to turn ON buzzer
- Using analogWrite with LOW to turn ON buzzer
const int sensorPin = 2;
const int buzzerPin = 9;
void setup() {
pinMode(sensorPin, INPUT);
pinMode(buzzerPin, OUTPUT);
}
void loop() {
int sensorState = digitalRead(sensorPin);
if (sensorState == HIGH) {
digitalWrite(buzzerPin, HIGH);
} else {
digitalWrite(buzzerPin, LOW);
}
}Solution
Step 1: Analyze sensor reading
The code reads sensorPin state; if HIGH, it triggers the buzzer.Step 2: Check buzzer control logic
If sensorState is HIGH, buzzerPin is set HIGH (ON), else LOW (OFF).Final Answer:
The buzzer will sound only when the sensor detects HIGH signal. -> Option AQuick Check:
sensor HIGH -> buzzer ON, else OFF [OK]
- Assuming buzzer is always ON
- Thinking code has syntax errors
- Confusing sensor input with buzzer output
const int sensorPin = 3;
const int buzzerPin = 8;
void setup() {
pinMode(sensorPin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
}
void loop() {
if (digitalRead(sensorPin) == HIGH) {
digitalWrite(buzzerPin, HIGH);
} else {
digitalWrite(buzzerPin, LOW);
}
}Solution
Step 1: Check pinMode for sensorPin
sensorPin reads sensor input, so it must be INPUT, not OUTPUT.Step 2: Verify buzzerPin mode
buzzerPin controls buzzer output, so OUTPUT is correct.Final Answer:
sensorPin should be set as INPUT, not OUTPUT. -> Option BQuick Check:
Input pins need pinMode(INPUT) [OK]
- Setting sensorPin as OUTPUT
- Confusing input and output pin modes
- Misusing digitalWrite for sensor reading
Solution
Step 1: Understand requirement for continuous HIGH detection
We need to confirm sensorPin stays HIGH for 3 seconds before buzzer sounds.Step 2: Evaluate options for timing logic
Using a timer to measure continuous HIGH duration is correct; delay alone pauses code and is insufficient.Final Answer:
Use a timer to check if sensorPin stays HIGH for 3000 ms before activating buzzer. -> Option DQuick Check:
Continuous HIGH detection needs timer logic [OK]
- Using delay which blocks code execution
- Switching pin modes incorrectly
- Turning buzzer ON immediately without timing
Ready to prove it?
Pass the test and this pattern moves unknown
