Automated FARM Arduino (fan,lights,water Pump)




Introduction: Automated FARM Arduino (fan,lights,water Pump)

Gardening is very long task taking time every day .

In this tutorial i will show how to make everything automated in a easy way .

The arduino will automatically detect if the plants need something.

-When it's getting dark the light will turn on.

-If the plants need water it's activate the water pump.

-Every few hours the fan will turn on to give some fresh air.

And a timelapse.

Step 1: What You Need :

Let's have look :

-Arduino uno


-Wire i like these wire they are already cut in different size but it's obviously optional

-Photo resistor

-Soil moisture

-Water pump


-FAN i used a 12v from an old computer: (zalman)

Diode 1n4007


-Light If you can't order this one check "12v light Photosynthesis"


To get the project funnier i added a raspberry pi with camera to get a timelapse, if you interrested i will add a tutorial to set it up (very easy)

Step 2: Getting Started

It can look complicated but if you do part by part is simple:

Soil moisture:

To mesure if the plants need water we gonna mesure the humidity .

One pin goes to 5V , other one to ground and third the anolog to A1

Light sensor:

This part will allow us to mesure if there is enough light in the room

One part is connected to A0 with the resistor connected to the ground.

The other one to the 5v


This will be the most complicated of the project if you don't want a fan just skip it.

I posted one photo without the relay to get better idea.

- One wire goes from the resistor to pin 3

-The GND of the fan goes on the middle pin of the transistor.

The + of the fan goes to the relay (middle pin)

The last pin of the transistor goes to gnd

The diode goes to the midlle pin of transistor and to the GND


The anode(+) goes to the relay

The other to the GND of the power supply

Water Pump:

Still the same the + goes to midlle on the relay and GND to GND of the power source

We almost done! Just need to connect the relay to the arduino and updload the code .

Step 3: Final Schema

I used 2 relay in the graphic fritzing does not have relay 4channel yet.

Important for the relay i used only one 12v power supply , i separate the wire positive and negative then solder 3 wire from the positive and connect each part on the relay.One is enough as everything does not turn on at the same time.

I used a 5v power supply for the arduino




IN1: PIN2 Activate the light

IN2: PIN5 Activate the water pump

IN3: none

IN4: PiN4 activate the FAN

Step 4: The Code

To finish upload the code to the arduinio.

I wrote comment on the line to modify if you want to change the value (when to turn on the light or the water pump or how long the fan must be on or off).

Don't pay attention to the name of the variable as they are not relevant .

int PROBE= 1;
int value= 0; 
int TIP120pin = 3; //for this project, 
int ledState = LOW;             // 
unsigned long previousMillis = 0;        // will store last time LED was updated
long OnTime = 3600000;           // milliseconds of on-time FAN modify here
long OffTime = 60000;          // milliseconds of off-time FAN modify here
 int sensorPin = A0;   // select the input pin 
int sensorValue = 0;  // variable to store the value coming from the sensor

void setup() 
  // set the digital pin as output:
  pinMode(TIP120pin, OUTPUT); // Set pin for output to control TIP120 Base pin    
pinMode(4, OUTPUT);
  pinMode(2, OUTPUT); //pin connected to the relay
  Serial.begin(9600); //sets serial port for communication
void loop()
  // check to see if it's time to change the state of the FAN
  unsigned long currentMillis = millis();
  if((ledState == HIGH) && (currentMillis - previousMillis >= OnTime))
    ledState = LOW;  // Turn fan off
    digitalWrite(4,LOW);//tur off the relay
    previousMillis = currentMillis;  // Remember the time
    digitalWrite(TIP120pin, ledState);  // Update
  else if ((ledState == LOW) && (currentMillis - previousMillis >= OffTime))
    ledState = HIGH;  // turn fan on
    digitalWrite(4,HIGH);// turn on the relay
    previousMillis = currentMillis;   // Remember the time
    digitalWrite(TIP120pin, ledState);    // Update 
  //code for the light
    sensorValue = analogRead(sensorPin);    
  Serial.println(sensorValue); //prints the values coming from the sensor on the screen
  if(sensorValue > 150) //Modify here for the light
  digitalWrite(2,HIGH); //turn relay ON
  else digitalWrite(2,LOW); //turn relay OFF
