Arduino Dusk/dawn Clock Timer

485

2

4

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.

Share

    Recommendations

    • Backyard Contest

      Backyard Contest
    • Pets Challenge

      Pets Challenge
    • Colors of the Rainbow Contest

      Colors of the Rainbow Contest

    4 Discussions

    0
    None
    v.zitka

    Question 9 days 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
    None
    HzMeister

    5 months ago

    This is awesome! I was thinking about doing this exact thing a few months ago but didn't have the inclination to figure out the programming - let alone putting all this work to make a quality guide like this.. This is perfection. Thanks!

    0
    None
    PaulM734

    5 months ago

    Great work !