Cirkit Designer LogoCirkit DesignerYour all-in-one circuit design IDE Launch Cirkit Designer App

ESP32-Based Rain and Obstacle Detection System with Relay Motor Control

View and Edit Project In Cirkit DesignerImage of ESP32-Based Rain and Obstacle Detection System with Relay Motor Control

Circuit Documentation

Summary

The circuit is designed to monitor environmental conditions such as voltage levels and rain presence while controlling a DC motor based on sensor inputs. It includes a voltage sensing mechanism, rain detection, and an IR sensor for object detection. The circuit uses an ESP32 microcontroller to process sensor data and control a 4-channel relay module that interfaces with a DC motor. Power management is handled by a 12V battery, a step-down power converter, and a solar charge controller connected to a solar panel. The circuit also includes a voltage sensor, an IR sensor, two rain sensor modules (detection and control boards), and passive components like a resistor and an electrolytic capacitor.

Component List

Power Components

Sensors

Actuators

Microcontroller

Passive Components

Wiring Details

Power Components

Sensors

Actuators

Microcontroller

Passive Components

Documented Code

#include <Arduino.h>

// Pin Definitions
const int voltageSensorPin = 34;    // Voltage sensor analog input
const int rainSensorPin = 35;       // Rain sensor analog input
const int irSensorPin = 32;         // IR sensor digital input
const int relay1Pin = 26;           // Relay channel 1 for DC motor
const int relay2Pin = 27;           // Relay channel 2 (for future use)
const int relay3Pin = 14;           // Relay channel 3 (for future use)
const int relay4Pin = 12;           // Relay channel 4 (for future use)

// Voltage sensor calibration constants
const float voltageReference = 3.3; // Reference voltage of ESP32 ADC
const int resolution = 4095;        // ADC resolution
const float voltageDividerRatio = 5.7; // Ratio of the voltage divider used in the sensor

void setup() {
  // Initialize serial communication
  Serial.begin(115200);

  // Set pin modes
  pinMode(voltageSensorPin, INPUT);
  pinMode(rainSensorPin, INPUT);
  pinMode(irSensorPin, INPUT);
  pinMode(relay1Pin, OUTPUT);
  pinMode(relay2Pin, OUTPUT);
  pinMode(relay3Pin, OUTPUT);
  pinMode(relay4Pin, OUTPUT);

  // Initialize relays to be off
  digitalWrite(relay1Pin, LOW);
  digitalWrite(relay2Pin, LOW);
  digitalWrite(relay3Pin, LOW);
  digitalWrite(relay4Pin, LOW);
}

void loop() {
  // Voltage sensing
  int voltageSensorValue = analogRead(voltageSensorPin);
  float batteryVoltage = (voltageSensorValue * voltageReference / resolution) * voltageDividerRatio;
  Serial.print("Battery Voltage: ");
  Serial.println(batteryVoltage);

  // Rain detection
  int rainSensorValue = analogRead(rainSensorPin);
  Serial.print("Rain Sensor Value: ");
  Serial.println(rainSensorValue);

  // IR sensor detection
  int irSensorValue = digitalRead(irSensorPin);
  Serial.print("IR Sensor State: ");
  Serial.println(irSensorValue);

  // Motor control based on rain and IR sensor input
  if (rainSensorValue > 500) {
    // If rain detected, stop the motor
    digitalWrite(relay1Pin, LOW);
    Serial.println("Rain detected, motor stopped.");
  } else if (irSensorValue == HIGH) {
    // If no rain and IR sensor detects an object, start the motor
    digitalWrite(relay1Pin, HIGH);
    Serial.println("IR detected, motor running.");
  } else {
    // If no rain and no IR detection, keep motor off
    digitalWrite(relay1Pin, LOW);
    Serial.println("No rain, no IR, motor stopped.");
  }

  // Add delays to reduce serial output spamming
  delay(1000);
}

This code is designed to run on an ESP32 microcontroller. It initializes the necessary pins for reading from the voltage and rain sensors, as well as for controlling the relays connected to the DC motor. The loop function reads