Introduction: Arduino Multiple Timer. Day Based and Alarm Based
So I was asked by someone if I can make something that will start a motor at a fixed time. Ok ... easy.
But the motor should start in each day at a different hour or perhaps Monday and Friday at an hour and Sunday not at all, or whenver he wants to program the motor to start. I thought this would make a nice project, especially that he came with the request to start three motors after all. After several discutions with several people I've ended up with a project that:
- Can have up to 6 "alarms". In fact the number can be bigger with small modifications of the code
- Can start up to 7 devices separatelly (each device being connected to a pin).
- Has a real time clock (DS1307)
- Is based on Arduino
- Saves the settings in EEPROM so that they will not be fogotten after a power failure.
For this project I have done two shields that are put on top of Arduino UNO.
One shield with the DS1307 RTC (I didn't have the time to wait for a ready made one)
One shield that has three buttons connected, an 16x2 LCD and three relays (the rest of the connections (4 of them) would have to be connected from the pins directly. I did provided the programming for them, but, since I did not needed more than 3 outputs, i did not made a greater shield :).
So for the functioning principle now:
The user can:
- Set up the current date and time and the current day of the week.
- Choose a time for each of the 6 possible alarms
- Choose the day that each alarm will be started
- Choose the time (in eaither secs or minutes) that an alarm can stay on
- Choose the outputs (relays) that will be started when each of the alarms will start.
All these will be shown on the 16x2 LCD screen and will be set up using the three buttons.
For example if I want the system to water my lawn Monday to Friday from 8.00 AM to 9.00 PM, Saturday to start heating the water in the pool at 8.20 (not that I have one ;) ) and stop at 9.00, and everyday to start the coffe making machine at 7.30 AM and stop it at 7.31, I would need to set up 3 alarms:
1. First alarm with the starting hour at 8.00 and a period of 60 mins that will run in the first six days of the week and will start two of the relays (if I have two pumps for the lawn)
2. The second would start at 8.20 and run for 40 mins and would start the relay number 3 and would run only Saturday
3. The third one would start each day at 7.30 and would run for 60 secs and would start the relay number 4 (if you have one connected)
Step 1: Making the Shields
The shields were made using the Toner transfer method, but any method would do. They were coroded using Feric Chloride.
1. The RTC
The first step for me was to make the RTC shield. If you are lucky you allready have one and you do not need to make it.
The schematic was made in Eagle and can be found in the download files.
As you can see, the shield has a battery holder (so that the time will be remembered if the power goes out). An Cr2032 battery is what I have chosen here. Also there is an variable resistor that can be used for an LCD, but in my project I did not used this one. It is there just in case I need it for some other project. There is also a reset button that can be put here to reset your Arduino in case you need it.
2. The relay shield
The relay shield can be seen in images before being coroded and after it was partially mounted with the electronic parts. The shreded paper is shreded just because I could not wait for the glue to harden and I used a cutter that was just not sharp enough :D.
The schematic and the board layout are in the attached files.
Step 2: The Code
The code was compiled using Arduino version 1.0.6.
The code uses the following libraries:
- Wire.h - to communicate with the DS1307 clock
- LiquidCrystal.h - to show the time on the screen and the menu
- math.h - this library is here just because I wanted to see/show how you can use it in stead of bit shifting, but can be easily removed and the few places were it is used can be replaces with bitshifting
- EEPROM.h - this is used to store the settings into the eeprom
The code is quite lengthy, but it takes about half of the available space in Arduino, so there is still room for improvements.
The entire code is in the attachement so I will just put some parts of it here just to explain a few things since the language used in comments is Romanian not English (well, what can I say ... I write what I speak :) ):
In the define portion of the program we have:
#define DS1307_I2C_ADDRESS 0x68 //this is the i2c address of the DS1307 chip
#define PIN_STG 8 //the arduino pin used for the left button in the menu
#define PIN_MENU 2 //the arduino pin used for the menu button
#define PIN_DR 10 //the arduino pin used for the rights button in the menu
#define PIN_COMMAND_A A1 //relay A
#define PIN_COMMAND_B A2 //relay B
#define PIN_COMMAND_C A3 //relay C
#define PIN_COMMAND_D A0 //relay D
#define PIN_COMMAND_E 7 //relay E
#define PIN_COMMAND_F 9 //relay F
#define PIN_COMMAND_G 13 //relay G
#define DESCHIS LOW //Deschis means open so that the relays will be normally opened
#define INCHIS HIGH //Inchis means closed so that when an alarm goes on, the relays will be closed
I would say that the rest of the code is straight forward. It contains some gibberish code that makes the microcontroller happy and does not take space so that I can move in the menu or read the settings etc.), but the rest is just Arduino code.
Enjoy the alarm. (the photos from this step are provided by a user of this project Hibs H).
Thanks to Hibs H for providing photos and for testing the project and giving me the feedback needed to solve some bugs. Especially he contributed to the possibility to add longer alarm times and to program the alarms across the entire day.