3 Simple Ways to
Share What You Make

With Instructables you can share what you make with the world — and tap into an ever-growing community of creative experts.

PhotosPhotos

Share one or more photos of a project, recipe, or whatever you've made, quickly and easily.

Step by StepStep-By-Step

Share your step-by-step photos with text instructions of what you made so others can do it too!

VideoVideo

Share your how-to video. You'll need your embed code from a video site such as YouTube.

Garduino: Gardening + Arduino

Step 11Give Garduino Control

Give Garduino Control
Now that you've got everything built, it's time to let Garduino loose on your plant friends. Here's the code I used:

//include the datetime library, so our garduino can keep track of how long the lights are on
#include <DateTime.h>

//define analog inputs to which we have connected our sensors
int moistureSensor = 0;
int lightSensor = 1;
int tempSensor = 2;

//define digital outputs to which we have connecte our relays (water and light) and LED (temperature)
int waterPump = 7;
int lightSwitch = 8;
int tempLed = 2;

//define variables to store moisture, light, and temperature values
int moisture_val;
int light_val;
int temp_val;

//decide how many hours of light your plants should get daily
float hours_light_daily_desired = 14;

//calculate desired hours of light total and supplemental daily based on above values
float proportion_to_light = hours_light_daily_desired / 24;
float seconds_light = 0;
float proportion_lit;

//setup a variable to store seconds since arduino switched on
float start_time;
float seconds_elapsed;
float seconds_elapsed_total;
float seconds_for_this_cycle;

void setup() {
//open serial port
Serial.begin(9600);
//set the water, light, and temperature pins as outputs that are turned off
pinMode (waterPump, OUTPUT);
pinMode (lightSwitch, OUTPUT);
pinMode (tempLed, OUTPUT);
digitalWrite (waterPump, LOW);
digitalWrite (lightSwitch, LOW);
digitalWrite (tempLed, LOW);

//establish start time
start_time = DateTime.now();
seconds_elapsed_total = 0;

}
void loop() {
// read the value from the moisture-sensing probes, print it to screen, and wait a second
moisture_val = analogRead(moistureSensor);
Serial.print("moisture sensor reads ");
Serial.println( moisture_val );
delay(1000);
// read the value from the photosensor, print it to screen, and wait a second
light_val = analogRead(lightSensor);
Serial.print("light sensor reads ");
Serial.println( light_val );
delay(1000);
// read the value from the temperature sensor, print it to screen, and wait a second
temp_val = analogRead(tempSensor);
Serial.print("temp sensor reads ");
Serial.println( temp_val );
delay(1000);
Serial.print("seconds total = ");
Serial.println( seconds_elapsed_total );
delay(1000);
Serial.print("seconds lit = ");
Serial.println( seconds_light);
delay(1000);
Serial.print("proportion desired = ");
Serial.println( proportion_to_light);
delay(1000);
Serial.print("proportion achieved = ");
Serial.println( proportion_lit);
delay(1000);

//turn water on when soil is dry, and delay until soil is wet
if (moisture_val < 850)
{
digitalWrite(waterPump, HIGH);
}

while (moisture_val < 850)
{
delay(10000);
//thanks to JoshTW for the following, important correction
moisture_val = analogRead(moistureSensor);
}

digitalWrite(waterPump, LOW);

//update time, and increment seconds_light if the lights are on
seconds_for_this_cycle = DateTime.now() - seconds_elapsed_total;
seconds_elapsed_total = DateTime.now() - start_time;
if (light_val > 900)
{
seconds_light = seconds_light + seconds_for_this_cycle;
}

//cloudy days that get sunny again: turn lights back off if light_val exceeds 900. this works b/c the supplemental lights aren't as bright as the sun:)
if (light_val > 900)
{
digitalWrite (lightSwitch, LOW);
}

//turn off lights if proportion_lit>proportion_to_light, and then wait 5 minutes
if (proportion_lit > proportion_to_light)
{
digitalWrite (lightSwitch, LOW);
delay (300000);
}

//figure out what proportion of time lights have been on
proportion_lit = seconds_light/seconds_elapsed_total;

//turn lights on if light_val is less than 900 and plants have light for less than desired proportion of time, then wait 10 seconds
if (light_val < 900 and proportion_lit < proportion_to_light)
{
digitalWrite(lightSwitch, HIGH);
delay(10000);
}

//turn on temp alarm light if temp_val is less than 850 (approximately 50 degrees Fahrenheit)
if (temp_val < 850)
{
digitalWrite(tempLed, HIGH);
}

}

