Video Tutorial
Tutorial
Hey! If you have a lot of spare electronics and for some reason you don’t have an alarm clock then it’s actually pretty easy to make one with a couple dollars in parts. Here’s a pretty basic Arduino tutorial to design something like this.
Part List
You’ll need :
- an RTC (DS3231 chip based ones are very cheap, ~$2 CAD from ebay)
- an Arduino Nano (~$3-4 CAD)
- some LED’s (Ultrabright warm white are like 60 cents for 2 or cheaper)
- Jumper cables (for a breadboard)
- Breadboard
- A power supply or laptop or anything with a USB port that delivers 5v power
The Electronics
The basic idea of this Arduino tutorial is easy enough to grasp even if you’re unfamiliar with electronics: All you’re doing is sending the current time to the Arduino, which compares it with your preset wake-up time, and ramps up the brightness to a few LED’s based on how far past that wake-up time it is. There’s very little coding really or electronics involved here, and you could probably connect this with tape, but I prefer a breadboard.
A Bit About I2C
The circuit is not a particularly complex system, and it works on a protocol called I²C (Also known as I2C, I squared C, or Inter-Integrated Circuit Protocol). It’s a pretty popular standard for communication between chips and simple electronics (useful for connecting some small OLED displays too, which is really cool, and I might make something about later), but all you need to know is that it involves data transfer using two pins (Other protocols for transferring data like SPI have like 8 pins):
- SDA Serial Data
- SCL Serial Clock
Serial means one bit at a time, as opposed to sending them in parallel, by the way. That’s not super important, but it’s good to know that it’s not just random jargon. The point is that you only have to connect two pins. Now, these specific pins depend on the board you’re using - the SDA and SCL pins are A4 and A5 on the Arduino Uno and Nano, or 2 and 3 on the Leonardo, and it might be different depending on your board so set it up, and then use the i2c scanner to check if your board is detecting an i2c compatible device. If it’s not, check the pins and see if the boards are powered correctly.
This website gives a few pinout diagrams and a way to diagnose the i2C connection.
The Code
You’ll have to install the DS3231 library to be able to interface with the RTC (I know, the random combinations of numbers and letters feels a bit arbitrary). You can download it here: https://www.arduinolibraries.info/libraries/ds3231.
#include <DS3231.h>
#include <Wire.h>
DS3231 Clock;
//time is given by h_wakeup:m_wakeup -- make sure to use 24H time!
const int h_wakeup = 6; //number of hours over which to
const int m_wakeup = 45;
const int t_wakeup = 10; //number of minutes over which to increase the brightness
bool h12 = false;
bool PM = false;
float brightness;
// int buttonState = 0;
void setup() {
// put your setup code here, to run once:
Wire.begin();
Serial.begin(9600);
/*
//setting time (RUN THE FIRST TIME THEN COMMENT OUT AGAIN)
const byte day = 22;
const byte month = 9;
const byte year = 19;
const byte seconds = 00;
const byte minutes = 11;
const byte hours = 9;
Clock.setHour(hours);
Clock.setMinute(minutes);
Clock.setSecond(seconds);
// Set the date
Clock.setDate(day);
Clock.setMonth(month);
Clock.setYear(year);
*/
//pinMode (3, OUTPUT);
pinMode(5,OUTPUT);
pinMode(6,OUTPUT);
pinMode(7, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
// Serial.print(Clock.getTime());
Serial.print("T = ");
Serial.print(Clock.getTemperature(),2);
Serial.print("\n");
Serial.print(Clock.getHour(h12,PM));
Serial.print(":");
Serial.print(Clock.getMinute());
Serial.print(":");
Serial.print(Clock.getSecond());
Serial.print("\n");
delay(1000);
// ALARM CLOCK
if (Clock.getHour(h12, PM) == h_wakeup || Clock.getHour(h12, PM) == h_wakeup+1){
if (Clock.getMinute() >= m_wakeup){
brightness = (240/t_wakeup)*(Clock.getMinute()-m_wakeup) + ((240/t_wakeup)/60 * Clock.getSecond());
//brightness clamping
if (brightness < 0){
brightness = 0;
}
if (brightness > 240){
brightness = 240;
}
analogWrite(7, brightness);
analogWrite(6, brightness);
analogWrite(5, brightness);
Serial.print("B: ");
Serial.print((float)brightness);
}
/* --- DEMO SECTION --- This just fades them in and out, similar to the PWM example in the default library
int multiplier = 1;
brightness = 1;
for(int i = 0; i<255;i++){
brightness+= multiplier;
analogWrite(7, brightness);
analogWrite(6, brightness);
analogWrite(5, brightness);
delay(1000);
if (brightness > 255 ){
multiplier = -1;
}
else if (brightness < 0){
multiplier = 1;
}
Serial.print("B: ");
Serial.print((float)brightness);
*/
*/
}
On the first run of the code, you’ll have to uncomment the time setting portion of the code (Which I’ve labeled in all caps). Make sure to set this to whatever time is on your clock, and then comment it back out (The /* and */ symbols sandwich the commented code, which doesn’t run, in the middle. Delete the comment flags to run it, and then put them back in so you don’t reset the time every single time you run it.)
Conclusion
That’s about it for this Arduino tutorial. I hope you find it useful and can make something like this yourself! Be careful not to short circuit the RTC like I did and use tape or something non-conductive if you want to hide the red power LEDs. If you found it useful or have any questions/corrections/suggestions, feel free to leave them in the comments.