Author Options:

Automatic light switch with timer and Arduino Answered


I need to set up a test environment with End of Day lighting (EOD). I am a rose breeder and I read some interesting articles on the use of far-red light (740nm) followed by red light (660nm)  for a short while at EOD. The goal is to lengthen the stems of the roses by giving them EOD light in a sequence.

What I need to achieve is that by dusk (EOD) the far-red goes on for 10 minutes and then off, followed by the red light for 10 minutes and then off as well utill the next evening. 

I have the following hardware:
Arduino's (Uno and Mega)
LED's (red and far-red)
two relays to power the LED's transformer(s)
resistors etc.

However: I am not a programmer. Can somebody help me with the code needed for this trial? As far as I can see it should be reasonably straightforward.

Many thanks,




3 years ago

I have made the below sketch based on the TimeAlarm library. I want two relays to switch after each other:

meaning both off > 1 on for a given time > 1 off > 2 on for a given time > 2 off > netx cycle of almost 24 hours.

However: this does not work:

I get: Both on > 1 off > 1 on > 2 off > 2 on

Switching HIGH and LOW in the Relay code produces:

Both on > 1 off > 2 off

Now why are both on at the start and why does my sketch not work. Driving me nuts....

A time switch based on the TimeAlarm example
I want to switch realy 1 on for 5 seconds then go off after which
Relay 2 is witched on for 5 seconds and then off

However: this does not work as expected: both relays are ON immediately at 19:00

Eventually I want this to work with an RTC so I can use real time (I have a DS1307)

#include <Time.h>
#include <TimeAlarms.h>

// Pins
#define RELAY1 6
#define RELAY2 7

void setup() {

// Start Serial & set pin to output

//set relays
pinMode(RELAY1, OUTPUT);
pinMode(RELAY2, OUTPUT);

// start clock
setTime(19,0,0,9,5,15); // set time to 19:00 Sep 5 2015 I want to use an RTC eventually......

// create the alarms
Alarm.alarmRepeat(19,0,5, FarRedOn); // 19:05 every day
Alarm.alarmRepeat(19,0,10,FarRedOff); // 19:00:10 every day

Alarm.alarmRepeat(19,0,11, RedOn); // 19:00:11 every day
Alarm.alarmRepeat(19,0,16, RedOff); // 19:00:16 every day

Alarm.timerRepeat(15, Repeats); // timer for every 15 seconds
Alarm.timerOnce(10, OnceOnly); // called once after 10 seconds


void loop() {

Alarm.delay(1000); // wait one second between clock display

void FarRedOn(){
Serial.println("Far Red lights on");
digitalWrite(RELAY1,HIGH); // Turns ON Relay 1

void FarRedOff(){
Serial.println("Far Red lights off");
digitalWrite(RELAY1,LOW); // Turns Relay 1 Off

void RedOn(){
Serial.println("Red lights on");
digitalWrite(RELAY2,HIGH); // Turns ON Relays 2

void RedOff(){
Serial.println("Red lights off");
digitalWrite(RELAY2,LOW); // Turns Relay 2 Off

void Repeats(){
Serial.println("15 second timer");

void OnceOnly(){
Serial.println("This timer only triggers once");

void digitalClockDisplay()
// digital clock display of the time

void printDigits(int digits)
if(digits < 10)


3 years ago

Thanks for your thoughts , @downuner35m and @steveastrouk I am not really afraid of clouds: the level of darkness is really just before it gets really dark; but a timer could work as well for a quick trial....


3 years ago

So, only one shot per evening ?

As Downunder suggests, why not sense light level instead ? You'd really need a thing called a real time clock to do it by astronomical dusk timing.


3 years ago

What about bad weathr, big clouds?
A light sensor will react and turn your lights on.
IMHO it is easier to use a timer and to adjust the time ever two weeks or so.