Here’s an example Arduino code for a basic hydroponic irrigation system that
monitors pH, EC, and water levels. The code assumes you are using analog
sensors for pH and EC, and digital sensors for water levels. It also controls
dosing pumps for nutrients A and B based on the pH and EC readings, and a
water pump for irrigation.
Arduino Code for Hydroponic Irrigation System
// Pin Definitions
const int pHPin = A0; // pH Sensor connected to analog pin A0
const int ECPin = A1; // EC Sensor connected to analog pin A1
const int waterLevelPin = 2; // Water level sensor connected to digital pin
D2
const int pHPumpPin = 3; // Acid Pump connected to digital pin D3
const int basePumpPin = 4; // Base Pump connected to digital pin D4
const int waterPumpPin = 5; // Water Pump connected to digital pin D5
const int nutrientAPumpPin = 6; // Nutrient A Pump connected to digital pin
D6
const int nutrientBPumpPin = 7; // Nutrient B Pump connected to digital pin
D7
// pH and EC thresholds
float minPH = 5.5; // Minimum pH threshold
float maxPH = 6.5; // Maximum pH threshold
float minEC = 1000; // Minimum EC (µS/cm) threshold
float maxEC = 3000; // Maximum EC (µS/cm) threshold
// Water Level Threshold
const int lowWaterLevel = 0; // Low water level indicator (LOW state)
const int highWaterLevel = 1; // High water level indicator (HIGH state)
void setup() {
// Start serial communication for debugging
Serial.begin(9600);
// Set pump pins as outputs
pinMode(pHPumpPin, OUTPUT);
pinMode(basePumpPin, OUTPUT);
pinMode(waterPumpPin, OUTPUT);
pinMode(nutrientAPumpPin, OUTPUT);
pinMode(nutrientBPumpPin, OUTPUT);
// Set water level sensor pin as input
pinMode(waterLevelPin, INPUT);
void loop() {
// Read sensor values
float pH = readPH(); // Read pH sensor value
float EC = readEC(); // Read EC sensor value
int waterLevel = digitalRead(waterLevelPin); // Read water level sensor
// Print sensor data to Serial Monitor for debugging
Serial.print("pH: ");
Serial.println(pH);
Serial.print("EC: ");
Serial.println(EC);
Serial.print("Water Level: ");
if (waterLevel == LOW) {
Serial.println("Low");
} else {
Serial.println("High");
// Check pH level and adjust
if (pH < minPH) {
// pH too low, add base
digitalWrite(pHPumpPin, HIGH); // Turn on acid pump to lower pH
delay(1000); // Run the pump for 1 second
digitalWrite(pHPumpPin, LOW); // Turn off acid pump
} else if (pH > maxPH) {
// pH too high, add acid
digitalWrite(basePumpPin, HIGH); // Turn on base pump to raise pH
delay(1000); // Run the pump for 1 second
digitalWrite(basePumpPin, LOW); // Turn off base pump
// Check EC level and adjust
if (EC < minEC) {
// EC too low, add nutrient A
digitalWrite(nutrientAPumpPin, HIGH); // Turn on nutrient A pump
delay(1000); // Run the pump for 1 second
digitalWrite(nutrientAPumpPin, LOW); // Turn off nutrient A pump
} else if (EC > maxEC) {
// EC too high, add nutrient B
digitalWrite(nutrientBPumpPin, HIGH); // Turn on nutrient B pump
delay(1000); // Run the pump for 1 second
digitalWrite(nutrientBPumpPin, LOW); // Turn off nutrient B pump
// Check water level
if (waterLevel == LOW) {
// Low water level, turn on water pump
digitalWrite(waterPumpPin, HIGH); // Turn on water pump
delay(2000); // Run the pump for 2 seconds
digitalWrite(waterPumpPin, LOW); // Turn off water pump
// Wait before reading sensors again
delay(5000); // 5 seconds delay between each cycle
float readPH() {
int sensorValue = analogRead(pHPin); // Read the pH sensor value (0-1023)
float voltage = sensorValue * (5.0 / 1023.0); // Convert to voltage (0-5V)
// Convert voltage to pH value (This may vary based on sensor calibration)
float pH = 7 + ((2.5 - voltage) * 3.5); // Adjust this formula based on your
sensor calibration
return pH;
float readEC() {
int sensorValue = analogRead(ECPin); // Read the EC sensor value (0-1023)
float voltage = sensorValue * (5.0 / 1023.0); // Convert to voltage (0-5V)
// Convert voltage to EC value (This will depend on the calibration of your
EC sensor)
float EC = voltage * 1000; // Example conversion, adjust based on your
sensor
return EC;
Explanation of the Code:
1. Pin Assignments: The analog and digital pins are assigned to the pH
sensor, EC sensor, water level sensor, and actuators (pumps and
valves).
2. Sensor Readings: The readPH() and readEC() functions convert the
analog sensor values into usable pH and EC readings. These
conversion formulas should be adjusted according to the calibration of
your specific sensors.
3. Control Logic:
o pH Control: If the pH goes below the minimum, an acid pump is
triggered to raise the pH. If it exceeds the maximum, a base
pump is triggered to lower the pH.
o EC Control: Nutrient A and B pumps are controlled based on the
EC level.
o Water Level: If the water level is low, a water pump is activated
to refill the reservoir.
4. Pump Timing: The pumps are activated for 1 second (or 2 seconds for
the water pump), but this can be adjusted according to your system's
requirements.
Key Notes:
Sensor Calibration: You need to calibrate your pH and EC sensors
before using this code, as the formulas for converting analog readings
to pH and EC values depend on your sensor's characteristics.
Timing: The delay times (1 second for pumps and 5 seconds for the
cycle) can be adjusted based on your system's needs.
This Arduino code is a basic framework for your hydroponic irrigation system.
You may need to fine-tune the sensor reading logic and pump control based
on your specific hardware and system design.