Simple time switch with relay and RTC

This seemed a simple thing to achieve. What do I want: I need to do an experiment with rose plants and Far-Red light (730nm) and Red (660nm) LEDs called "End of Day light". Using this technique the stems should stretch, and I need to prove this. I an a rose breeder and happy tech enthousiast, but not a programmer.

Both lights need to be ON for 10 minutes right after the end of the day, say: the last state of dusk in succession: first 10 min. far red, then 10 minutes red light.

There are two approaches:

  1. use a light sensor to switch the relays on at a given light level and use the DELAY function
  2. use a Real time clock to trigger the relays at a given time.

Option 2) seems best since I will not have troubles with heavy clouds etc. I have searched and searched but cannot find a simple sketch that does the job.

Can anyone point me in the right direction?

Thanks, Peter

pjcvdpol:
...
Option 2) seems best since I will not have troubles with heavy clouds etc.
...

Plus sun eclipse or troubles with bird...

Indeed! The chickencoop is interesting. However: no internet in the greenhouse; so I need to think of something else Just for a quick first trial just 10 min + 10 min at e.g. 17:30 would work fine for starters. I can refine later....

sonnyyu:
Plus sun eclipse or troubles with bird...

http://forum.arduino.cc/index.php?topic=289149.msg2022563#msg2022563

pjcvdpol:
… However: no internet in the greenhouse;

No problem!

You could transfer US Navy database into local.

Yun/Yun shield support PostgreSQL,Mysql,Sqlite3,MSSQL,Sybase,MongoDB…

Yun/Yun shield supported DB

sonnyyu:
RTC is required setup correct time initially.

Using GPS to update RTC and then re-adjust RTC for current timezone.

http://forum.arduino.cc/index.php?topic=158728.0

Using long wave time signal/synchronize with atomic clock

http://forum.arduino.cc/index.php?topic=81501.msg614220#msg614220

Or low tech solutions:

sonnyyu:
Or low tech solutions:

Or a cheap high tech solution, the ZS-042 module (DS3231 based).
Edit - with the TimeAlarms library.

aarg:
Or a cheap high tech solution, the ZS-042 module (DS3231 based).
Edit - with the TimeAlarms library.

I do have a DS1307, and read about the timealarms library which sounds promising. Can you point me to a simple (IMHO the should be VERY simple) sketch which I can use as template?

Sorry for my Noob-status btw...

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
Serial.begin(9600);

//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() {

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

}
void FarRedOn(){
Serial.println(“Far Red lights on”);
digitalWrite(RELAY1,HIGH); // Turns ON Relay 1
delay(2000);
}

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
Serial.print(hour());
printDigits(minute());
printDigits(second());
Serial.println();
}

void printDigits(int digits)
{
Serial.print(":");
if(digits < 10)
Serial.print(‘0’);
Serial.print(digits);
}

I would use a photoresistor. You set up the resistor, then log its values, find what value you want. Then you can do,
if(photoresistor > value){
digitalWrite(relay, LOW)}
if(photoresistor <= value){
digitalWrite(relay, HIGH);
delay(600000);}
if(relay == High){
digitalWrite(relay, LOW);
delay(36000000);}

simple, will turn on at the same light level everyday.

Please provide more information on your relays and how they are wired.

It's clear from your code that you think they are "active high" relays and you have them wired as "normally open".

You are seeing the opposite behaviour, so it's very possible the relays are "active low" and you are not treating the setup and on/off logic correctly. Or you indeed have "active high" relays and they are wired to be normally closed.

cattledog:
Please provide more information on your relays and how they are wired.

It's clear from your code that you think they are "active high" relays and you have them wired as "normally open".

You are seeing the opposite behaviour, so it's very possible the relays are "active low" and you are not treating the setup and on/off logic correctly. Or you indeed have "active high" relays and they are wired to be normally closed.

It is a double 10A, 250V relay with optocoupler and diode:

I have wired one "normally closed", the second is unwired. I chose NC since that is the state they should be in most of the day...

I think there is a misunderstanding of the meaning of normally closed(NC) and normally open (NO).

In the unactivated state for the relay coil, NC means that the internal power switch is closed and current will flow through the circuit and your lights will be on. NO means that the internal power switch is open and no current will flow through the circuit and the lights will be off.

When the relay coil is activated by 5v with the digitalWrite(relay,HIGH) it causes the internal power switch to change state. NC will open and no current can flow. NO will close and current can flow.

Given what you have said in your comments about lights on and off I think you want the relays to be wired Normally Open (NO).

Thank you, Cattledog, that is useful information. I have rewired the relay, and now things seem to work better. However: on the relay the two red LED's (one for each channel) are burning continuously when my lanp is off, when the lamp is on the LED goes off. To me it makes sense to have the LED's on if the channel is "hot" only, that is why I choose the other port on the relay.

Any suggestions how to "switch" this around?

many thanks,

Peter

Connect the LEDs to Vcc instead of ground (with the appropriate polarity, of course).

Can you get schematics for the relay module from the supplier?

Like you, I would expect that the indicator leds would be on when the coil is activated and off when the coil is not activated, and the indicators should be independent of how you have the load side wired. But, it's possible some designer somewhere thought that red means off.

How do you have the four pins on the module Vcc, In1, In2, Gnd connected?

I think I nailed it. I added a function "RelayOff before the normal cycle starts; that seems to do the trick. Now I need to add a RTC, I have one. How do I do this?

/*
A time switch based on the TimeAlarm example
I want to switch realy 1 on for 10 seconds then go off after which
Relay 2 is witched on for 10 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
Serial.begin(9600);

//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,2, RelayOff); // 19:00:2 every day

Alarm.alarmRepeat(19,0,10, FarRedOn); // 19:00:10 every day
Alarm.alarmRepeat(19,0,20,FarRedOff); // 19:00:20 every day

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

Alarm.alarmRepeat(19,0,36, FarRedOn2); // 19:00:36 every day
Alarm.alarmRepeat(19,0,46,FarRedOff2); // 19:00:46 every day

Alarm.alarmRepeat(19,0,49, RedOn2); // 19:00:49 every day
Alarm.alarmRepeat(19,0,59, RedOff2); // 19:00:59 every day

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

}

void loop() {

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

}
//Relays OFF
void RelayOff(){
Serial.println(“All Off”);
digitalWrite(RELAY1,HIGH); // Turns OFF Relay 1
digitalWrite(RELAY2,HIGH); // Turns OFF Relay 2
}

// Switch Lights

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

}

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

}

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

}

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

}

void FarRedOn2(){
Serial.println(“Far Red lights on”);
digitalWrite(RELAY1,LOW); // Turns ON Relay 1

}

void FarRedOff2(){
Serial.println(“Far Red lights off”);
digitalWrite(RELAY1,HIGH); // Turns Relay 1 Off

}

void RedOn2(){
Serial.println(“Red lights on”);
digitalWrite(RELAY2,LOW); // Turns ON Relays 2

}

void RedOff2(){
Serial.println(“Red lights off”);
digitalWrite(RELAY2,HIGH); // 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
Serial.print(hour());
printDigits(minute());
printDigits(second());
Serial.println();
}

void printDigits(int digits)
{
Serial.print(":");
if(digits < 10)
Serial.print(‘0’);
Serial.print(digits);
}

Please read the two posts at the top of this Forum by Nick Gammon on guidelines for posting here, especially the use of code tags ("</>") when posting source code files. Also, before posting the code, use Ctrl-T in the IDE to reformat the code in a standard format, which makes it easier for us to read.