Complete the code to turn on an active buzzer connected to pin 8.
const int buzzerPin = 8; void setup() { pinMode(buzzerPin, OUTPUT); digitalWrite(buzzerPin, [1]); } void loop() { // Buzzer is on }
To turn on an active buzzer, you set the pin to HIGH.
Complete the code to generate a tone on a passive buzzer connected to pin 9.
const int buzzerPin = 9; void setup() { pinMode(buzzerPin, OUTPUT); } void loop() { tone(buzzerPin, [1]); delay(1000); noTone(buzzerPin); delay(1000); }
The tone() function needs a frequency in Hertz. 1000 Hz is a common tone frequency.
Fix the error in the code to stop the passive buzzer sound.
const int buzzerPin = 7; void setup() { pinMode(buzzerPin, OUTPUT); tone(buzzerPin, 2000); delay(500); [1](buzzerPin); } void loop() {}
To stop a tone on a passive buzzer, use noTone(pin).
Fill both blanks to create an array of structs that maps buzzer types to their control methods.
struct BuzzerControl {
const char* type;
const char* method;
};
BuzzerControl controls[2] = {
{ "active", [1] },
{ "passive", [2] }
};Active buzzers are controlled by digitalWrite(HIGH), passive buzzers by tone(frequency).
Fill all three blanks to complete the code that plays a 1kHz tone for 1 second on a passive buzzer, then stops it.
const int buzzerPin = 6; void setup() { pinMode(buzzerPin, OUTPUT); tone([1], [2]); delay(1000); [3](buzzerPin); } void loop() {}
The tone() function needs the pin and frequency, then noTone() stops the sound.
