Author Options:

Alarm using RTC. Answered


I am new to arduino and RTC DS1307. I am trying to create an alarm using RTC and arduino.

I have a buzzer that I have connected to arduinio and also I have connected RTC module to it. I am programming the arduino in such a way that whn the timer runs off, it produces a sound from the buzzer. I am trying to use Time.h and TimeAlarms.h libraries. I dnt know how can I make the timer buzz the buzzer. 

This is the code I ve done so far. 

#include <Wire.h>
#include "RTClib.h"
#include <time.h>
#include <TimeAlarms.h>;


void setup () {
    pinMode(4, OUTPUT); // set a pin for buzzer output

  if (! RTC.isrunning()) {
    Serial.println("RTC is NOT running!");
    // following line sets the RTC to the date & time this sketch was compiled
    RTC.adjust(DateTime(__DATE__, __TIME__));


void loop () {

    DateTime now = RTC.now();

    Serial.print(now.year(), DEC);
    Serial.print(now.month(), DEC);
    Serial.print(now.day(), DEC);
    Serial.print(' ');
    Serial.print(now.hour(), DEC);
    Serial.print(now.minute(), DEC);
    Serial.print(now.second(), DEC);



    //Somthing sould come here to make the buzzer buzz
    soundBuzzer(); //sounds the buzzer
       buzz(4, 2500, 500); // buzz the buzzer on pin 4 at 2500Hz for 1000 milliseconds

// This is the function to make the buzzing
void soundBuzzer(){


//This is for the buzzer. It produces a beep.
void buzz(int targetPin, long frequency, long length) {
  long delayValue = 1000000/frequency/2; // calculate the delay value between transitions
  //// 1 second's worth of microseconds, divided by the frequency, then split in half since
  //// there are two phases to each cycle
  long numCycles = frequency * length/ 1000; // calculate the number of cycles for proper timing
  //// multiply frequency, which is really cycles per second, by the number of seconds to
  //// get the total number of cycles to produce
for (long i=0; i < numCycles; i++){ // for the calculated length of time...
    digitalWrite(targetPin,HIGH); // write the buzzer pin high to push out the diaphram
    delayMicroseconds(delayValue); // wait for the calculated delay value
    digitalWrite(targetPin,LOW); // write the buzzer pin low to pull back the diaphram
    delayMicroseconds(delayValue); // wait againf or the calculated delay value

I am really bad at programming so sorry for mistakes. But please I need help.


The forums are retiring in 2021 and are now closed for new topics and comments.