How to Make an Arduino Alarm Clock

by Jelmer Keijzer in Circuits > Arduino

703 Views, 4 Favorites, 0 Comments

How to Make an Arduino Alarm Clock

5791f523-fc69-4eba-aa53-7c879a39ec34.jpg

This year I had to make something with an arduino for a school project. It was quite difficult to decide what I wanted to make. I decided that I wanted something that isn't too hard to build and something that is expandable. If it turns out that my initial idea was too easy, I can easily add more ideas to the build. An alarm clock is perfect for this.

Supplies

9aa73247-8b52-47dd-87d9-3ad51ae73cf5.jpg

The tools/items I used are:

-Arduino Uno

-Lcd keypad shield

-Speaker/buzzer

-2 Jumper wires (female to male)

-Arduino IDE

Optional:

-Soldering iron

-Wood

-Bolts and nuts

-A hot glue gun

The Circuitry

cc5ac2cd-7855-464f-9bcd-90f8a90beb88.jpg

First, you'll have to install the LCD keypad shield onto the arduino uno. Fortunately, this is really easy. You locate the far right pin of the shield, that pin goes into the far right hole of the arduino. After that, you can grab your two wires and put the two pins from your buzzer or speaker in the female sides of the wires. These wires connect to digital pin 2 and one of the ground pin. It doesn't matter which one of the wires goes to ground or digital pin 2.

Installing the Libraries

WhatsApp Image 2023-02-03 at 15.12.39.jpeg

Download the zip folder for the library here. You only need the LCDKeypad.zip file and not the source code. To install this, go to Sketch>Include Library>Add .zip Library in the arduino IDE software and find the downloaded file (usually in your downloads folder).

The Code

#include <LiquidCrystal.h>
#include <LCDKeypad.h>
 
byte Bell[8] = {
  B00000,
  B00100,
  B01110,
  B01110,
  B01110,
  B11111,
  B00000,
  B00100
};


#define NOTE_B0  31
#define NOTE_C1  33
#define NOTE_CS1 35
#define NOTE_D1  37
#define NOTE_DS1 39
#define NOTE_E1  41
#define NOTE_F1  44
#define NOTE_FS1 46
#define NOTE_G1  49
#define NOTE_GS1 52
#define NOTE_A1  55
#define NOTE_AS1 58
#define NOTE_B1  62
#define NOTE_C2  65
#define NOTE_CS2 69
#define NOTE_D2  73
#define NOTE_DS2 78
#define NOTE_E2  82
#define NOTE_F2  87
#define NOTE_FS2 93
#define NOTE_G2  98
#define NOTE_GS2 104
#define NOTE_A2  110
#define NOTE_AS2 117
#define NOTE_B2  123
#define NOTE_C3  131
#define NOTE_CS3 139
#define NOTE_D3  147
#define NOTE_DS3 156
#define NOTE_E3  165
#define NOTE_F3  175
#define NOTE_FS3 185
#define NOTE_G3  196
#define NOTE_GS3 208
#define NOTE_A3  220
#define NOTE_AS3 233
#define NOTE_B3  247
#define NOTE_C4  262
#define NOTE_CS4 277
#define NOTE_D4  294
#define NOTE_DS4 311
#define NOTE_E4  330
#define NOTE_F4  349
#define NOTE_FS4 370
#define NOTE_G4  392
#define NOTE_GS4 415
#define NOTE_A4  440
#define NOTE_AS4 466
#define NOTE_B4  494
#define NOTE_C5  523
#define NOTE_CS5 554
#define NOTE_D5  587
#define NOTE_DS5 622
#define NOTE_E5  659
#define NOTE_F5  698
#define NOTE_FS5 740
#define NOTE_G5  784
#define NOTE_GS5 831
#define NOTE_A5  880
#define NOTE_AS5 932
#define NOTE_B5  988
#define NOTE_C6  1047
#define NOTE_CS6 1109
#define NOTE_D6  1175
#define NOTE_DS6 1245
#define NOTE_E6  1319
#define NOTE_F6  1397
#define NOTE_FS6 1480
#define NOTE_G6  1568
#define NOTE_GS6 1661
#define NOTE_A6  1760
#define NOTE_AS6 1865
#define NOTE_B6  1976
#define NOTE_C7  2093
#define NOTE_CS7 2217
#define NOTE_D7  2349
#define NOTE_DS7 2489
#define NOTE_E7  2637
#define NOTE_F7  2794
#define NOTE_FS7 2960
#define NOTE_G7  3136
#define NOTE_GS7 3322
#define NOTE_A7  3520
#define NOTE_AS7 3729
#define NOTE_B7  3951
#define NOTE_C8  4186
#define NOTE_CS8 4435
#define NOTE_D8  4699
#define NOTE_DS8 4978
#define REST 0


