CYTHEIA AIOpen the editor

Buzzer Alarm

Press and hold the button to sound the alarm buzzer.

Open this circuit and run it

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

Wiring

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

Open this circuit and run it

Next challenge

More projects in Input & Controls