This circuit is designed to monitor and respond to environmental conditions using a variety of sensors and actuators. It includes an Arduino UNO as the central microcontroller, which processes inputs from an IR sensor, a soil moisture sensor, an ultrasonic sensor, and a DHT11 temperature and humidity sensor. The circuit can activate a relay to control a water pump and a buzzer for alerts. The system is powered by a 9V battery and is capable of detecting soil moisture levels, presence of objects or animals via IR and ultrasonic sensors, and can measure temperature and humidity.
#include <DHT.h>
// Define pins for DHT sensor
#define DHTPIN 2
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
// Define pins for soil moisture sensor and relay
const int soilMoisturePin = A0;
const int relayPin = 8;
// Define pins for ultrasonic sensor and IR sensor
#define TRIG_PIN 3
#define ECHO_PIN 4
#define IR_SENSOR_PIN 5
#define BUZZER_PIN 6
// Define threshold values
const int moistureThreshold = 30;
const int maxMoistureValue = 1023;
// Variables for ultrasonic sensor
long duration;
int distance;
// Variables for IR sensor
bool irDetected = false;
void setup() {
// Initialize serial communication
Serial.begin(9600);
// Initialize DHT sensor
dht.begin();
// Set pin modes
pinMode(relayPin, OUTPUT);
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
pinMode(IR_SENSOR_PIN, INPUT);
pinMode(BUZZER_PIN, OUTPUT);
// Ensure the pump and buzzer are off initially
digitalWrite(relayPin, LOW);
digitalWrite(BUZZER_PIN, LOW);
}
void loop() {
// Delay for reading sensors
delay(2000);
// Read DHT sensor
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
// Check if DHT readings are valid
if (isnan(humidity) || isnan(temperature)) {
Serial.println("Failed to read from DHT sensor!");
} else {
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" *C");
}
// Read soil moisture value
int sensorValue = analogRead(soilMoisturePin);
int moisturePercentage = map(sensorValue, 0, maxMoistureValue, 100, 0);
// Print the moisture percentage to the serial monitor for debugging
Serial.print("Soil Moisture: ");
Serial.print(moisturePercentage);
Serial.println("%");
// Check if soil moisture is below the threshold
if (moisturePercentage > moistureThreshold) {
digitalWrite(relayPin, HIGH); // Turn on the pump
Serial.println("Pump ON");
} else {
digitalWrite(relayPin, LOW); // Turn off the pump
Serial.println("Pump OFF");
}
// Measure distance from the ultrasonic sensor
distance = measureDistance(TRIG_PIN, ECHO_PIN);
// Read IR sensor value
irDetected = digitalRead(IR_SENSOR_PIN) == LOW; // Assuming LOW means detection
// Check if an animal is detected (adjust distance threshold as needed)
if (distance < 100 || irDetected) {
activateBuzzer();
Serial.println("Animal Detected!");
} else {
deactivateBuzzer();
}
// Small delay to prevent too much serial output
delay(200);
}
int measureDistance(int trigPin, int echoPin) {
// Send a pulse to trigger the ultrasonic sensor
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Measure the duration of the echo pulse
long duration = pulseIn(echoPin, HIGH);
// Calculate the distance in cm
int distance = duration * 0.034 / 2;
return distance;
}
void activateBuzzer() {
digitalWrite(BUZZER_PIN, HIGH);
}
void deactivateBuzzer() {
digitalWrite(BUZZER_PIN, LOW);
}
This code is designed to run on the Arduino UNO and manages the various sensors and actuators in the circuit. It reads environmental data, controls a water pump based on soil moisture, and activates a buzzer if an animal is detected or if an object is too close to the ultrasonic sensor. The code includes functions for reading sensors, controlling the relay, and managing the buzzer.