<repo_name>boikedamhuis/Arduino-soil-moisture-relay-controller<file_sep>/README.md
# Arduino Soil Moisture relay controller

Arduino example to control a relay based on moisture values. The basis for a smart garden irrigation system.
# Wiring relay

- Connect VCC to 3.3v
- Connect ground to ground
- Connect data pin to pin D4/GPIO2

![alt text](https://hackster.imgix.net/uploads/attachments/342703/image_pMi8PUoeFZ.png?auto=compress%2Cformat&w=1280&h=960&fit=max "Logo Title Text 1")
# Wiring soil moisture sensor

- Connect VCC to 3.3v
- Connect ground to ground
- Connect data pin to AO analog pin.

![alt text](https://www.electronicwings.com/public/images/user_images/images/NodeMCU/NodeMCU%20Interfaces/NodeMCU%20Soil%20Moisture/Soil_Moisture_Interfacing_Diagram.png "Logo Title Text
1")
Wire your pump to the relay. The pump will start pumping water as soon as the soil moisture sensor detects too less water. 

<file_sep>/Arduino-soil-moisture-relay-controller.ino
//-------------------------------------------------------------------
// Arduino example to control a relay based on moisture values
//-------------------------------------------------------------------


int moisture_pin = A0;
int relay_pin = 2;
int moisture_value;


//Chamge the minimum percentage here!
int minimum_percentage = 50;

void setup() {

  //Start serial connection
   Serial.begin(9600);

   pinMode(relay_pin, OUTPUT);
    
   Serial.println("Getting data from the sensor ...");

  //Delay so the sensor can initialize
   delay(2000);

}

void loop() {

   moisture_value= analogRead(moisture_pin);
   moisture_value = map(moisture_value,550,0,0,100);

   Serial.print("Mositure value : ");
   Serial.print(moisture_value);
   Serial.println(" %");

   if (moisture_value < minimum_percentage) {
    digitalWrite(relay_pin, HIGH);
   } else {
    digitalWrite(relay_pin, LOW);

   }

   delay(1000);

   }
