Clock alarm temprture senseor and timer, all in one device using an arduino uno and RTC
Things used in this project
Hardware components
Software apps and online services
Arduino IDE
Hand tools and fabrication machines
Soldering iron (generic)
Solder Wire, Lead Free
Cable Cutter, 143mm
3D Printer (generic). For the case
Hot glue gun (generic). Hold the part of the case togther you can also use other glue
Story
In this project we will make an arduino clock alarm with timer and a temperature sensor.
Wiring:
Button #1 Mode - connected to digtal pin 2
Button #2 Select - connected to digtal pin 4
Button #3 Plus - connected to digtal pin 3
Button #4 Minus - connected to digtal pin 12
Button #5 Timer on/0ff - connected to digtal pin 11
You can wire all of the component to a breadboard or wire to a arduino proto shield if you would like to use the case.
To set time you will need to hold the mode button on screen 1.
libraries:
DHT - https://github.com/adafruit/DHT-sensor-library
LiquidCrystal I2C - https://www.arduinolibraries.info/libraries/liquid-crystal-i2-c
Wire - https://github.com/arduino/ArduinoCore-avr/tree/master/libraries/Wire
virtuabotixRTC - https://github.com/chrisfryer78/ArduinoRTClibrary
RTCLib - https://github.com/adafruit/RTClib
Custom parts and enclosures
Schematics
#include <DHT.h> #include <LiquidCrystal_I2C.h> #include <Wire.h> #include <virtuabotixRTC.h> #include <RTClib.h> #define DHTPIN 13 #define DHTTYPE DHT22 // DHT 22 (AM2302) DHT dht(DHTPIN, DHTTYPE); virtuabotixRTC myRTC(6, 7, 8); LiquidCrystal_I2C lcd(0x27, 16, 2); const int buzzer = 9; const int buttonPin1 = 2; const int buttonPin2 = 3; const int buttonPin3 = 4; const int buttonPin4 = 12; const int buttonPin5 = 11; //variable float temp; boolean buttonActive = false; boolean longPressActive = false; long buttonTimer = 0; long longPressTime = 400; int buttonState1 = 0; int buttonState2 = 0; int buttonState3 = 0; int buttonState4 = 0; int buttonState5 = 0; int hours = 20; int minute = 0; int secondsSet = 0; int minutesSet = 0; int hoursSet = 0; int monthSet = 0; int dayMonthSet = 0; int dayWeekSet = 0; int yearSet = 0; int timerMin = 0; int timerSec = 0; int timerOn = 0; int clicks = 0; int clicks2 = 0; int clicks3 = 0; int clicks4 = 0; String hourSetDisplay; String secondSetDisplay; String minuteSetDisplay; String dayOfMonthSetDisplay; //String monthSetDisplay; String hoursSetDisplay; String minutesSetDisplay; String secondsSetDisplay; String dayMonthSetDisplay; String monthSetDisplay; String dayWeekDisplay; String timerSecDisplay; String timerMinDisplay; String alarmHourDisplay; String alarmMinuteDisplay; int screens = 1; bool alarmToggle = true; bool timerToggle = true; bool alarmToggleSet = true; int x = 0; unsigned long previousMillis = 0; unsigned long interval = 1000UL; unsigned long previousMillis1 = 0; unsigned long interval1 = 500UL; void setup() { Serial.begin(9600); // seconds, minutes, hours, day of the week, day of the month, month, year //myRTC.setDS1302Time(41, 0, 16, 1, 25, 7, 2021); myRTC.updateTime(); pinMode(buzzer, OUTPUT); pinMode(buttonPin1, INPUT); pinMode(buttonPin2, INPUT); pinMode(buttonPin3, INPUT); pinMode(buttonPin4, INPUT); pinMode(buttonPin5, INPUT); dayMonthSet = myRTC.dayofmonth; monthSet = myRTC.month; secondsSet = myRTC.seconds; minutesSet = myRTC.minutes; hoursSet = myRTC.hours; dayWeekSet = myRTC.dayofweek; yearSet = myRTC.year; int buttonState1 = 0; int buttonState2 = 0; int buttonState3 = 0; int buttonState4 = 0; int buttonState5 = 0; dht.begin(); lcd.begin(); lcd.home(); lcd.clear(); } void loop() { //screens screensSelect: myRTC.updateTime(); if (screens == 1) { screen_1(); } else if (screens == 2) { screen_2(); } else if (screens == 3) { screen_3(); } else if (screens == 4) { set_time(); } else if (screens == 5) { set_date(); } else if (screens == 6) { set_day(); } else if (screens == 7) { set_year(); } else if (screens == 8) { alarmOnOff(); } //blink if (millis() - previousMillis1 > interval1) { previousMillis1 += interval1; if (screens != 1) { lcd.print(' '); lcd.print(' '); } } //timer if (millis() - previousMillis > interval) { previousMillis += interval; if (timerOn == 1) { if (timerMin == 0 && timerSec == 0 && timerToggle) { timerOn = 0; Serial.println("timerOn"); delay(170); for (int i = 0; i < 255; i++) { myRTC.updateTime(); analogWrite(buzzer, i); buttonState1 = digitalRead(buttonPin1); if (buttonState1 == HIGH) { timerToggle = false; noTone(buzzer); delay(170); break; } delay(10); } //timer buzzer for (int i = 255; i > 0; i--) { analogWrite(buzzer, i); myRTC.updateTime(); buttonState1 = digitalRead(buttonPin1); if (buttonState1 == HIGH) { timerToggle = false; noTone(buzzer); delay(170); break; } delay(10); } noTone(buzzer); } else { timerSec -= 1; if (timerSec < 0) { if (timerMin != 0) { timerMin -= 1; } timerSec = 59; } } } } //alarm while (myRTC.hours == hours && myRTC.minutes == minute && alarmToggle && alarmToggleSet) { delay(170); lcd.clear(); lcd.setCursor(0, 0); lcd.print("Its: "); lcd.print(alarmHourDisplay); lcd.print(":"); lcd.print(alarmMinuteDisplay); fixTimeDate(); for (int i = 0; i < 255; i++) { myRTC.updateTime(); analogWrite(buzzer, i); buttonState1 = digitalRead(buttonPin1); if (buttonState1 == HIGH) { alarmToggle = false; noTone(buzzer); delay(170); goto screensSelect; } delay(10); } //alarm buzzer for (int i = 255; i > 0; i--) { analogWrite(buzzer, i); myRTC.updateTime(); buttonState1 = digitalRead(buttonPin1); if (buttonState1 == HIGH) { alarmToggle = false; noTone(buzzer); delay(170); goto screensSelect; } delay(10); } buttonState1 = digitalRead(buttonPin1); if (buttonState1 == HIGH) { alarmToggle = false; noTone(buzzer); delay(170); } noTone(buzzer); } if (myRTC.minutes - 1 == minute) { alarmToggle >= true; } // Serial.println("alarmToggle:"); // Serial.print(alarmToggle); // Serial.println("alarmToggleset:"); // Serial.print(alarmToggleSet); } //fix all the time for display void fixSetTime() { if (hoursSet < 10) { hoursSetDisplay = String(0) + String(hoursSet); } else if (hoursSet >= 10) { hoursSetDisplay = String(hoursSet); } if (minutesSet < 10) { minutesSetDisplay = String(0) + String(minutesSet); } else if (minutesSet >= 10) { minutesSetDisplay = String(minutesSet); } if (secondsSet < 10) { secondsSetDisplay = String(0) + String(secondsSet); } else if (secondsSet >= 10) { secondsSetDisplay = String(secondsSet); } } void fixSetDate() { if (dayMonthSet < 10) { dayMonthSetDisplay = String(0) + String(dayMonthSet); } else if (dayMonthSet >= 10) { dayMonthSetDisplay = String(dayMonthSet); } if (monthSet < 10) { monthSetDisplay = String(0) + String(monthSet); } else if (monthSet >= 10) { monthSetDisplay = String(monthSet); } } void fixTimeDate() { if (myRTC.hours < 10) { hourSetDisplay = String(0) + String(myRTC.hours); } else if (myRTC.hours >= 10) { hourSetDisplay = String(myRTC.hours); } if (myRTC.minutes < 10) { minuteSetDisplay = String(0) + String(myRTC.minutes); } else if (myRTC.minutes >= 10) { minuteSetDisplay = String(myRTC.minutes); } if (myRTC.seconds < 10) { secondSetDisplay = String(0) + String(myRTC.seconds); } else if (myRTC.seconds >= 10) { secondSetDisplay = String(myRTC.seconds); } if (myRTC.dayofmonth < 10) { dayOfMonthSetDisplay = String(0) + String(myRTC.dayofmonth); } else if (myRTC.dayofmonth >= 10) { dayOfMonthSetDisplay = String(myRTC.dayofmonth); } if (myRTC.month < 10) { monthSetDisplay = String(0) + String(myRTC.month); } else if (myRTC.month >= 10) { monthSetDisplay = String(myRTC.month); } // alarm if (hours < 10) { alarmHourDisplay = String(0) + String(hours); } else if (hours >= 10) { alarmHourDisplay = String(hours); } if (minute < 10) { alarmMinuteDisplay = String(0) + String(minute); } else if (minute >= 10) { alarmMinuteDisplay = String(minute); } switch (myRTC.dayofweek) { case 1: dayWeekDisplay = "Sun"; break; case 2: dayWeekDisplay = "Mon"; break; case 3: dayWeekDisplay = "Tue"; break; case 4: dayWeekDisplay = "Wed"; break; case 5: dayWeekDisplay = "Thu"; break; case 6: dayWeekDisplay = "Fri"; break; case 7: dayWeekDisplay = "Sat"; break; } } void fixTimer() { if (timerMin < 10) { timerMinDisplay = String(0) + String(timerMin); } else if (timerMin >= 10) { timerMinDisplay = String(timerMin); } if (timerSec < 10) { timerSecDisplay = String(0) + String(timerSec); } else if (timerSec >= 10) { timerSecDisplay = String(timerSec); } } void screen_1() { fixTimeDate(); //display time temp = dht.readTemperature() - 1; lcd.setCursor(0, 0); lcd.print(hourSetDisplay); lcd.print(":"); lcd.print(minuteSetDisplay); lcd.print("."); lcd.print(secondSetDisplay); lcd.print(" "); lcd.print("T:"); lcd.print(temp, 0); lcd.print((char)223); lcd.print("C"); lcd.setCursor(0, 1); lcd.print(" "); lcd.print(dayWeekDisplay); lcd.print(" "); lcd.print(dayOfMonthSetDisplay); lcd.print("/"); lcd.print(monthSetDisplay); lcd.print("/"); lcd.print(myRTC.year); lcd.print(" "); //next screen if (digitalRead(buttonPin1) == HIGH) { if (buttonActive == false) { buttonActive = true; buttonTimer = millis(); } if ((millis() - buttonTimer > longPressTime) && (longPressActive == false)) { longPressActive = true; screens = 4; delay(170); } } else { if (buttonActive == true) { if (longPressActive == true) { longPressActive = false; } else { delay(170); lcd.clear(); screens = 2; } buttonActive = false; } } } void screen_2() { //select buttonState3 = digitalRead(buttonPin3); if (buttonState3 == HIGH) { if (x == 0) { // Toggle on x = 1; lcd.setCursor(3, 1); } else { // Toggle off lcd.setCursor(0, 1); x = 0; } delay(170); } // add time buttonState2 = digitalRead(buttonPin2); if (buttonState2 == HIGH) { alarmToggle = true; //hours up if (x == 0) { hours += 1; if (hours >= 24) { hours = 0; } } //minutes up if (x == 1) { minute += 1; if (minute >= 60) { minute = 0; } } delay(170); } buttonState4 = digitalRead(buttonPin4); //hours down if (buttonState4 == HIGH) { alarmToggle = true; if (x == 0) { hours -= 1; if (hours < 0) { hours = 23; } } //minute down if (x == 1) { minute -= 1; if (minute < 0) { minute = 59; } } delay(170); } //print the alarm time: fixTimeDate(); lcd.setCursor(0, 0); lcd.print("Alarm Time:"); lcd.setCursor(0, 1); lcd.print(alarmHourDisplay); lcd.print(":"); lcd.print(alarmMinuteDisplay); lcd.print(" "); if (x == 0) { lcd.setCursor(0, 1); } else { lcd.setCursor(3, 1); } //next screen if (digitalRead(buttonPin1) == HIGH) { if (buttonActive == false) { buttonActive = true; buttonTimer = millis(); } if ((millis() - buttonTimer > longPressTime) && (longPressActive == false)) { longPressActive = true; //stop from hold click; } } else { if (buttonActive == true) { if (longPressActive == true) { longPressActive = false; } else { //next screen lcd.clear(); screens += 1; } buttonActive = false; } } } void screen_3() { //turn on timer: buttonState5 = digitalRead(buttonPin5); if (buttonState5 == HIGH) { if (clicks4 == 1) { timerOn = 0; clicks4 = 0; } else { timerOn = 1; clicks4 = 8; } delay(170); } //select min/sec buttonState3 = digitalRead(buttonPin3); if (buttonState3 == HIGH) { if (clicks3 == 1) { clicks3 = 0; } else { clicks3 += 1; } delay(170); } buttonState2 = digitalRead(buttonPin2); if (buttonState2 == HIGH) { //min up if (clicks3 == 0) { lcd.setCursor(0, 1); timerMin += 1; lcd.setCursor(0, 1); if (timerMin >= 100) { timerMin = 0; } } //sec up if (clicks3 == 1) { lcd.setCursor(3, 1); timerSec += 1; lcd.setCursor(3, 1); if (timerSec >= 60) { timerSec = 0; } } delay(170); } buttonState4 = digitalRead(buttonPin4); if (buttonState4 == HIGH) { //min down if (clicks3 == 0) { lcd.setCursor(0, 1); timerMin -= 1; lcd.setCursor(0, 1); if (timerMin < 0) { timerMin = 99; } } //sec down if (clicks3 == 1) { lcd.setCursor(3, 1); timerSec -= 1; lcd.setCursor(3, 1); if (timerSec < 0) { timerSec = 59; } } delay(170); } //print timer fixTimer(); lcd.setCursor(0, 0); lcd.print("Timer:"); lcd.setCursor(0, 1); lcd.print(timerMinDisplay); lcd.print(":"); lcd.print(timerSecDisplay); lcd.print(" "); if (clicks3 == 0) { lcd.setCursor(0, 1); } if (clicks3 == 1) { lcd.setCursor(3, 1); } //next screen if (digitalRead(buttonPin1) == HIGH) { if (buttonActive == false) { buttonActive = true; buttonTimer = millis(); } if ((millis() - buttonTimer > longPressTime) && (longPressActive == false)) { longPressActive = true; //stop from hold click; } } else { if (buttonActive == true) { if (longPressActive == true) { longPressActive = false; } else { //next screen lcd.clear(); screens = 8; } buttonActive = false; } } } void set_time() { hoursSet = myRTC.hours; minutesSet = myRTC.minutes; secondsSet = myRTC.seconds; fixSetTime(); //next screen if (digitalRead(buttonPin1) == HIGH) { if (buttonActive == false) { buttonActive = true; buttonTimer = millis(); } if ((millis() - buttonTimer > longPressTime) && (longPressActive == false)) { longPressActive = true; //stop from hold click; } } else { if (buttonActive == true) { if (longPressActive == true) { longPressActive = false; } else { //next screen lcd.clear(); screens += 1; } buttonActive = false; } } if (clicks == 0) { lcd.setCursor(0, 1); } if (clicks == 1) { lcd.setCursor(3, 1); } if (clicks == 2) { lcd.setCursor(6, 1); } //select hours/min/sec buttonState3 = digitalRead(buttonPin3); if (buttonState3 == HIGH) { if (clicks == 2) { clicks = 0; } else { clicks += 1; } delay(170); } buttonState2 = digitalRead(buttonPin2); if (buttonState2 == HIGH) { //hours up if (clicks == 0) { lcd.setCursor(0, 1); hoursSet += 1; lcd.setCursor(0, 1); if (hoursSet >= 24) { hoursSet = 0; } } if (clicks == 1) { //min up lcd.setCursor(3, 1); minutesSet += 1; lcd.setCursor(3, 1); if (minutesSet >= 60) { minutesSet = 0; } } if (clicks == 2) { //sec up lcd.setCursor(6, 1); secondsSet += 1; lcd.setCursor(6, 1); if (secondsSet >= 60) { secondsSet = 0; } } delay(170); } buttonState4 = digitalRead(buttonPin4); if (buttonState4 == HIGH) { //hours down if (clicks == 0) { lcd.setCursor(0, 1); hoursSet -= 1; lcd.setCursor(0, 1); if (hoursSet < 0) { hoursSet = 23; } } //min down if (clicks == 1) { lcd.setCursor(3, 1); minutesSet -= 1; lcd.setCursor(3, 1); if (minutesSet < 0) { minutesSet = 59; } } //sec down if (clicks == 2) { lcd.setCursor(6, 1); secondsSet -= 1; lcd.setCursor(6, 1); if (secondsSet < 0) { secondsSet = 59; } } } //print set time lcd.setCursor(0, 0); lcd.print("Set Time: "); lcd.setCursor(0, 1); lcd.print(hoursSetDisplay); lcd.print(":"); lcd.print(minutesSetDisplay); lcd.print(":"); lcd.print(secondsSetDisplay); lcd.print(" "); if (clicks == 0) { lcd.setCursor(0, 1); } if (clicks == 1) { lcd.setCursor(3, 1); } if (clicks == 2) { lcd.setCursor(6, 1); } // seconds, minutes, hours, day of the week, day of the month, month, year myRTC.setDS1302Time(secondsSet, minutesSet, hoursSet, 6, dayMonthSet, monthSet, 2021); delay(170); } void set_date() { dayMonthSet = myRTC.dayofmonth; monthSet = myRTC.month; //next screen if (digitalRead(buttonPin1) == HIGH) { if (buttonActive == false) { buttonActive = true; buttonTimer = millis(); } if ((millis() - buttonTimer > longPressTime) && (longPressActive == false)) { longPressActive = true; //stop from hold click; } } else { if (buttonActive == true) { if (longPressActive == true) { longPressActive = false; } else { //next screen lcd.clear(); screens += 1; } buttonActive = false; } } //select dayOfMonth/month buttonState3 = digitalRead(buttonPin3); if (buttonState3 == HIGH) { if (clicks2 == 1) { clicks2 = 0; } else { clicks2 += 1; } delay(170); } buttonState2 = digitalRead(buttonPin2); if (buttonState2 == HIGH) { //add dayOfMonth if (clicks2 == 0) { lcd.setCursor(0, 1); dayMonthSet += 1; lcd.setCursor(0, 1); if (monthSet == 4 || monthSet == 6 || monthSet == 6 || monthSet == 9 || monthSet == 11) { if (dayMonthSet > 30) { dayMonthSet = 1; } } else { if (dayMonthSet > 31) { dayMonthSet = 1; } } } //add month if (clicks2 == 1) { lcd.setCursor(3, 1); monthSet += 1; lcd.setCursor(3, 1); if (monthSet > 12) { monthSet = 1; } } delay(170); } buttonState4 = digitalRead(buttonPin4); if (buttonState4 == HIGH) { //dayOfMonth down if (clicks2 == 0) { lcd.setCursor(0, 1); dayMonthSet -= 1; lcd.setCursor(0, 1); if (monthSet == 4 || monthSet == 6 || monthSet == 6 || monthSet == 9 || monthSet == 11) { if (dayMonthSet < 1) { dayMonthSet = 30; } } else { if (dayMonthSet < 1) { dayMonthSet = 31; } } } //month down if (clicks2 == 1) { lcd.setCursor(3, 1); if (monthSet == 1) { monthSet = 12; } else { monthSet -= 1; } } delay(170); } fixSetDate(); lcd.setCursor(0, 0); lcd.print("Set Date: "); lcd.setCursor(0, 1); lcd.print(dayMonthSetDisplay); lcd.print("/"); lcd.print(monthSetDisplay); lcd.print(" "); if (clicks2 == 0) { lcd.setCursor(0, 1); } if (clicks2 == 1) { lcd.setCursor(3, 1); } // seconds, minutes, hours, day of the week, day of the month, month, year myRTC.setDS1302Time(secondsSet, minutesSet, hoursSet, dayWeekSet, dayMonthSet, monthSet, 2021); //delay(170); } void set_day() { dayWeekSet = myRTC.dayofweek; buttonState1 = digitalRead(buttonPin1); //next screen if (digitalRead(buttonPin1) == HIGH) { if (buttonActive == false) { buttonActive = true; buttonTimer = millis(); } if ((millis() - buttonTimer > longPressTime) && (longPressActive == false)) { longPressActive = true; //stop from hold click; } } else { if (buttonActive == true) { if (longPressActive == true) { longPressActive = false; } else { //next screen lcd.clear(); screens += 1; } buttonActive = false; } } //day up buttonState2 = digitalRead(buttonPin2); if (buttonState2 == HIGH) { lcd.setCursor(0, 1); dayWeekSet += 1; lcd.setCursor(0, 1); if (dayWeekSet > 7) { dayWeekSet = 7; } delay(170); } //day down buttonState4 = digitalRead(buttonPin4); if (buttonState4 == HIGH) { lcd.setCursor(0, 1); dayWeekSet -= 1; lcd.setCursor(0, 1); if (dayWeekSet < 1) { dayWeekSet = 30; } delay(170); } //print day fixTimeDate(); lcd.setCursor(0, 0); lcd.print("Set Day Of Week: "); lcd.setCursor(0, 1); lcd.print(dayWeekDisplay); lcd.print(" "); lcd.setCursor(3, 1); // seconds, minutes, hours, day of the week, day of the month, month, year myRTC.setDS1302Time(secondsSet, minutesSet, hoursSet, dayWeekSet, dayMonthSet, monthSet, 2021); } void set_year() { yearSet = myRTC.year; //next screen buttonState1 = digitalRead(buttonPin1); if (digitalRead(buttonPin1) == HIGH) { if (buttonActive == false) { buttonActive = true; buttonTimer = millis(); } if ((millis() - buttonTimer > longPressTime) && (longPressActive == false)) { longPressActive = true; //stop from hold click; } } else { if (buttonActive == true) { if (longPressActive == true) { longPressActive = false; } else { //next screen lcd.clear(); screens = 1; } buttonActive = false; } } //year up buttonState2 = digitalRead(buttonPin2); if (buttonState2 == HIGH) { lcd.setCursor(0, 1); yearSet += 1; lcd.setCursor(0, 1); delay(170); } //year down buttonState4 = digitalRead(buttonPin4); if (buttonState4 == HIGH) { lcd.setCursor(0, 1); yearSet -= 1; lcd.setCursor(0, 1); delay(170); } //print year lcd.setCursor(0, 0); lcd.print("Set year: "); lcd.setCursor(0, 1); lcd.print(yearSet); lcd.print(" "); lcd.setCursor(3, 1); // seconds, minutes, hours, day of the week, day of the month, month, year myRTC.setDS1302Time(secondsSet, minutesSet, hoursSet, dayWeekSet, dayMonthSet, monthSet, yearSet); } void alarmOnOff() { //next screen buttonState1 = digitalRead(buttonPin1); if (digitalRead(buttonPin1) == HIGH) { if (buttonActive == false) { buttonActive = true; buttonTimer = millis(); } if ((millis() - buttonTimer > longPressTime) && (longPressActive == false)) { longPressActive = true; //stop from hold click; } } else { if (buttonActive == true) { if (longPressActive == true) { longPressActive = false; } else { //next screen lcd.clear(); screens = 1; } buttonActive = false; } } //alarm on/off buttonState2 = digitalRead(buttonPin2); buttonState4 = digitalRead(buttonPin4); if (buttonState2 == HIGH || buttonState4 == HIGH) { lcd.setCursor(0, 1); Serial.println(alarmToggleSet); if (alarmToggleSet) { alarmToggleSet = false; } else { alarmToggleSet = true; } lcd.setCursor(0, 1); delay(170); } //print alarmToggle lcd.setCursor(0, 0); lcd.print("Alarm on / off: "); lcd.setCursor(0, 1); if (alarmToggleSet == true) { lcd.print("On"); } else { lcd.print("Off"); } lcd.print(" "); lcd.setCursor(3, 1 }