Arduino Dusk/dawn Clock Timer

Introduction: Arduino Dusk/dawn Clock Timer

Summary:

This Arduino based timer can switch one 220V light at dusk, dawn or specified time.

Introduction:

Some of the lights in my house are automatically switched on at dusk, until either a pre-set time or until dawn (all night).

The location of the lights does not allow the use of a light sensor. The regular available clock timers switch on at a specific time. To switch on around dusk therefore requires regularly adjusting the timer program setting.

As a nice challenge, I decided to build a custom Arduino based stand alone timer instead. It uses a real time clock and the Dusk2Dawn library to determine the time at which the lights must be switched on or off. The enclosure for this timer is 3D printed and can be found on Thingiverse. The Arduino code for this project can be found on GitHub.

In the creation of this timer I got inspiration from many designs and circuits on the internet. My thanks to all the contributors that are not explicitly mentioned.

For readability partial diagrams are shown in the steps where needed, instead of a complete circuit diagram.

Alternative solutions:

Instead of a stand-alone timer, there are many solutions where a smart home automation system steers the lights. My goal was to have an independant solution, that does not depend on WIFI (or other) connectivity.

Restrictions:

The code provided with this project included a daylight saving changes implementation based on European daylight saving system.

Step 1: Parts List and Tools

Parts:

Total parts costs (excluding 3d print) approximately €30,-.

  • Arduino Nano V3 (compatible) without headers
  • Power supply 5V 0.6A (34 x 20 x 15mm)
  • Solid-state relay 5V - Active low - 2A 230VAC
  • Real time clock DS3231 (small)
  • 0.96” OLED display SPI 128*64 pixels
  • Rotary encoder - EC11 - 20mm
  • Knob 6mm shaft 15mm * 17mm
  • Breadboard printed circuit board,
  • 4* M3x25mm screws
  • 3d printed enclosure
  • Heat-shrink tubing
  • Wires
  • Screw terminal block (to connect neutral wires)

Tools required:

  • Soldering Iron
  • Solder Wire
  • Desoldering pump
  • Wire Strippers
  • Cutters
  • 3D printer (to print enclosure)
  • Assorted Small Tools

WARNING

This circuit works on 230v AC and if you are not accustomed to working with mains voltage or do not have ample experience in working with 230v AC Mains Voltage please stay away from this project.

I assume no responsibility for any loss or damage arising directly out of or as a consequence of following this project.

It is always advised to take proper care and precaution while working with AC Mains.

Step 2: Prepare OLED Display and Real Time Clock

The 3D printed enclosure is designed for minimal size. As a result, the headers of the OLED display and real time clock need to be removed.

In preparation for the next step, clear any remaining solder from the holes with the desoldering pump.

Step 3: Prepare Rotary Encoder

The rotary encoder has flimsy connectors. To prevent damage, mount a piece of printed circuit board to the encoder.

On the picture the ground connection (to top right and middle bottom) is already prepared as well.

Note: Ensure that the rotary encoder with printed circuit board fits in the enclosure without touching the Arduino. It may be required to grind the printed circuit board to get a snug fit.

Step 4: Enclosure

Print the three parts of the enclosure with a 3d printer. Refer to the instructions on Thingiverse.

Step 5: Disable Arduino Power LED (optional)

To prevent having a green glow in the timer, the power LED of the Arduino can be disabled.

Note that this modification is optional.

The modification to the Arduino Nano consists of removing the resistor next to the power led (see red circle in the picture).

Step 6: Power Supply + Solid State Relay

In this step the power supply and solid-state relay are combined and mounted in the bottom part of the enclosure.

Connections between the power supply and the relay are made at the bottom of these components. The screw terminal block of the relay will be used to connect to the Arduino.

Note: When making connections, ensure that the mounting holes of the solid-state relay are kept free.

  • Solder a connection wire between solid state relay A1 to one of the AC connections of the power supply
  • Solder a wire to the other AC connection of the power supply (this will be connected to the neutral screw terminal block in step 7)
  • Solder a wire between power supply -Vo to relay DC-
  • Solder a wire to connect power supply +Vo to relay DC+

Note: It may be required to shorten the leads on the power supply and relay to be able to fit in the enclosure.

Step 7: Arduino Nano + Power Supply + Solid-state Relay