// change this to make the song slower or faster
int tempo = 114;
int melody[] = {
  
  // Mii Channel theme 
  // Score available at https://musescore.com/user/16403456/scores/4984153
  // Uploaded by Catalina Andrade 
  
  NOTE_FS4,8, REST,8, NOTE_A4,8, NOTE_CS5,8, REST,8,NOTE_A4,8, REST,8, NOTE_FS4,8, //1
  NOTE_D4,8, NOTE_D4,8, NOTE_D4,8, REST,8, REST,4, REST,8, NOTE_CS4,8,
  NOTE_D4,8, NOTE_FS4,8, NOTE_A4,8, NOTE_CS5,8, REST,8, NOTE_A4,8, REST,8, NOTE_F4,8,
  NOTE_E5,-4, NOTE_DS5,8, NOTE_D5,8, REST,8, REST,4,
  
  NOTE_GS4,8, REST,8, NOTE_CS5,8, NOTE_FS4,8, REST,8,NOTE_CS5,8, REST,8, NOTE_GS4,8, //5
  REST,8, NOTE_CS5,8, NOTE_G4,8, NOTE_FS4,8, REST,8, NOTE_E4,8, REST,8,
  NOTE_E4,8, NOTE_E4,8, NOTE_E4,8, REST,8, REST,4, NOTE_E4,8, NOTE_E4,8,
  NOTE_E4,8, REST,8, REST,4, NOTE_DS4,8, NOTE_D4,8, 


  NOTE_CS4,8, REST,8, NOTE_A4,8, NOTE_CS5,8, REST,8,NOTE_A4,8, REST,8, NOTE_FS4,8, //9
  NOTE_D4,8, NOTE_D4,8, NOTE_D4,8, REST,8, NOTE_E5,8, NOTE_E5,8, NOTE_E5,8, REST,8,
  REST,8, NOTE_FS4,8, NOTE_A4,8, NOTE_CS5,8, REST,8, NOTE_A4,8, REST,8, NOTE_F4,8,
  NOTE_E5,2, NOTE_D5,8, REST,8, REST,4,


  NOTE_B4,8, NOTE_G4,8, NOTE_D4,8, NOTE_CS4,4, NOTE_B4,8, NOTE_G4,8, NOTE_CS4,8, //13
  NOTE_A4,8, NOTE_FS4,8, NOTE_C4,8, NOTE_B3,4, NOTE_F4,8, NOTE_D4,8, NOTE_B3,8,
  NOTE_E4,8, NOTE_E4,8, NOTE_E4,8, REST,4, REST,4, NOTE_AS4,4,
  NOTE_CS5,8, NOTE_D5,8, NOTE_FS5,8, NOTE_A5,8, REST,8, REST,4, 


  REST,2, NOTE_A3,4, NOTE_AS3,4, //17 
  NOTE_A3,-4, NOTE_A3,8, NOTE_A3,2,
  REST,4, NOTE_A3,8, NOTE_AS3,8, NOTE_A3,8, NOTE_F4,4, NOTE_C4,8,
  NOTE_A3,-4, NOTE_A3,8, NOTE_A3,2,


  REST,2, NOTE_B3,4, NOTE_C4,4, //21
  NOTE_CS4,-4, NOTE_C4,8, NOTE_CS4,2,
  REST,4, NOTE_CS4,8, NOTE_C4,8, NOTE_CS4,8, NOTE_GS4,4, NOTE_DS4,8,
  NOTE_CS4,-4, NOTE_DS4,8, NOTE_B3,1,
  
  NOTE_E4,4, NOTE_E4,4, NOTE_E4,4, REST,8,//25


  //repeats 1-25


  NOTE_FS4,8, REST,8, NOTE_A4,8, NOTE_CS5,8, REST,8,NOTE_A4,8, REST,8, NOTE_FS4,8, //1
  NOTE_D4,8, NOTE_D4,8, NOTE_D4,8, REST,8, REST,4, REST,8, NOTE_CS4,8,
  NOTE_D4,8, NOTE_FS4,8, NOTE_A4,8, NOTE_CS5,8, REST,8, NOTE_A4,8, REST,8, NOTE_F4,8,
  NOTE_E5,-4, NOTE_DS5,8, NOTE_D5,8, REST,8, REST,4,
  
  NOTE_GS4,8, REST,8, NOTE_CS5,8, NOTE_FS4,8, REST,8,NOTE_CS5,8, REST,8, NOTE_GS4,8, //5
  REST,8, NOTE_CS5,8, NOTE_G4,8, NOTE_FS4,8, REST,8, NOTE_E4,8, REST,8,
  NOTE_E4,8, NOTE_E4,8, NOTE_E4,8, REST,8, REST,4, NOTE_E4,8, NOTE_E4,8,
  NOTE_E4,8, REST,8, REST,4, NOTE_DS4,8, NOTE_D4,8, 


  NOTE_CS4,8, REST,8, NOTE_A4,8, NOTE_CS5,8, REST,8,NOTE_A4,8, REST,8, NOTE_FS4,8, //9
  NOTE_D4,8, NOTE_D4,8, NOTE_D4,8, REST,8, NOTE_E5,8, NOTE_E5,8, NOTE_E5,8, REST,8,
  REST,8, NOTE_FS4,8, NOTE_A4,8, NOTE_CS5,8, REST,8, NOTE_A4,8, REST,8, NOTE_F4,8,
  NOTE_E5,2, NOTE_D5,8, REST,8, REST,4,


  NOTE_B4,8, NOTE_G4,8, NOTE_D4,8, NOTE_CS4,4, NOTE_B4,8, NOTE_G4,8, NOTE_CS4,8, //13
  NOTE_A4,8, NOTE_FS4,8, NOTE_C4,8, NOTE_B3,4, NOTE_F4,8, NOTE_D4,8, NOTE_B3,8,
  NOTE_E4,8, NOTE_E4,8, NOTE_E4,8, REST,4, REST,4, NOTE_AS4,4,
  NOTE_CS5,8, NOTE_D5,8, NOTE_FS5,8, NOTE_A5,8, REST,8, REST,4, 


  REST,2, NOTE_A3,4, NOTE_AS3,4, //17 
  NOTE_A3,-4, NOTE_A3,8, NOTE_A3,2,
  REST,4, NOTE_A3,8, NOTE_AS3,8, NOTE_A3,8, NOTE_F4,4, NOTE_C4,8,
  NOTE_A3,-4, NOTE_A3,8, NOTE_A3,2,


  REST,2, NOTE_B3,4, NOTE_C4,4, //21
  NOTE_CS4,-4, NOTE_C4,8, NOTE_CS4,2,
  REST,4, NOTE_CS4,8, NOTE_C4,8, NOTE_CS4,8, NOTE_GS4,4, NOTE_DS4,8,
  NOTE_CS4,-4, NOTE_DS4,8, NOTE_B3,1,
  
  NOTE_E4,4, NOTE_E4,4, NOTE_E4,4, REST,8,//25
   
};


