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 8Make Your Sensors Control Your Relays

Make Your Sensors Control Your Relays
«
  • green.jpg
  • red.jpg
Based on the values you obtained in step 6, we're going to make our Arduino flip a relay on and off depending on light intensity. Here's the code I used:

int moistureSensor = 0;
int lightSensor = 1;
int tempSensor = 2;
int moisture_val;
int light_val;
int temp_val;

void setup() {
Serial.begin(9600); //open serial port
pinMode (2, OUTPUT);
pinMode (7, OUTPUT);
pinMode (8, OUTPUT);
digitalWrite (2, LOW);
digitalWrite (7, LOW);
digitalWrite (8, LOW);
}

void loop() {

moisture_val = analogRead(moistureSensor); // read the value from the moisture sensor
Serial.print("moisture sensor reads ");
Serial.println( moisture_val );
if (moisture_val < 850)
{
Serial.println("turning on pump");
digitalWrite (7, HIGH);
delay(2000);
}
if (moisture_val > 850)
{
Serial.println("turning off pump");
digitalWrite (7, LOW);
delay(2000);
}

light_val = analogRead(lightSensor); // read the value from the photosensor
Serial.print("light sensor reads ");
Serial.println( light_val );
if (light_val < 850)
{
Serial.println("turning on lights");
digitalWrite (8, HIGH);
delay(2000);
}
if (light_val > 850)
{
Serial.println("turning off lights");
digitalWrite (8, LOW);
delay(2000);
}

temp_val = analogRead(tempSensor);
Serial.print("temp sensor reads ");
Serial.println( temp_val );
if (temp_val < 920)
{
Serial.println("turning on low-temperature LED");
digitalWrite (2, HIGH);
delay(2000);
}
if (temp_val > 920)
{
Serial.println("turning off low-temperature LED");
digitalWrite (2, LOW);
delay(2000);
}
}

For my particular setup, here's the responses I received:
-moisture-probe nails separated --> relay connected to output pin 7 flips, turning on anything plugged in to that plug. Relay should turn off when nails are touched together
-light sensor: turns on relay connected to output pin 8 when shaded, off when bright light / indirect sunlight shining on sensor
-temperature LED: turns off when you touch it for a few seconds or breathe steadily on it

I hooked up LEDs to test the relay without blowing out my fluorescent bulbs by switching them on and off too quickly...
« Previous StepDownload PDFView All StepsNext Step »

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!
65
Followers
18
Author:liseman
bicycles, gardening, and other important stuff