In this step, the Arduino Nano is connected to the power supply and solid-state relay.

  • Cut two wires of approximately 70mm length. Strip 30mm of isolation on one side, and 4mm on the other side.
  • Solder the side with 30mm stripped isolation to the Arduino +5V and GND, with the wire sticking through
  • Cut two heat-shrink tubes of 20mm length and mount them over the 25mm stripped part. This isolates the wires up to the connection with the mount screw terminal block DC+ and DC- of the solid-state relay.
  • Note that the wires for GND and +5V need to cross to connect correctly to the relay screw terminal block.
  • Cut a wire of approximately 40mm lenght and strip 4mm of isolation of both ends. Solder one side to the A2 connection on the BACKSIDE of the Arduino, and connect the other side to the CH1 connection of the solid-state mount screw terminal block.

WARNING

The Arduino is powered directly from the stable +5V power supply instead of using the Arduino internal power regulator. Therefore, it is not safe to connect USB when the Arduino receives power from the power supply.

Always disconnect 230VAC mains before using the Arduino USB connection.

Step 8: Arduino Nano + Real Time Clock

In this step the real time clock is connected to the Arduino, partly by using the cables prepared in the previous step.

  • Solder the wire coming from Arduino GND (also connected to DC- of the relay) to ‘–‘ of the real time clock.
  • Solder the wire coming from Arduino +5V (also connected to DC+ of the relay) to ‘+’ of the real time clock.
  • Cut two wires of approximately 40mm length and strip 4mm of isolation of both ends.
  • Solder a wire between Arduino A4 and real time clock D (SDA).
  • Solder a wire between Arduino A5 and real time clock C (SCL).
  • Shape the wires of the real time clock to ensure they do not interfere with the rotary encoder. For this, the wires need to be at the bottom of the enclosure.

Step 9: Connect OLED Display

In this step the OLED SPI display is added to the Arduino.

  • Cut 2 wires of 65mm length and strip 4mm of isolation of both ends.
  • Solder a wire to the GND connection of the OLED display. Solder this wire to the heat-shrink tubing isolated wire coming from the Arduino GND (refer to step 4) and connect both wires to the DC- mount screw terminal block of the solid-state relay.
  • Solder a wire to the VCC connection of the OLED display. Solder this wire to the heat-shrink tubing isolated wire coming from the Arduino +5V (refer to step 4) and connect both wires to the DC+ mount screw terminal block of the solid-state relay.
  • Cut 5 wires of 65mm length and strip 4mm of isolation of both ends.
  • Solder a wire to connect D0 (CLK) to Arduino D10
  • Solder a wire to connect D1 (MOSI / DATA) to Arduino D9
  • Solder a wire to connect RES (RT) to Arduino D8
  • Solder a wire to connect DC to Arduino D11
  • Solder a wire to connect CS to Arduino D12

Note: The order of the display wires is not logical. This is the result of first using the Adafruit example, and then changing the connections because using D13 results in a red LED on the Arduino all the time.

Alternative

It is possible to use a ‘normal’ order for the SPI connections. For this, the Arduino program digital output definition in oledcontrol.cpp must be adjusted accordingly:

// Using software SPI

// pin definitions

#define CS_PIN 12

#define RST_PIN 8

#define DC_PIN 11

#define MOSI_PIN 9

#define CLK_PIN 10

Step 10: Rotary Encoder

The diagram shows the connections of the Arduino to the rotary encoder (encoder seen from the top).

  • Cut 4 wires of 45mm and strip 4mm of isolation of both ends.
  • Connect Arduino GND to top right and bottom middle connectors of the encoder
  • Connect Arduino D2 to bottom left of the encoder
  • Connect Arduino D3 to bottom right of the encoder
  • Connect Arduino D4 to top left of the encoder

Step 11: Installation in the Enclosure

Install all the electronics in the bottom part of the enclosure:

  • Slide the Arduino on the vertical slot
  • Slide the real time clock in the bottom compartment
  • Slide the power supply and relay in the top compartment, ensure the relay sits on its mounts.

Step 12: Connecting to Mains / Light to Be Switched

WARNING

Ensure taking proper care and precaution while working with AC Mains, ensure that the AC Mains is disconnected.

