Bird
0
0
Arduinoprogramming~10 mins

Alarm system with sensor and buzzer in Arduino - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Alarm system with sensor and buzzer
Start
Initialize pins
Read sensor value
Is sensor triggered?
NoWait and read again
Yes
Turn buzzer ON
Keep buzzer ON for a moment
Turn buzzer OFF
Repeat loop
The system starts by setting up pins, then continuously reads the sensor. If triggered, it turns the buzzer on briefly, then off, and repeats.
Execution Sample
Arduino
const int sensorPin = 2;
const int buzzerPin = 9;

void setup() {
  pinMode(sensorPin, INPUT);
  pinMode(buzzerPin, OUTPUT);
}

void loop() {
  int sensorValue = digitalRead(sensorPin);
  if (sensorValue == HIGH) {
    digitalWrite(buzzerPin, HIGH);
    delay(1000);
    digitalWrite(buzzerPin, LOW);
  }
}
This code reads a sensor on pin 2; if triggered (HIGH), it turns on a buzzer on pin 9 for 1 second.
Execution Table
StepActionSensor ValueCondition (sensorValue == HIGH)Buzzer StateDelay/Wait
1Setup pinsN/AN/ABuzzer OFFN/A
2Read sensorLOWFalseBuzzer OFFN/A
3Loop againN/AN/ABuzzer OFFN/A
4Read sensorHIGHTrueBuzzer ONStart 1000ms delay
5During delayHIGHTrueBuzzer ONContinuing delay
6After delayHIGHTrueBuzzer OFFDelay ended
7Loop againN/AN/ABuzzer OFFN/A
💡 The loop never exits; it continuously reads sensor and controls buzzer accordingly.
Variable Tracker
VariableStartAfter Step 2After Step 4After Step 6Final
sensorValueN/ALOWHIGHHIGHDepends on sensor
buzzerPin stateOFFOFFONOFFOFF or ON depending on sensor
Key Moments - 3 Insights
Why does the buzzer turn on only when sensorValue is HIGH?
Because the condition in the if statement (sensorValue == HIGH) is true only when the sensor detects something, as shown in execution_table row 4.
What happens during the delay(1000) call?
The program pauses for 1000 milliseconds (1 second) while the buzzer stays ON, as seen in execution_table rows 4 and 5.
Why does the loop keep running forever?
Because Arduino's loop() function repeats endlessly, continuously checking the sensor and controlling the buzzer, as shown by the repeated steps in the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the buzzer state at Step 5 during the delay?
ABuzzer OFF
BBuzzer ON
CBuzzer blinking
DBuzzer disconnected
💡 Hint
Check the 'Buzzer State' column at Step 5 in the execution_table.
At which step does the sensorValue first become HIGH?
AStep 4
BStep 6
CStep 2
DStep 7
💡 Hint
Look at the 'Sensor Value' column in the execution_table to find when it changes to HIGH.
If the sensor never detects anything (always LOW), what will the buzzer state be after Step 3?
ABlinking
BON
COFF
DRandom
💡 Hint
Refer to the 'Buzzer State' column in execution_table rows 2 and 3.
Concept Snapshot
Arduino alarm system:
- Setup sensor pin as INPUT
- Setup buzzer pin as OUTPUT
- In loop, read sensor
- If sensor HIGH, turn buzzer ON
- Keep buzzer ON for 1 second
- Then turn buzzer OFF
- Repeat forever
Full Transcript
This Arduino alarm system code starts by setting the sensor pin as input and the buzzer pin as output. In the loop, it reads the sensor value. If the sensor detects something (sensorValue is HIGH), it turns the buzzer on for one second, then turns it off. The loop repeats endlessly, continuously checking the sensor and controlling the buzzer. The execution table shows each step: reading sensor, checking condition, turning buzzer on or off, and waiting during delay. Variables like sensorValue and buzzer state change accordingly. Key points include understanding the if condition, the delay pause, and the infinite loop behavior.