Step 8Make Your Sensors Control Your Relays
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 Step | Download PDFView All Steps | Next Step » |
![]() |
Add Comment
|





















































