Bird
Raised Fist0
Arduinoprogramming~20 mins

Alarm system with sensor and buzzer in Arduino - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Alarm System Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this Arduino alarm system code?

Consider this Arduino code snippet for an alarm system using a sensor and buzzer. What will the buzzer do when the sensor reads a value above 500?

Arduino
const int sensorPin = A0;
const int buzzerPin = 9;

void setup() {
  pinMode(buzzerPin, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  int sensorValue = analogRead(sensorPin);
  Serial.println(sensorValue);
  if (sensorValue > 500) {
    digitalWrite(buzzerPin, HIGH);
  } else {
    digitalWrite(buzzerPin, LOW);
  }
  delay(1000);
}
AThe buzzer will never sound because pinMode for buzzerPin is missing.
BThe buzzer will beep once every second regardless of sensor value.
CThe buzzer will sound only once when sensor value first goes above 500, then stay silent.
DThe buzzer will sound continuously when sensor value is above 500, and be silent otherwise.
Attempts:
2 left
💡 Hint

Look at how digitalWrite is used inside the if condition.

🧠 Conceptual
intermediate
1:00remaining
Which component is responsible for detecting the environment change in this alarm system?

In an alarm system with a sensor and buzzer, which part detects changes like motion or light?

AThe sensor
BThe buzzer
CThe Arduino board
DThe power supply
Attempts:
2 left
💡 Hint

Think about which part senses something outside.

🔧 Debug
advanced
2:00remaining
What error will this Arduino code cause?

Examine this code snippet for an alarm system. What error will occur when compiling?

Arduino
const int sensorPin = A0;
const int buzzerPin = 9;

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

void loop() {
  int sensorValue = analogRead(sensorPin);
  if (sensorValue > 400) {
    digitalWrite(buzzerPin, HIGH);
  } else {
    digitalWrite(buzzerPin, LOW);
  }
  delay(500);
}
ALogicalError: buzzer never turns off
BRuntimeError: buzzerPin not initialized
CSyntaxError: Missing semicolon after buzzerPin declaration
DNo error, code compiles and runs fine
Attempts:
2 left
💡 Hint

Check the line where buzzerPin is declared.

📝 Syntax
advanced
1:00remaining
Which option correctly initializes the buzzer pin as output?

Choose the correct Arduino code line to set the buzzer pin as an output in setup().

ApinMode(buzzerPin, INPUT);
BpinMode(buzzerPin, OUTPUT);
CdigitalWrite(buzzerPin, OUTPUT);
DdigitalWrite(buzzerPin, HIGH);
Attempts:
2 left
💡 Hint

Remember, pinMode sets pin direction.

🚀 Application
expert
3:00remaining
How many times will the buzzer beep in this code?

Given this Arduino code, how many times will the buzzer beep during the loop() execution?

Arduino
const int sensorPin = A0;
const int buzzerPin = 9;

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

void loop() {
  int sensorValue = analogRead(sensorPin);
  if (sensorValue > 600) {
    for (int i = 0; i < 3; i++) {
      digitalWrite(buzzerPin, HIGH);
      delay(200);
      digitalWrite(buzzerPin, LOW);
      delay(200);
    }
  }
  delay(1000);
}
A3 times
BContinuously without stopping
C1 time
D0 times
Attempts:
2 left
💡 Hint

Look at the for loop inside the if condition.

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

  1. Step 1: Understand digitalRead function

    The function digitalRead(pin) checks the electrical state of the given pin, returning HIGH or LOW.
  2. Step 2: Apply to sensorPin

    When used with sensorPin, it reads if the sensor is triggered (HIGH) or not (LOW).
  3. Final Answer:

    It reads the current state (HIGH or LOW) of the sensor connected to sensorPin. -> Option C
  4. 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

  1. Step 1: Identify function to set pin output

    digitalWrite(pin, HIGH) sets the pin to HIGH voltage, turning ON devices like buzzers.
  2. Step 2: Check pin number and value

    Pin 9 is correct, and HIGH turns the buzzer ON.
  3. Final Answer:

    digitalWrite(9, HIGH); -> Option A
  4. 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

  1. Step 1: Analyze sensor reading

    The code reads sensorPin state; if HIGH, it triggers the buzzer.
  2. Step 2: Check buzzer control logic

    If sensorState is HIGH, buzzerPin is set HIGH (ON), else LOW (OFF).
  3. Final Answer:

    The buzzer will sound only when the sensor detects HIGH signal. -> Option A
  4. 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:
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);
  }
}
medium
A. digitalWrite should use LOW to turn buzzer ON.
B. sensorPin should be set as INPUT, not OUTPUT.
C. digitalRead cannot be used on sensorPin.
D. buzzerPin should be set as INPUT, not OUTPUT.

Solution

  1. Step 1: Check pinMode for sensorPin

    sensorPin reads sensor input, so it must be INPUT, not OUTPUT.
  2. Step 2: Verify buzzerPin mode

    buzzerPin controls buzzer output, so OUTPUT is correct.
  3. Final Answer:

    sensorPin should be set as INPUT, not OUTPUT. -> Option B
  4. 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

  1. Step 1: Understand requirement for continuous HIGH detection

    We need to confirm sensorPin stays HIGH for 3 seconds before buzzer sounds.
  2. Step 2: Evaluate options for timing logic

    Using a timer to measure continuous HIGH duration is correct; delay alone pauses code and is insufficient.
  3. Final Answer:

    Use a timer to check if sensorPin stays HIGH for 3000 ms before activating buzzer. -> Option D
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard