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

ESP32-Based Automatic Water Level Controller with Ultrasonic Sensor and OLED Display

View and Edit Project In Cirkit DesignerImage of ESP32-Based Automatic Water Level Controller with Ultrasonic Sensor and OLED Display

Circuit Documentation

Summary of the Circuit

This circuit is designed as an Automatic Water Level Controller. It uses an ESP32 microcontroller to monitor the water level through an HC-SR04 Ultrasonic Distance Sensor. Based on the detected water level, the ESP32 controls a 5V relay to turn a water pump on or off. The water level is also displayed on a 0.96" OLED screen. The circuit includes a resistor and a bc547 transistor to interface the ESP32 with the relay, and a 1N4007 diode for protection against voltage spikes from the relay coil.

Component List

ESP32

5V Relay

Water Pump

0.96" OLED

HC-SR04 Ultrasonic Distance Sensor

Resistor (1k Ohm)

1N4007 Rectifier Diode

bc547

Wiring Details

ESP32

5V Relay

Water Pump

HC-SR04 Ultrasonic Distance Sensor

0.96" OLED

Resistor (1k Ohm)

1N4007 Rectifier Diode

bc547

Documented Code

/*
 * Automatic Water Level Controller
 * This code controls a water pump based on the water level detected by an
 * ultrasonic sensor. It also displays the water level on an OLED display.
 */

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET    -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

#define TRIG_PIN 4
#define ECHO_PIN 2
#define PUMP_PIN 15

long duration;
int distance;

void setup() {
  pinMode(TRIG_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);
  pinMode(PUMP_PIN, OUTPUT);
  digitalWrite(PUMP_PIN, LOW);

  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    for(;;);
  }
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(SSD1306_WHITE);
  display.setCursor(0, 0);
  display.print("Water Level:");
  display.display();
}

void loop() {
  digitalWrite(TRIG_PIN, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);

  duration = pulseIn(ECHO_PIN, HIGH);
  distance = duration * 0.034 / 2;

  display.clearDisplay();
  display.setCursor(0, 0);
  display.print("Water Level: ");
  display.print(distance);
  display.print(" cm");
  display.display();

  if (distance < 10) {
    digitalWrite(PUMP_PIN, HIGH);
  } else {
    digitalWrite(PUMP_PIN, LOW);
  }

  delay(1000);
}

This code initializes the OLED display and sets up the pins for the ultrasonic sensor and the pump control. In the main loop, it triggers the ultrasonic sensor to measure the distance, updates the OLED display with the current water level, and controls the pump based on the water level. If the water level is below 10 cm, the pump is turned on; otherwise, it is turned off.