📖 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.
Practice
(1/5)
1. What does the digitalRead(sensorPin) function do in an Arduino alarm system?
easy
A. It turns on the buzzer connected to sensorPin.
B. It sets the sensorPin to HIGH to activate the sensor.
C. It reads the current state (HIGH or LOW) of the sensor connected to sensorPin.
D. It resets the Arduino board.
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 C
Quick Check:
digitalRead(sensorPin) = sensor state [OK]
Hint: digitalRead reads input pin state, not output [OK]
Common Mistakes:
Confusing digitalRead with digitalWrite
Thinking digitalRead sets pin state
Assuming digitalRead controls buzzer
2. Which of the following is the correct way to turn ON a buzzer connected to pin 9 in Arduino?
easy
A. digitalWrite(9, HIGH);
B. pinMode(9, INPUT);
C. digitalRead(9);
D. analogWrite(9, LOW);
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 A
Quick Check:
digitalWrite(pin, HIGH) = turn ON output [OK]
Hint: Use digitalWrite(pin, HIGH) to turn ON output devices [OK]
Common Mistakes:
Using digitalRead instead of digitalWrite
Setting pinMode to INPUT to turn ON buzzer
Using analogWrite with LOW to turn ON buzzer
3. What will be the output behavior of this Arduino code snippet?
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);
}
}
medium
A. The buzzer will sound only when the sensor detects HIGH signal.
B. The buzzer will always be ON regardless of sensor state.
C. The buzzer will never turn ON.
D. The code will cause a compilation error.
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 A
Quick Check:
sensor HIGH -> buzzer ON, else OFF [OK]
Hint: If sensor reads HIGH, buzzer turns ON [OK]
Common Mistakes:
Assuming buzzer is always ON
Thinking code has syntax errors
Confusing sensor input with buzzer output
4. Identify the error in this Arduino alarm code snippet:
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 B
Quick Check:
Input pins need pinMode(INPUT) [OK]
Hint: Set sensor pins as INPUT, buzzer pins as OUTPUT [OK]
Common Mistakes:
Setting sensorPin as OUTPUT
Confusing input and output pin modes
Misusing digitalWrite for sensor reading
5. You want to modify an Arduino alarm system so the buzzer sounds only if the sensor detects HIGH for 3 seconds continuously. Which code change correctly implements this?
hard
A. Add a delay(3000) after detecting HIGH before turning buzzer ON.
B. Set sensorPin as OUTPUT and buzzerPin as INPUT to control timing.
C. Turn buzzer ON immediately when sensorPin is HIGH, then delay 3000 ms.
D. Use a timer to check if sensorPin stays HIGH for 3000 ms before activating buzzer.
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 D
Quick Check:
Continuous HIGH detection needs timer logic [OK]
Hint: Use timer logic, not delay, for continuous sensor check [OK]
Common Mistakes:
Using delay which blocks code execution
Switching pin modes incorrectly
Turning buzzer ON immediately without timing
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.