Flashing Alarm

In this project we will build a flashing alarm using a buzzer block and an LED block. We will also use a non-blocking way to control flashing of LED and buzzer instead of using delay() function which blocks the execution of entire sketch. 

There are two types of buzzers, an active buzzer and a passive buzzer. An active buzzer has its own oscillator circuit to product constant sound when supplier with 5V. Passive buzzer requires external oscillating signal such as from microcontroller. We will use active buzzer in this project to create alarm sound.

Components

Connections

Connect the battery block Vs pin to the Vs pin of the microcontroller block. Connect any of black (negative) pins on the battery block to one of the negative (black) pin on the microcontroller block.

Connect the LED block red terminal to microcontroller block pin D2 with and connect the LED black terminal to the microcontroller block GND pin. Connect the buzzer block red terminal to microcontroller pin D4 and black terminal to the black GND terminal of battery block.

Code

We first define the pin D2 as led pin and D4 as buzzer pin. We then configure both the pins in output mode. We will use function millis() to track amount of time elapsed since start of the execution of the sketch and use it to measure time intervals. We can use these time intervals to perform actions like turning LED and buzzer on and off as needed.

/*
  This program is for flashing alarm using LED & buzzer
  We will use non blocking method to flash LED and turn buzzer on & off
  instead of using delay() function which stops execution of entire 
  sketch for the amount of time in delay function. We cannot do simultaneous
  actions using delay function.
  Makernova Robotics @2025
*/
const int Buzzer_Pin = 4;  // declare buzzer pin as D4
const int ledPin = 2;      // declare LED pin as D2

const int interval = 50;            // define interval time as 50ms
unsigned long buzzstartMillis = 0;  // variable to track buzzer on time
unsigned long ledstartMillis = 0;   // variable to track led on time

int count = 0;     // counter to track buzzer state
int ledCount = 0;  // counter to track led state

void setup() {
  // Initializing LED pin & Buzzer pin as output
  pinMode(ledPin, OUTPUT);
  pinMode(Buzzer_Pin, OUTPUT);
}

void loop() {
  // make buzzer on for a period of 20x50ms = 1 second
  if (millis() - buzzstartMillis < 20 * interval && count == 0) {
    digitalWrite(Buzzer_Pin, HIGH);
  }
  // once 1 second has elapsed, reset the buzzertime to current time
  // so that it can measure next time interval
  if (millis() - buzzstartMillis >= 20 * interval && count == 0) {
    buzzstartMillis = millis();
    count = 1;
  }
  // make buzzer on for a period of 10x50ms = 0.5 second
  if (millis() - buzzstartMillis < 10 * interval && count == 1) {
    digitalWrite(Buzzer_Pin, LOW);
  }
  // once 0.5 second has elapsed, reset the buzzertime to current time
  // so that it can measure next time interval
  if (millis() - buzzstartMillis >= 10 * interval && count == 1) {
    buzzstartMillis = millis();
    count = 0;
  }

  // make LED on for 50ms interval
  if (millis() - ledstartMillis < interval && ledCount == 0) {
    digitalWrite(ledPin, HIGH);
  }
  // after 50ms, reset the ledtime to current time so that it can
  // measure next time interval
  if (millis() - ledstartMillis >= interval && ledCount == 0) {
    ledstartMillis = millis();
    ledCount = 1;
  }
  // make LED off for 50ms interval
  if (millis() - ledstartMillis < interval && ledCount == 1) {
    digitalWrite(ledPin, LOW);
  }
  // after 50ms, reset the ledtime to current time so that it can
  // measure next time interval
  if (millis() - ledstartMillis >= interval && ledCount == 1) {
    ledstartMillis = millis();
    ledCount = 0;
  }
}