In recent blog posts (part 1, part 2) I talked about building an alarm clock with Arduino. I finally had some leftover time to build a similar thing with Arduino.
The parts I used are:
- Arduino Mega
- Tiny RTC
- Seven Segment display with four numbers
The initial idea was to use a bluetooth shield to modify the time and to set the alarm. In the end I didn’t used this, so at this moment it only tells the user the time. Before I used the Tiny RTC I had two buttons to modify time and to switcht between modes, so you had a mode for displaying time and a mode for setting the time. But because of the RTC I didn’t need the setting time, because it’s done through uploading a sketch to the Arduino. These features could be incorporated into one sketch for the Arduino with so it will use different functions.
I 3d printed a casing for it so it would hold the parts and the breadboard. I haven’t soldered any parts together, this would be the next step, but as an early prototype this would be sufficient enough.
It runs on a 9v battery, but the code requires a lot of power, because of the constant calls made between the RTC module and the Arduino. So the code needs to be made more efficient.
For the code I used an external library that makes driving the display a lot easier. I modified it a bit to accept Strings as a time and not only numbers.
#include "SevSeg.h"
#include "Wire.h"
#define DS1307_ADDRESS 0x68
SevSeg sevseg; //Instantiate a seven segment controller object
String minuteString = "";
void setup() {
byte numDigits = 4;
byte digitPins[] = {2, 3, 4, 5};
byte segmentPins[] = {6, 7, 8, 9, 10, 11, 12, 13};
sevseg.begin(COMMON_ANODE, numDigits, digitPins, segmentPins);
sevseg.setBrightness(90);
Wire.begin();
Serial.begin(9600);
}
void loop() {
// Reset the register pointer
Wire.beginTransmission(DS1307_ADDRESS);
byte zero = 0x00;
Wire.write(zero);
Wire.endTransmission();
Wire.requestFrom(DS1307_ADDRESS, 7);
int second = bcdToDec(Wire.read());
int minute = bcdToDec(Wire.read());
int hour = bcdToDec(Wire.read() & 0b111111); //24 hour time
int weekDay = bcdToDec(Wire.read()); //0-6 -> sunday - Saturday
int monthDay = bcdToDec(Wire.read());
int month = bcdToDec(Wire.read());
int year = bcdToDec(Wire.read());
if(minute < 10)
{
minuteString = "0" + String(minute);
}
else
{
minuteString = String(minute);
}
String hourString = String(hour);
String time = String(hourString + minuteString);
Serial.println(minuteString);
sevseg.setNumber(time.toInt(), 2);
sevseg.refreshDisplay(); // Must run repeatedly
}
byte bcdToDec(byte val) {
// Convert binary coded decimal to normal decimal numbers
return ( (val/16*10) + (val%16) );
}
Here are some pictures of the end result: