Buzzer Alarm
Press and hold the button to sound the alarm buzzer.
- Beginner
- Input & Controls
- 4 parts
Opens in your browser with the parts, wiring and code already in place. Press Run to watch it work, then change anything you like.
How it works
A push button connects 5 volts to pin 2, so pin 2 reads high while the button is held. A buzzer is on pin 8.
loop() checks the button every 100 milliseconds. While it is held, tone(8, 1000, 200) plays a 1000 Hz tone for 200 milliseconds. Because loop() comes round again after only 100 milliseconds, each new tone starts before the last one ends, so the sound is continuous, and the Serial tab prints “ALARM!”. When the button is released, noTone(8) silences the buzzer and the sketch prints “Armed”.
Parts
- Arduino Uno
- Push Button
- Buzzer
- GND
Wiring
- Arduino Uno 5V → Push Button A
- Push Button B → Arduino Uno D2
- Arduino Uno D8 → Buzzer +
- Buzzer - → GND
Arduino code
// Buzzer Alarm
void setup() {
pinMode(2, INPUT);
pinMode(8, OUTPUT);
Serial.begin(9600);
}
void loop() {
int pressed = digitalRead(2);
if (pressed == HIGH) {
tone(8, 1000, 200);
Serial.println("ALARM!");
} else {
noTone(8);
Serial.println("Armed");
}
delay(100);
}
Try changing it
- Change the pitch to 2000 Hz by editing tone(8, 1000, 200).
- Make it a beeping alarm instead of a steady tone: add delay(200); right after tone(8, 1000, 200); so every beep is followed by a gap.
Next challenge
More projects in Input & Controls
- Button Controlled LEDPress a button to turn on an LED.
- Potentiometer ReadingRead a potentiometer and display its value on the serial monitor.
- Interrupt Button CounterAn interrupt toggles the LED the instant you press the button, even while loop() is busy…
- Keypad Code LockType a 4-digit code on a keypad and press # to unlock: a servo opens the latch and an…