# The Code

{% hint style="info" %}
*If your UNO drivers aren’t installed automatically when using this kit, I will save you some time searching for the correct drivers by* [*downloading them here*](https://www.techstudio.co.za/wp-content/uploads/2021/08/CH341SER.zip)\_\_
{% endhint %}

Open up your Arduino IDE and connect your Arduino board to your PC.

Copy and paste the code below and upload the code.

{% hint style="danger" %}
Once the code is uploaded, disconnect the USB cable and switch on the battery. if you need to make changes to the code, please switch off the battery before you connect the USB cable to your PC.
{% endhint %}

```
//Original Code: https://www.instructables.com/Arduino-Plant-Watering-System/
//Amended by Armandus Basson
//For Arduino UNO
//Components / Kit: https://www.robofactory.co.za/home/232-automatic-smart-plant-watering-kit.html
 
int waterPump = 5; //Relay Pin = 5
 
void setup() {
  Serial.begin(9600);
  pinMode(waterPump, OUTPUT);
}
 
void loop() {
  int humidityRaw = analogRead(A2);
  int humidityReal = map(humidityRaw, 1023, 0 ,0, 100); //Convert raw sensor data to percentage
   
  //Print converted moisture sensor data (percentage) to the serial console
  Serial.print("Soil Moisture = ");
  Serial.print(humidityReal);
  Serial.println("%");
   
  delay(4000); //Delay before next sensor reading. Current set as 4 seconds
 
  if (humidityReal > 30) //Set threshold when relay must open for pump to feed water
  {
    digitalWrite(waterPump, HIGH);
    delay(5000); //Pump need to run for 5 seconds before the next sensor reading
  }else{
    digitalWrite(waterPump, LOW);
  }
}
```

### Credits

> Credit for this code needs to go to TechStudio
>
> <https://www.techstudio.co.za/2021/08/09/arduino-uno-smart-plant-watering-kit-code-driver/>
>
> Instructables Orignal Post
>
> <https://www.instructables.com/Arduino-Plant-Watering-System/>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://learn.arduinosa.co.za/arduino-kits/smart-plant-watering-kit/the-code.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