Note the inclusion of the datetime library, which you can install from here. Place the contents of the file in the libraries directory of the arduino folder and you should be good to go.  See the readme in the download if you run into problems.

Monitoring needs to be greatly improved, as Garduino should sit there doing nothing most of the time when it's working properly. You should be able to pull the moisture sensors from the soil and have them turn on within a few seconds (air = 0 conductivity between them), but you can't do this with the light sensor: it's just trying to turn on for 16 hours every 24 hours. Currently, connecting your Arduino to your computer and then monitoring the serial port will give you some meaningful feedback. But, this is resetting the time counter to 0, so you don't get any useful historical data unless you leave the Garduino with a computer connected during the period you want to monitor...
« Previous StepDownload PDFView All StepsNext Step »
8 comments
Nov 22, 2011. 1:58 PMmarcuantonio says:
I get these values, Im concerned that the seconds lit is not changing and hence the achieved is not changing either.

The lights are on however.

Is this normal?


moisture sensor reads 614
light sensor reads 107
temp sensor reads 489
seconds elapsed total = 0.00
seconds lit = 0.00
proportion desired = 0.58
proportion achieved = 0.00
moisture sensor reads 627
light sensor reads 118
temp sensor reads 490
seconds elapsed total = 27.00
seconds lit = 0.00
proportion desired = 0.58
proportion achieved = 0.00
Nov 22, 2011. 6:10 PMmarcuantonio says:
Yes, I do have a light connected. I lowered this value and that gave seems to have helped doing the seconds of light desired and achieved.


//update time, and increment seconds_light if the lights are on
seconds_for_this_cycle = DateTime.now() - seconds_elapsed_total;
seconds_elapsed_total = DateTime.now() - start_time;
if (light_val > 120)
May 25, 2011. 12:12 AMtracyscott says:
You need to install the DateTime library into your Arduino environment.

Download it from here:
http://www.arduino.cc/playground/Code/DateTime

On my Mac, I ended up putting it here:
$ pwd
/Applications/Arduino.app/Contents/Resources/Java/libraries

At the same level as the existing libraries:
$ ls
DateTime DateTimeStrings Ethernet LiquidCrystal SPI SoftwareSerial Stepper
DateTimeBad EEPROM Firmata Matrix Servo Sprite Wire

Restart Arduino and it should show up in the menus under:

Sketch > Import Library

Read more here:
http://www.arduino.cc/en/Main/Libraries
Mar 4, 2011. 11:53 AMTXTCLA55 says:
PROBLEM:

//establish start time
start_time = DateTime.now();
seconds_elapsed_total = 0;

Wont comply as "DateTime" was not declared in this scope.

Even though i added the file to arduino complier.
Mar 23, 2011. 2:48 PMjmarnocha says:
i am having the exact same issue. any thoughts????
Mar 25, 2011. 8:49 AMTXTCLA55 says:
Lisemsan, how do i do that? I haven't really done much work with my arduino since i bought it and i am not to found of programming...

Pro

Get More Out of Instructables

Already have an Account?

close

All Steps Viewing
View all steps of an Instructable on the same page when you're a Pro Member.

Upgrade to Pro today!
66
Followers
18
Author:liseman
bicycles, gardening, and other important stuff