Arduino UNO Based Soil Moisture Monitoring and Servo Control System
View and Edit Project In Cirkit Designer
Circuit Documentation
Summary
This circuit integrates an Arduino UNO with a set of sensors and a servo motor to create a system capable of measuring soil moisture and distance to an object. The system uses a soil moisture module and a SparkFun Soil Moisture Sensor to determine the wetness of the soil, and an HC-SR04 Ultrasonic Sensor to measure distance. A servo motor is controlled based on the distance measured. The Arduino UNO serves as the central processing unit, running the embedded code to read sensor data, process it, and control the servo accordingly.
Component List
Arduino UNO
- Description: A microcontroller board based on the ATmega328P.
- Pins: UNUSED, IOREF, Reset, 3.3V, 5V, GND, Vin, A0-A5, SCL, SDA, AREF, D0-D13.
Soil Moisture Module
- Description: A sensor module used to measure the moisture content in soil.
- Pins: positive, negative, Analog, Digital, Ground, VCC.
Servo
- Description: An actuator that can be precisely controlled for angular rotation.
- Pins: gnd, vcc, pulse.
HC-SR04 Ultrasonic Sensor
- Description: A sensor used to measure distance via ultrasonic waves.
- Pins: VCC, TRIG, ECHO, GND.
SparkFun Soil Moisture Sensor
- Description: A simple breakout for measuring the moisture in soil and similar materials.
- Pins: VCC, GND, SIG.
Wiring Details
Arduino UNO
- 5V connected to HC-SR04 VCC and Servo vcc.
- GND connected to HC-SR04 GND, Soil Moisture Module Digital, and Servo gnd.
- A0 connected to Soil Moisture Module VCC.
- D12 connected to HC-SR04 TRIG.
- D11 connected to HC-SR04 ECHO.
- D8 connected to Servo pulse.
Soil Moisture Module
- Digital connected to Arduino UNO GND.
- VCC connected to Arduino UNO A0.
- positive connected to SparkFun Soil Moisture Sensor VCC.
- negative connected to SparkFun Soil Moisture Sensor GND.
Servo
- gnd connected to Arduino UNO GND.
- vcc connected to Arduino UNO 5V.
- pulse connected to Arduino UNO D8.
HC-SR04 Ultrasonic Sensor
- VCC connected to Arduino UNO 5V.
- TRIG connected to Arduino UNO D12.
- ECHO connected to Arduino UNO D11.
- GND connected to Arduino UNO GND.
SparkFun Soil Moisture Sensor
- VCC connected to Soil Moisture Module positive.
- GND connected to Soil Moisture Module negative.
Documented Code
#include <Servo.h>
Servo servo1;
const int trigPin = 12;
const int echoPin = 11;
long duration;
int distance = 0;
int potPin = A0; // Input pin for Soil Moisture Module
int soil = 0;
int fsoil;
void setup() {
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
servo1.attach(8);
}
void loop() {
// Reset distance and soil moisture average
distance = 0;
fsoil = 0;
// Take 2 measurements for distance
for (int i = 0; i < 2; i++) {
digitalWrite(trigPin, LOW);
delayMicroseconds(7);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance += duration * 0.034 / 2;
delay(10);
}
distance /= 2;
Serial.println(distance);
// If distance is within range, check soil moisture
if (distance < 15 && distance > 1) {
delay(1000);
// Take 3 measurements for soil moisture
for (int i = 0; i < 3; i++) {
soil = analogRead(potPin);
soil = constrain(soil, 485, 1023);
fsoil += map(soil, 485, 1023, 100, 0);
delay(75);
}
fsoil /= 3;
Serial.println(fsoil);
Serial.print("%");
// Control servo based on soil moisture
if (fsoil > 3) {
delay(1000);
Serial.print("WET ");
servo1.write(180);
delay(3000);
} else {
delay(1000);
Serial.print("dry ");
servo1.write(0);
delay(3000);
}
// Return servo to neutral position
servo1.write(90);
}
// Delay before next loop iteration
delay(1000);
}
This code is designed to run on the Arduino UNO. It initializes the servo and ultrasonic sensor, then enters a loop where it measures distance and soil moisture. If an object is detected within a certain range, it checks the soil moisture and rotates the servo to a position based on the moisture level. The servo returns to a neutral position after each check.