Example Code for Arduino-Auto Flower Watering
Last revision 2025/12/25
This article provides detailed instructions for setting up an automated flower watering system using Arduino, including hardware requirements, wiring tips, and sample code to control a pump based on soil moisture levels.
Hardware Preparation
| Name | Model/SKU | Quantity | Purchase Link |
|---|---|---|---|
| 6xAA Battery Holder with DC2.1 Power Jack (FIT0141) | FIT0141 | 1 | Link |
| Free Life - EcoDuino Control Board | - | 1 | - |
| Moisture Sensor | SEN0114 | 1 | Link |
| Immersible Pump | - | 1 | - |
Software Preparation
- Development Tool: Arduino IDE (download from Arduino Official Website)
- Library: No additional libraries required
Wiring Diagram
- Soil moisture sensor:blue wire(A2),red wire(VCC),black wire(GND)
- Pump:brown wire( ),blue wire(-)
- The blue potentiometer is connected to the A1 pin of the main control board,The user can read the value and set the threshold for automatic watering.
Other Preparation Work
- Connect the Moisture Sensor to the EcoDuino Control Board's analog port A2
- Set the pump trigger threshold by modifying the
setHumidityvariable in the code
Sample Code
#define MOISTURE_PIN A2
int soilHumidity;
int setHumidity = 50; //Set the pump trigger threshold
void setup() {
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
}
void loop() {
soilHumidity = map(analogRead(MOISTURE_PIN), 0, 1023, 0, 100); //Map analog value to 0~100% soil moisture value
if (soilHumidity < setHumidity) {
pumpOn();
} else {
pumpOff();
}
}
//open pump
void pumpOn() {
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
}
//close pump
void pumpOff() {
digitalWrite(5, LOW);
digitalWrite(6, LOW);
}
Result
The pump turns on automatically when the soil humidity (read from the Moisture Sensor) is below 50, and turns off when the humidity is above 50.
Additional Information
This sample program does not use DHT11. You can set the threshold to turn ON/OFF the pump. When the soil moisture is lower than the threshold, it will turn ON the pump.
Was this article helpful?
