After finishing my hexapod I needed another project
Over the last year I have bought numerous small break out boards on Ebay. So I came up with the idea to make an Arduino MP3 alarm clock. The idea is simple, an LCD that shows the date and time. A couple of buttons to set an alarm, and an MP3 module and small speaker to play an MP3 when the alarm goes of.
I decided to use my 6€ 12864ZW graphical LCD, with 128×64 pixels it has enough room to display all the things I want. I used the Arduino u8glib and software SPI to make it work with my Arduino Mega2560. I decided to use software SPI because I already had 2 other devices on the SPI bus. It should be possible to use the hardware SPI bus but that would need more coding. You can connect multiple devices to an SPI bus but you can only talk to one at a time.
Next thing I needed was an RTC, a real time clock. I have a DS1307 RTC but it’s not very accurate, it “loses” about 1-2 seconds a day. So that’s not very good. Luckily there some more accurate RTC’s, like the DS3231. It’s very similar to the DS1307 and you can even use the same code. I bought a couple of DS3231 modules on Ebay and they work very well. The DS3231 uses the I2C bus to communicate with a micro controller. So you need to connect 2 wires (SDA and SCL) to the Arduino and 5V and GND. I did not use a special library to get the data from the DS3231, just the Arduino Wire library.
An Arduino does not have enough memory to store an MP3 file so I used a SD card reader and a small SD card to store the MP3 file. The SD card is connected to the SPI pins of the Arduino.
I used an LCsoft VS1053 MP3 module and a small 0,25W speaker (bought from Tayda Electronics) to play MP3’s. The VS1053 MP3 module is also connected to the SPI pins of the Arduino. The LCsoft module needs to be modified to make it play MP3’s. You can read about that in my previous post.
To be able to set the alarm and turn it of and on I used 5 small push buttons. And as I had a couple of DHT11 sensors laying around I also included one. So I can display the temperature and humidity on the gLCD.
I originally wanted to use my Bajduino Mega 3A for this project but I could not get the VS1053 library to work with the ATmega1284 micro controller. I will try and get it to work at a later time. So I chose an Arduino Mega2560 for this project. An Arduino with an ATmega328 (Duemilanove/Uno/Nano) does not have enough program memory for this project. The u8glib and VS1053 libraries take up a lot of memory. My final sketch compiled in the Arduino IDE 1.0.5 takes up more then 40kb of program memory.
I coded 2 alarms in my sketch, one is only active during the week (Alarm 1 on the LCD) and the second alarm (Alarm 2 on the LCD) is only active during the weekend.
I used 5 push buttons and connected them to 5 digital pins using the internal pullup resistors. 1 to turn on/off alarm 1 and another for the second alarm. The other 3 buttons are used to set the hour and minutes of both alarms. With the first button I can “scroll” to the hour and minutes of both alarms. The other 2 buttons (up/down) are used to set the hour and minutes. Following video shows how it works:
If you would want to replicate this I recommend that you first try out the example sketches of the VS1053 MP3 module. I had to modify one of the files of the library to make it play mp3’s without the song skipping. In the SFEMP3ShieldConfig.h file I had to set define SD_SEL to pin 53 or I got garbled noise. This will make the SD card use pin 53 as chip select. In this file you will also find to which pins the module connects.
The VS1053 will not play just any MP3 file with this library. I first tried some self ripped variable bitrate MP3 files and only got some noise out my speaker. I then ripped some MP3’s with a constant bitrate @ 96kbs. These files played a lot better.
I made a big wire mess connecting all these devices to an Arduino Mega2560. These are all the connections I made (well I left out the power connections):
SD card connections:
- MISO to pin 50
- MOSI to pin 51
- SCK to pin 52
- SS/CS to pin 53
VS1053 MP3 module:
- MP3_XCS to pin 6
- MP3_XDCS to pin 7
- MP3_DREQ to pin 2
- MP3_RESET to pin 8
- MISO to pin 50
- MOSI to pin 51
- SCK to pin 52
Graphical LCD (using software SPI)
- SCK to pin 32
- MOSI to pin 33
- CS to pin 30
- SDA to pin 20
- SCL to pin 21
Push buttons (using the internal pullup resistors of the ATmega)
- upButton to pin 22
- downButton to pin 23
- alarmSetButton to pin 24;
- alarmMoFriButton to pin 25;
- alarmWeButton to pin 38;
- Signal to pin 42
I first only used USB to power all the devices but it did not work very well. When playing MP3’s the lcd would become dimmer and the MP3 module would start to skip. So I used an external power supply to power the gLCD and MP3 module. The LCD draws about 100mA and the MP3 module a bit more then that (when playing an MP3).
Programming this was pretty straight forward. The libraries for the gLCD and VS1053 make it very easy. They use up a lot of memory though. I check the state of the buttons once every 300 milliseconds. This is done for 2 reasons. First to debounce them and second because they control the value of the hour/minute. If you would check them constantly the value would go up/down so fast it would be impossible to set the alarm time.
My code includes a lot of serial printing at the end of the sketch. I put all the serial print stuff in a separate “void debug”. When you uncomment this code the serial printing will cause the MP3 to skip during playback. Apparently the serial printing takes up a lot of time so the micro controller can’t keep up sending data from the SD card to the VS1053 module.
Here you can download the full sketch: Arduino MP3 Alarm clock