// sizeof gives the number of bytes, each int value is composed of two bytes (16 bits)
// there are two values per note (pitch and duration), so for each note there are four bytes
int notes = sizeof(melody) / sizeof(melody[0]) / 2;


// this calculates the duration of a whole note in ms (60s/tempo)*4 beats
int wholenote = (60000 * 4) / tempo;


int divider = 0, noteDuration = 0;


int starttime;
int activetime;
int prevoustime = 0;
int hours = 8;
int mins = 31;
int secs = 0;
int AlarmHours = 8;
int AlarmMinutes = 30;
LCDKeypad lcd;
void setup()
{
  lcd.begin(16, 2);
  lcd.clear();
 
  Serial.begin(9600);
 
  ;
  pinMode(A0, INPUT);
 
 
  pinMode(A1, OUTPUT);
 
  starttime = millis() / 1000;
  lcd.createChar(1, Bell);
}
 
void loop() {


  int A0  = analogRead(A0);
  lcd.setCursor(5, 0);
  lcd.write(byte(1));
  lcd.setCursor(11, 0);
  lcd.write(byte(1));
  lcd.setCursor(3, 1);
  lcd.print((String)"Alarm: ");
 
  int button = lcd.button();
 
  switch (button) {
    case KEYPAD_UP:
      lcd.clear();
      lcd.setCursor(5, 1);
      lcd.print("Set Time");
      lcd.setCursor(6, 0);
      mins++;
      break;
    case KEYPAD_DOWN:
      lcd.clear();
      lcd.setCursor(5, 1);
      lcd.print("Set Time");
      lcd.setCursor(6, 0);
      hours++;
      break;
    case KEYPAD_LEFT:
      lcd.clear();
      lcd.setCursor(5, 1);
      lcd.print("Set Time");
      lcd.setCursor(6, 0);
      AlarmMinutes++;
      break;
    case KEYPAD_RIGHT:
      lcd.clear();
      lcd.setCursor(5, 1);
      lcd.print("Set Time");
      lcd.setCursor(6, 0);
      AlarmHours++;
      break;
  }
 
  lcd.setCursor(5, 0);
  lcd.write(byte(1));
  lcd.setCursor(11, 0);
  lcd.write(byte(1));
  lcd.setCursor(3, 1);
  lcd.print((String)"Alarm: ");


  if(AlarmMinutes > 59)
  {
    AlarmHours++;
    AlarmMinutes = 0;
  }
  if(AlarmHours > 23)
  {
    AlarmHours = 0;
  }
  lcd.setCursor(9, 1);
  if (AlarmHours < 10)
  {
    lcd.print("0");
    lcd.print(AlarmHours);
  }
  else
  {
    lcd.print(AlarmHours);
  }
 
  lcd.print(":");
 
  if (AlarmMinutes < 10)
  {
    lcd.print("0");
    lcd.print(AlarmMinutes);
  }
  else
  {
    lcd.print(AlarmMinutes);
  }
  lcd.setCursor(15, 1);
  lcd.print(" ");
 
 
  activetime = (millis() / 1000) - starttime;
  if (secs > 59)
  {
    secs++;
 
  }
 
  if (secs > 59)
  {
    mins++;
    secs = 0;
  }
 
  if (prevoustime < (activetime - 59))
  {
    mins++;
    prevoustime = activetime;
  }
 
  if (mins > 59)
  {
    hours++;
    mins = 0;
  }
 
  if (hours > 23)
  {
    hours = 0;
  }
 
 
  lcd.setCursor(6, 0);
 
  if (hours < 10)
  {
    lcd.print("0");
    lcd.print(hours);
  }
  else
  {
    lcd.print(hours);
  }
 
  lcd.print(":");
 
  if (mins < 10)
  {
    lcd.print("0");
    lcd.print(mins);
  }
  else
  {
    lcd.print(mins);
  }
 
  lcd.print("");


  if (AlarmHours == hours && AlarmMinutes == mins)// change 8 and 30 to your time for this to ring
  {
    for (int thisNote = 0; thisNote < notes * 2; thisNote = thisNote + 2) {
      // calculates the duration of each note
      divider = melody[thisNote + 1];
      if (divider > 0) {
        // regular note, just proceed
        noteDuration = (wholenote) / divider;
      }else if (divider < 0) {
        // dotted notes are represented with negative durations!!
        noteDuration = (wholenote) / abs(divider);
        noteDuration *= 1.5; // increases the duration in half for dotted notes
      }
      // we only play the note for 90% of the duration, leaving 10% as a pause
      tone(2, melody[thisNote], noteDuration*0.9);


      // Wait for the specief duration before playing the next note.
      delay(noteDuration);
    
      // stop the waveform generation before the next note.
      noTone(2);
    }


  }
  else
  {
    delay(300);
  }
  lcd.clear();
}