I assume no responsibility for any loss or damage arising directly out of or as a consequence of following this project.

  • Connect the AC Mains phase to the A1 (left)screw terminal block of the relay.
  • Connect the phase of the light to be switched to the B1 (right) screw terminal block of the relay.
  • Use a separate screw terminal block to connect the AC mains neutral wire, the light neutral wire and the neutral wire of the power supply.
  • For strain relief, mount a tie wrap around each of the power cables.

Step 13: Finishing the Enclosure

In this step mounting in the enclosure is completed

  • Slide the OLED display through the display mounting hole in the middle part of the enclosure.
  • Slide the rotary encoder through the hole in middle part, ensure that the anti-rotation lines up. Mount the rotary encoder using the included washer and nut.
  • Mount the top part of the enclosure and close the enclosure by mounting the four M3x25mm screws from the bottom.

Step 14: Programming the Arduino

WARNING

The Arduino is powered directly from the stable +5V power supply instead of using the Arduino internal power regulator. Therefore, it is not safe to connect USB when the Arduino receives power from the power supply.

Always disconnect 230VAC mains before using the Arduino USB connection.

Retrieve the Arduino timer program from GitHub.

This program uses the Arduino IDE, which can be obtained here.

The program uses the following additional libraries:

SSD1303Ascii

Arduino Wire library

Note that the dusk2dawn library is also used, but included as code due to a change in its interface.

To ensure correct dusk / dawn calculation, longitude and latitude and time zone must be set.

As described in the dusk2dawn example, an easy way to find the longitude and latitude for any location is to find the spot in Google Maps, right click the place on the map, and select "What's here?". At the bottom, you’ll see a card with the coordinates.

Longitude and latitude are hardcoded in the program, in Dusk2Dawn.cpp line 19 and 20:

/*  Latitude and longtitude of your location must be set here. 
*   
*  HINT: An easy way to find the longitude and latitude for any location is 
*  to find the spot in Google Maps, right click the place on the map, and 
*  select "What's here?". At the bottom, you’ll see a card with the 
*  coordinates.
*/
#define LATITUDE 52.097105; // Utrecht
#define LONGTITUDE 5.068294; // Utrecht

The time zone is also hardcoded in Dusk2Dawn.cpp line 24. By default it is set to Netherlands (GMT + 1):

/*  Enter your timezone (offset to GMT) here.
*/
#define TIMEZONE 1

When programming the Arduino for the first time, the EEPROM memory needs to be initialized. For this, change timer.cpp line 11 to do EEPROM initialization:

// change to true for first time programming
#define INITIALIZE_EEPROM_MEMORY false

Upload the program to the Arduino and boot the Arduino.

Disable EEPROM initialization and upload the program to the Arduino again. The timer will now remember the switch time settings when rebooted.

Step 15: Setting Up the Time and Switching Times

User interaction concepts:

  • Short press is used to confirm selections. Furthermore, in the main timer screen a short press switches the light on or off.
  • Long press is used to enter the menu from the main timer screen. Anywhere in the menu, a long press will return to the main timer screen.
  • ‘>’ Selection cursus. This cursor indicates the selected option in a menu.

Main timer screen

The main timer screen shows:

Day of the week Su

Current time 16:00

Current timer state and next switch time Timer OFF until 17:12

Dawn and dusk time Dawn 08:05 Dusk 17:10

Setting the correct time

Long press to enter the menu. The following options are shown:

Back
Set time
Week day program
Weekend program
Options

Select set time to set the date and time of the real time clock. Enter the correct values for:

Year
Month
Day
Time

The timer automatically determines the week day. Switching daylight saving time is also done automatically. Daylight saving is implemented for European timezone only.

Setting the timer program

The timer has 2 programs, one for week days, one for weekend. Note that Friday is considered part of the weekend, lights can stay on a bit longer.

Each timer has a switch on and switch off moment. The moment can either be:

  • Time: Exact specified time
  • Dawn: Switch based on calculated time of dawn
  • Dusk: Switch based on calculated time of dusk

For dusk and dawn it is possible to enter a correction value of 59 minutes before or after.

Examples:

To switch a light on all night, select switch on at (dusk + 10min), switch off at (dawn - 10min)

To switch a light on in the evening, select switch on at dusk, switch off at time: 22:30.

Options

In the options screen a timeout can be set for switching of the screen.

When the screen is switched off, pressing the rotary encoder knob will return to the main timer screen.

1 Person Made This Project!

