I have been developing my little greenhouse idea over the past few weeks. The aim is to automate everything required to grow plants (lights, watering, temperature control etc). I am currently simulating sunrise, midday and sunset with different types of lights and working on adding automatic watering etc.
Here's the relay box I built with which I'm able to control up to 8 individual main appliances (this thing took me forever...):
Here's a picture from the arduino and laptop sitting in the corner of the growroom (using the laptop and the webcam to make a fast forward movie of the plant growth, ):
Here's a picture taken from the webcam installed in the growroom:
For those interested, I am currently using a cold 200w envirolite fluorescent, couple of warm 15w fluorescents, one warm 80 watt flueroscent and a 160w uv tanning light.
And here's the code so far:
#include <DateTime.h>
#include <DateTimeStrings.h>
// time
#define TIME_MSG_LEN 11 // time sync to PC is HEADER and unix time_t as ten ascii digits
#define TIME_HEADER 255 // Header tag for serial time sync message
int cyear = 0;
int cmonth = 0;
int cday = 0;
int chour = 0;
int cminute = 0;
// LED
int ledPin = 13;
// Light Sensor (NJL7502L)
int lightSensor = 2;
// Relays
int relay_1 = 12;
int relay_2 = 11;
int relay_3 = 10;
int relay_4 = 9;
int relay_5 = 8;
static int relay_1val = 0; //off
static int relay_2val = 0; //off
static int relay_3val = 0; //off
static int relay_4val = 0; //off
static int relay_5val = 0; //off
int relaySO = 0; //relay serial override = off
// Soilmoisture Sensors
int moistSensor_1 = 0;
int moistSensor_2 = 1;
//lights -- lights must be off before midnight
int light_1 = 1; //light 1 is connected to relay 1 - midday - 6400k
int light_2 = 2; //light 2 is connected to relay 2 - sunrise light
int light_3 = 3; //light 3 is connected to relay 3 - uv lights
int light_1_start = 9; //hour to start light
int light_2_start = 8; //hour to start light
int light_3_start = 14; //hour to start light
int light_1_end = 22; //hour to end light
int light_2_end = 23; //hour to end light
int light_3_end = 16; //hour to end light
char cmd = 0;
int ack;
void setup()
{
Serial.begin(9600); // open serial
//set time
DateTime.sync(1233529585);
//relays off
pinMode(relay_1, OUTPUT);
digitalWrite(relay_1, LOW);
pinMode(relay_2, OUTPUT);
digitalWrite(relay_2, LOW);
pinMode(relay_3, OUTPUT);
digitalWrite(relay_3, LOW);
pinMode(relay_4, OUTPUT);
digitalWrite(relay_4, LOW);
pinMode(relay_5, OUTPUT);
digitalWrite(relay_5, LOW);
}
void loop()
{
lightcontrol();
delay(1000);
}
void lightcontrol()
{
getthetime();
if (relaySO == 0)
{
Serial.print("madetime");
if (chour >= light_1_start)
{
if (chour < light_1_end)
{
relay_1val = 0; //resetting it in case we had an override...
relay1toggle(); //turn light on
}
else
{
relay_1val = 1; //resetting it in case we had an override...
relay1toggle(); //turn light off
}
}
else
{
relay_1val = 1; //resetting it in case we had an override...
relay1toggle(); //turn light off
}
// light 2
if (chour >= light_2_start)
{
if (chour < light_2_end)
{
relay_2val = 0; //resetting it in case we had an override...
relay2toggle(); //turn light on
}
else
{
relay_2val = 1; //resetting it in case we had an override...
relay2toggle(); //turn light off
}
}
else
{
relay_3val = 1; //resetting it in case we had an override...
relay3toggle(); //turn light off
}
// light 3
if (chour >= light_3_start)
{
if (chour < light_3_end)
{
relay_3val = 0; //resetting it in case we had an override...
relay3toggle(); //turn light on
}
else
{
relay_3val = 1; //resetting it in case we had an override...
relay3toggle(); //turn light off
}
}
else
{
relay_3val = 1; //resetting it in case we had an override...
relay3toggle(); //turn light off
}
}
}
void relay1toggle() {
relay_1val ^= 1;
ledblink();
if (relay_1val)
{
Serial.println("Relay 1 on");
digitalWrite(relay_1, HIGH);
}
else
{
Serial.println("Relay 1 off");
digitalWrite(relay_1, LOW);
}
}
void relay2toggle() {
relay_2val ^= 1;
ledblink();
if (relay_2val)
{
Serial.println("Relay 2 on");
digitalWrite(relay_2, HIGH);
}
else
{
Serial.println("Relay 2 off");
digitalWrite(relay_2, LOW);
}
}
void relay3toggle() {
relay_3val ^= 1;
ledblink();
if (relay_3val)
{
Serial.println("Relay 3 on");
digitalWrite(relay_3, HIGH);
}
else
{
Serial.println("Relay 3 off");
digitalWrite(relay_3, LOW);
}
}
void relay4toggle() {
relay_4val ^= 1;
ledblink();
if (relay_4val)
{
Serial.println("Relay 4 on");
digitalWrite(relay_4, HIGH);
}
else
{
Serial.println("Relay 4 off");
digitalWrite(relay_4, LOW);
}
}
void relay5toggle() {
relay_5val ^= 1;
ledblink();
if (relay_5val)
{
Serial.println("Relay 5 on");
digitalWrite(relay_5, HIGH);
}
else
{
Serial.println("Relay 5 off");
digitalWrite(relay_5, LOW);
}
}
void ledblink()
{
digitalWrite(ledPin, HIGH);
delay(100);
digitalWrite(ledPin, LOW);
delay(100);
}
void getthetime(){
DateTime.available();
cyear = DateTime.Year + 1900;
cmonth = DateTime.Month + 1;
cday = DateTime.Day;
chour = DateTime.Hour;
cminute =DateTime.Minute;
}
If somebody knows how to program for the relays to be turned on and off in more detail (minutes) that would be great, or in fact any recommendations on how to make this code better are welcomed =D thanks for looking!
I'll update this post as I go along...
Current plans:
- add few fog makers and humidity sensor to keep humidity constantly high
- add automatic watering (half way through)
PS. growing tomatoes, cress, chillies and bamboos so far.