Copy and paste this code in arduino ide or open the file attached below. This code is based on this project from THEBEASTATFNBR and this project from robsoncouto. You can easily change the music, here is an amazing library of songs. To change the song you simply replace the melody[] and tempo variables with the ones from your song.

Downloads

The Manual

After you have uploaded your code to your arduino the time will be automatically set to 8:31. To change this you can use the up and down buttons. The left and right buttons are used to change the alarm time. This is the time that the alarm will go off. To turn off the alarm you can use the reset button. This will reset the whole arduino so unfortunately your clock will be reset to the start time. To change this you can change the alarmhours/minutes and the Hours/minutes variables.

Optional

9d615f2c-6b80-4c5f-9dca-4cb903541a0f.jpg

In this step, we're going to make our alarm clock somewhat pretty. First, you've got to solder your pins in the correct hole. You can do this by putting the pins in the hole and putting the tin over the pins. This makes sure that they're stuck in their place and that they won't come out easily. Make sure you're putting the pins in the right holes and that you're not accidently soldering a pin into two holes. Because after you've soldered the pins it is pretty challening to get them out. After that is done you can make a wooden surface for the clock. I've made it using a laser cutter if you have access to a laser cutter you can use the file below, but if you don't you can just saw a plank. The measurements of the wood are 13x6 cm. Lastly, we can screw the arduino in its place with the bolts and nuts and glue the speaker or buzzer to the wooden surface.