Loading
Measure how full a tank is from the lid with an HC-SR04, show the percentage on an OLED and sound an alarm before it overflows.
Guide
The sensor sits under the lid looking down. The closer the water, the shorter the echo. Two measurements you take once (empty and full) turn that distance into a percentage.
7 parts · about ₹1,123
What you need
Step 1 of 4
The HC-SR04 needs 5 V and its Echo pin answers with 5 V, but ESP32 pins only take 3.3 V. A divider brings it down:
Milestone
Sensor wired safely
Step 2 of 4
Step 3 of 4
| 1 | #include <Wire.h> |
| 2 | #include <Adafruit_GFX.h> |
| 3 | #include <Adafruit_SSD1306.h> |
| 4 | |
| 5 | const int TRIG = 5, ECHO = 18, BUZZER = 19; |
| 6 | const float EMPTY_CM = 100.0; // sensor to the tank bottom (measure yours) |
| 7 | const float FULL_CM = 15.0; // sensor to the water when it is "full" |
| 8 | const int ALARM_PCT = 95; |
| 9 | |
| 10 | Adafruit_SSD1306 oled(128, 64, &Wire, -1); |
| 11 | |
| 12 | float readCm() { // average 5 pings to calm the ripples |
| 13 | float sum = 0; int ok = 0; |
| 14 | for (int i = 0; i < 5; i++) { |
| 15 | digitalWrite(TRIG, LOW); delayMicroseconds(2); |
| 16 | digitalWrite(TRIG, HIGH); delayMicroseconds(10); |
| 17 | digitalWrite(TRIG, LOW); |
| 18 | unsigned long us = pulseIn(ECHO, HIGH, 30000); |
EMPTY_CM.FULL_CM. Keep it at least 15 cm: the sensor cannot see closer than about 2 cm, and splashes cause false readings.Milestone
Shows the right level
The percentage matches the tank within a few percent.
Step 4 of 4
Labs
All labs