Recommendations

  • Metal Contest

    Metal Contest
  • Lamps and Lighting Contest

    Lamps and Lighting Contest
  • 3D Printed Student Design Challenge

    3D Printed Student Design Challenge

23 Comments

0
steve.reeves270
steve.reeves270

Question 1 year ago

Hi I came across this little project and WOW it's fab Thank you for taking the time to build and share your design, Just what I was after for my garden lighting. I have one small issue, I can set the date and time's, I can set the on time manually without no problems but when I come to program the off time, it displays Time and when I press the button expecting to enter the hours and minutes like the on time does it just jumps back to the main menu and cannot put the hours and minutes in manually ? Any ideas what could be causing it ?

0
steve.reeves270
steve.reeves270

Answer 1 year ago

I've managed to figure it out, line 290 in oeledcontroll.cpp from 4 to five
if ((mSelection == 2 || mSelection == 5) && ///14/07/2020 change to 4 from 5

0
wcmiller5
wcmiller5

Reply 1 year ago

Hi sterv.reeves270. I started this a couple days ago and also had a couple problems. I made your change ( I also went to HW SPI ) and seem to be coming up with dawn being 16:22 and dusk 31:16 ? In my location and TZ it should be around dawn 6:21 and dusk 21:16. Both are around 10 hrs too large? Any ideas?

Also when I set the days of week on time to 14:55, it didn't come on and the until stayed at 16:44.

I have the D2D program working properly on a automated chicken coop door so I understand most of what is going on but I'm stymied by the menu stuff. Somehow 10 hrs (600 minutes) is getting into the calculations.


0
steve.reeves270
steve.reeves270

Reply 1 year ago

Hi
Have you set the time zone to your country ?
I live in the UK so mine is set to 0.
Also when you first programmed it did make the changes at line 11 of the timer.cpp on first upload and then re uploaded the code again with it set back to false ?.
Over the last couple of days I've been studying through the code to get a better understanding of what's going on. It's been working great now for a few days, I have it coming on at Dusk until midnight.
Steve

0
wcmiller5
wcmiller5

Reply 1 year ago

Thanks again, Yes it was the TimeZone. I have a lamp in the living room that has a timer that I have to relearn every season change. Hopefully this will end me having to make changes. I'm waiting on my SSD and I'll put in a project box and hide it under the couch so my wife doesn't see it. She doesn't appreciate the beauty of these gizmos.

0
steve.reeves270
steve.reeves270

Reply 1 year ago

These are the changes I've made, I like to comment at the top of the code so I can see any changes I've made. So far this is working perfect, I can set it all up with auto time or manual or even have auto on manual off. So not sure why it's not working your end. I also added 10K pullup resistors to to the rotary inputs and the button as I found it gave some issues with the movements and click
//###################################################################
//#Longitude and latitude are hardcoded in the program, in Dusk2Dawn.cpp line 19 and 20:
//#The time zone is also hardcoded in Dusk2Dawn.cpp line 24. By default it is set to Netherlands (GMT + 1):
//#When programming the Arduino for the first time, the EEPROM memory needs to be initialized. For this, change timer.cpp line 11 to do EEPROM initialization:
//#Cahnged line 31 in timer.ccp to digitalWrite(PINOUT, LOW); // Changed 14/07/2020 to LOW FROM HIGH timer.ccp
//#changed line 56 in timer.ccp to digitalWrite(PINOUT, mSwitchedOn ? HIGH : LOW); // Changed 14/07/2020 to LOW FROM HIGH timer.ccp
//###################################################################

If I'm reading this correct this should auto adjust the time when summer time ends ?so I'm just testing this part, but don't think that's working as I set the date close it and left it running and the time is still the same so looking into this bit once 2am is reached

0
wcmiller5
wcmiller5

Reply 1 year ago

I noticed that you didn't include your original change " line 290 in oeledcontroll.cpp but I made all these changes and still problems. I'm going to go back to original GIT and start again from scratch to make sure I haven't made some inadvertent other changes. I don;t think changing to HW SPI would make any difference. I'll get this updated on Sat and let you know then. Thanks for your continued help. If that doesn't get any positive results is there any way to get a copy of your code to use?

0
steve.reeves270
steve.reeves270

Reply 1 year ago

Yeah sorry about that, i was not near my PC at the time, This is why i like to add comments at the beginning of the code when I make changes.
I was going to suggest that you should start with new code with the changes I've made as mine is working really well and had no issues.
Yeah let me know how you get on and I can see about zipping up a copy and upload some where

0
wcmiller5
wcmiller5

Reply 1 year ago

I went back to original download, made all the changes AND.. Still have problems. I also looked into the DST and it looks like it is hard coded in as "mDayLightSaving". In my coopDoor project I used a standard library file "flex_DST", it is in the library manager. it allows you to set your DST rules depending where you live. For me currently DST is a secondary concern. If you could send the zip to: esp8266adabill@gmail.com I would appreciate it.

0
wcmiller5
wcmiller5

Reply 1 year ago

Steve, Hate to keep bothering you but I have another problem. When I set the time manually, ON 21:00 OFF 21:20 and go back to the menu, the light comes ON with the display saying: 20:50Timer OFF until 21:20
dawn 06:23 dusk 21:48 BUT the light is ON! I assume that when the display says Timer OFF until 21:21 that at the time of 20:50 the light should be OFF as indicated by the Timer OFF, But the light is ON? Any ideas? Do you have yours working properly?

0
steve.reeves270
steve.reeves270

Reply 1 year ago

Yeah mine is working perfect, I think what you will find is that is actually off, have you got a relay or LED connected to the A2 port ?
what I found is that some relay modules require a high to keep the relay off and then switch to lo to keep the relay on.
To start with I had an LED connected to mine and when the timer was saying off the LED was on and when the timer was saying timer on the led was off, now I’m not near my pc, but i think the part you need to change is in the timer.ccp if you look and see where it controls the A2 port just switch all the HIGH AND LOW around this was the LED will only light up when timer say on

hope this helps
Steve

0
wcmiller5
wcmiller5

Reply 1 year ago

I tried that and it works OK in manual when I set the ON OFF times, then when I go back to use Dusk to Dawn it comes on (I'm at 10:46 ) and the LED is ON! it says ON until 6:23. Should be OFF until 21:25.
Thinking it might have Dawn and Dusk reversed, I set om at Dawn and Off at Dusk. the LED went OFF and the OLED says OFF until 6:23.
reversing the logicat least turned the LED OFF.

0
steve.reeves270
steve.reeves270

Reply 1 year ago

I did not come across that problem, I’ve had mine set dusk until midnight, dusk till dawn, manual on and off times without no issues, I will be able to look a bit more into this Thursday night as I’m away from home at the moment, have you changed anything else in the code ?

0
wcmiller5
wcmiller5

Reply 1 year ago

changed 3 things:
1) changed to HW SPI from the SW SPI it came as
2) Made your original change to L200 oledcontrol.cpp
3) made the last change you suggested to the timer.cpp in
void Timer::update()
//digitalWrite(PINOUT, mSwitchedOn ? LOW : HIGH); // original
digitalWrite(PINOUT, mSwitchedOn ? HIGH : LOW); //modified to