//code water pump
 value= analogRead(PROBE);  
 value= value/100;  //divided by 100
 if(value<5)  //modify here the value
   digitalWrite(5, HIGH);  
   digitalWrite(5, LOW);  

Step 5: Conclusion

If you respect the few steps in order will be very easy

My water pump is directly immerge into the water and the pipe deep in the ground , this is fine to get water everywhere in your farm but could be improve in order to control where the water is going.

Same for the soil moisture deep in the ground . Think to check it sometime because it gets damage fast .

The fan on the top to give fresh air or to renew it

For my use i kept the default value on the code but think maybe to modify it for the light to turn on when it's darker.

On the picture you can see it's a big mess i used to long cable so think carefully!

My plants grew up pretty fast but pay attention to what seeds you get mine are up to 90cm !

Looking forward for your comments of any improvement.

For the timelapse here



    • Water Contest

      Water Contest
    • Fix It! Contest

      Fix It! Contest
    • Tiny Home Contest

      Tiny Home Contest

    24 Discussions


    Question 6 months ago

    Hi Thomas,

    I'm working on your another project that using the Cayenne app. I can't seem to verify nor upload the code snippet #1 in the IDE. I was wondering if you have any idea why is it not working?

    1 more answer

    Also, how did you manage to connect the bulb to the relay?

    Hello Thomas,

    I would like to know if you had already thought to integrate an LCD screen to see the soil moisture level, temperature ...

    Thank you for your answer

    so i built the system and imported the code, and I turned it on and the relay light will turn on for the light sensor when it gets dark but it won't turn the light on, same with the pump and the fan. I have already checked the wiring and it looked good. I was wondering if you had any idea on what is wrong

    2 replies

    For the light maybe try change the variable of darkness , otherwise if none of three are working it looks like a material issue because they are working separately for example the fan is program to work a certain amount of time so nothing to do with the light or the pump

    Hello sorry for the delay, yes any seed more or less , i am not an expert but you will have to check how much light water etc they need

    thomas, thank you for this code. what seed can be plant ?

    also can you use a zener diode instead of the one you use?

    1 reply

    Yes there is nothing mandatory just check it's the right voltage .

    question on the relay vcc goes to 5v, where does that go since something is already plugged in. Also GND goes to GND, but which GND port?

    1 reply

    You have two 5v on the arduino same for GND and it doesn't matter its the ground .

    Hello Thomas, you didn't precise the value of the resistance which is connected to the fan, is it a 10k like the photoresistor?
    Thank you

    1 reply

    Hello , you can connect the fan directly to the relay without the transistor and resistor otherwise 10k will do.

    I've copied the code in and...

    Arduino: 1.8.1 (Windows 10), Board: "Arduino/Genuino Uno"

    sketch_feb14a:1: error: expected unqualified-id before '<' token

    <p>int PROBE= 1;<br>int value= 0;


    sketch_feb14a:1: error: expected unqualified-id before '<' token

    <p>int PROBE= 1;<br>int value= 0;


    sketch_feb14a:9: error: expected unqualified-id before '{' token



    C:\Users\Tom\AppData\Local\Temp\arduino_modified_sketch_490438\sketch_feb14a.ino: In function 'void loop()':

    sketch_feb14a:47: error: 'value' was not declared in this scope

    value= analogRead(PROBE);


    sketch_feb14a:47: error: 'PROBE' was not declared in this scope

    value= analogRead(PROBE);


    C:\Users\Tom\AppData\Local\Temp\arduino_modified_sketch_490438\sketch_feb14a.ino: At global scope:

    sketch_feb14a:60: error: expected unqualified-id before '<' token



    exit status 1
    expected unqualified-id before '<' token

    This report would have more information with
    "Show verbose output during compilation"
    option enabled in File -> Preferences.
    1 reply


    It was because some HTML tag got inside the code... ( the <p>)

    I changed it it will worked now

    excuse me Sir, i had an error with your coding. When I try to verify the coding using arduino software version 1.6.9, it fail. What should I do to fix this error?? you can refer the picture I upload

    1 reply

    You have no following command for "if" nor for "else" in about the middle of your picture. Either delete that or fix it by filling in what you want it to do.


    if (sensorvalue > 400)


    digitalWrite(relay, HIGH); // Or whatever you would like to happen next




    digitalWrite(relay, LOW);