Bird
0
0
Arduinoprogramming~20 mins

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

Choose your learning style9 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.