I've added a lot of Serial.prints but have made no other actual code changes.
0
v.zitka
v.zitka

Question 2 years ago on Step 15

Hello, your project looks exactly like what I need for my garden lighting. I tried to assemble it. I suppose I have followed all the steps described in the project. Unfortunately, I can only set the time and date in the menu. The "week day program" will jump back to the start screen. The "Weekend program" displays "Switch at" for 2 seconds and the entire Arduino is blocked, LED L flashes quickly. Shouldn't there be a tip for what it could be?
Thank you!
Vaclav

0
wcmiller5
wcmiller5

Answer 1 year ago

check the latest comment about how to make some changes to correct the problem.

0
wcmiller5
wcmiller5

Reply 1 year ago

I had a similar problem and I converted the OLED over to HW SPI and it seems to work. I have other problems but it is now posting the main screen and menue when I long push.

0
HarryNak2
HarryNak2

1 year ago

Hi,
Just like Vaclav, I encounter the same exact problem:
Unfortunately, I can only set the time and date in the menu. The "week day program" will jump back to the start screen. The "Weekend program" displays "Switch at" for 2 seconds and the entire Arduino is blocked, LED L flashes quickly. Shouldn't there be a tip for what it could be?
Thank you!

0
JoaoAlmeida
JoaoAlmeida

Question 2 years ago

Can you give some details on about the encoder?

TIA
João