Cirkit Designer Logo
Cirkit Designer
Your all-in-one circuit design IDE
Home / 
Project Documentation

ESP32-Based Water Level and Temperature Monitoring System with OLED Display

Image of ESP32-Based Water Level and Temperature Monitoring System with OLED Display

Circuit Documentation

Summary

This circuit is designed to measure temperature and water volume using an ESP32 microcontroller, a DS18B20 temperature sensor, an HC-SR04 ultrasonic sensor, and display the data on a 0.96" OLED screen. It also features a buzzer and an LED for alerting purposes. The circuit is powered by a battery case.

Component List

ESP32

  • Microcontroller with WiFi and Bluetooth capabilities.
  • Utilized for controlling sensors, processing data, and driving the display.

DS18B20

  • A digital temperature sensor.
  • Provides temperature readings for the system.

Buzzer

  • An audible signaling device.
  • Used to alert the user at specified intervals.

HC-SR04 Ultrasonic Sensor

  • Measures distance via ultrasonic waves.
  • Used to determine the water level in a container.

0.96" OLED

  • A small display for visual output.
  • Shows temperature and water volume readings.

LED: Two Pin (red)

  • A basic red light-emitting diode.
  • Acts as a visual indicator alongside the buzzer.

Resistor

  • A 4700 Ohm resistor.
  • Used for current limiting or pull-up/pull-down purposes.

My battery case

  • A power source for the circuit.
  • Supplies the necessary voltage to the components.

Wiring Details

ESP32

  • 3V3 connected to the positive rail of the battery case and VCC pins of other components.
  • GND connected to the negative rail of the battery case and GND pins of other components.
  • D27 connected to the ECHO pin of the HC-SR04 Ultrasonic Sensor.
  • D14 connected to the TRIG pin of the HC-SR04 Ultrasonic Sensor.
  • D12 connected to the PIN of the buzzer.
  • D13 connected to the anode of the LED.
  • D15 connected to the signal pin of the DS18B20 through a 4700 Ohm resistor.
  • D21 (SDA) and D22 (SCK) connected to the OLED for I2C communication.

DS18B20

  • signal connected to D15 on the ESP32 through a 4700 Ohm resistor.
  • GND connected to the negative rail of the battery case.
  • vcc connected to the positive rail of the battery case.

Buzzer

  • PIN connected to D12 on the ESP32.
  • GND connected to the negative rail of the battery case.

HC-SR04 Ultrasonic Sensor

  • VCC connected to the positive rail of the battery case.
  • TRIG connected to D14 on the ESP32.
  • ECHO connected to D27 on the ESP32.
  • GND connected to the negative rail of the battery case.

0.96" OLED

  • GND connected to the negative rail of the battery case.
  • VDD connected to the positive rail of the battery case.
  • SCK connected to D22 on the ESP32.
  • SDA connected to D21 on the ESP32.

LED: Two Pin (red)

  • cathode connected to the negative rail of the battery case.
  • anode connected to D13 on the ESP32.

Resistor

  • pin1 connected to the positive rail of the battery case.
  • pin2 connected to the signal pin of the DS18B20 and D15 on the ESP32.

My battery case

  • + connected to the positive rail supplying power to the components.
  • - connected to the negative rail supplying power to the components.

Documented Code

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <NewPing.h>

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

#define ONE_WIRE_BUS 15
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

#define TRIG_PIN 14
#define ECHO_PIN 27
#define MAX_DISTANCE 200 // Maximum distance for the sensor (in cm)
NewPing sonar(TRIG_PIN, ECHO_PIN, MAX_DISTANCE);

#define BUZZER_PIN 12
#define LED_PIN 13

const float BOTTLE_RADIUS = 3.25; // Bottle radius in cm
const float BOTTLE_HEIGHT = 19.0; // Bottle height in cm
unsigned long previousMillis = 0;
const long interval = 17000; // 17 seconds

void setup() {
  Serial.begin(115200);
  
  // Initialize OLED
  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println(F("OLED failed to initialize"));
    for(;;);
  }
  display.clearDisplay();
  
  // Initialize temperature sensor
  sensors.begin();
  
  // Initialize buzzer and LED pins
  pinMode(BUZZER_PIN, OUTPUT);
  pinMode(LED_PIN, OUTPUT);
  
  // Set LED to low (use resistor to adjust brightness)
  digitalWrite(LED_PIN, LOW); // LED off initially
}

void loop() {
  // Read temperature from DS18B20
  sensors.requestTemperatures();
  float temperature = sensors.getTempCByIndex(0);

  // Read distance from HC-SR04
  float distance = sonar.ping_cm();

  // Calculate water volume in mL
  float height_water = BOTTLE_HEIGHT - distance; // Water height in cm
  float volume_water = 3.14159 * BOTTLE_RADIUS * BOTTLE_RADIUS * height_water; // Volume in cm³
  volume_water *= 1.0; // Volume already in mL (1 cm³ = 1 mL)

  // Display temperature and volume on OLED
  display.clearDisplay();
  display.setTextSize(2); // Increase text size
  display.setTextColor(SSD1306_WHITE);
  display.setCursor(0,0);
  display.print("Temp: ");
  display.print(temperature);
  display.println(" C");

  display.print("Volume: ");
  display.print(volume_water);
  display.println(" mL");

  display.display();

  // Timer for buzzer and LED
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;
    
    // Sound buzzer and turn on LED simultaneously
    digitalWrite(LED_PIN, HIGH); // Turn on LED
    tone(BUZZER_PIN, 1000); // Turn on buzzer at 1000 Hz frequency
    delay(3000); // Buzzer sound and LED on duration for 3 seconds
    noTone(BUZZER_PIN); // Turn off buzzer
    digitalWrite(LED_PIN, LOW); // Turn off LED
  }

  delay(500); // 0.5 second pause for loop
}

This code is designed to run on the ESP32 microcontroller. It initializes the OLED display, temperature sensor, and ultrasonic sensor in the setup() function. In the loop() function, it reads the temperature and calculates the water volume based on the distance measured by the ultrasonic sensor. The results are displayed on the OLED. Additionally, every 17 seconds, the buzzer sounds and the LED lights up for 3 seconds as an alert.