ESP32-Based Water Quality Monitoring System with Ultrasonic Level Sensing
View and Edit Project In Cirkit Designer
Circuit Documentation
Summary
This circuit is designed to interface an ESP32 Devkit V1 microcontroller with an HC-SR04 Ultrasonic Sensor, a TDS Sensor Module, and a pH Degree Sensor Module for the purpose of measuring water level, total dissolved solids (TDS), and pH level. The circuit is powered by a 5V battery. The ESP32 reads the sensors' outputs and processes the data, which is then available for monitoring or further processing.
Component List
ESP32 Devkit V1
- Description: A microcontroller development board based on the ESP32 chip, featuring Wi-Fi and Bluetooth connectivity.
- Pins: 3V3, GND, D15, D2, D4, RX2, TX2, D5, D18, D19, D21, RX0, TX0, D22, D23, EN, VP, VN, D34, D35, D32, D33, D25, D26, D27, D14, D12, D13, VIN
HC-SR04 Ultrasonic Sensor
- Description: An ultrasonic distance sensor capable of measuring distances by emitting ultrasonic waves and timing their reflection.
- Pins: VCC, TRIG, ECHO, GND
TDS Sensor Module
- Description: A sensor module used to measure the total dissolved solids in water, indicating water quality.
- Pins: TDS Negative, TDS Positive, TDS A
pH Degree Sensor Module
- Description: A sensor module used to measure the pH level of a solution, indicating its acidity or alkalinity.
- Pins: pH Negative, pH Positive, pH A
5V Battery
- Description: A power source providing a stable 5V supply to the circuit.
- Pins: Positive, Negative
Wiring Details
ESP32 Devkit V1
- D22 connected to HC-SR04 Ultrasonic Sensor ECHO
- D23 connected to HC-SR04 Ultrasonic Sensor TRIG
- D34 connected to TDS Sensor Module TDS A
- D35 connected to pH Degree Sensor Module pH A
HC-SR04 Ultrasonic Sensor
- VCC connected to 5V Battery Positive
- TRIG connected to ESP32 Devkit V1 D23
- ECHO connected to ESP32 Devkit V1 D22
- GND connected to 5V Battery Negative
TDS Sensor Module
- TDS Positive connected to 5V Battery Positive
- TDS Negative connected to 5V Battery Negative
- TDS A connected to ESP32 Devkit V1 D34
pH Degree Sensor Module
- pH Positive connected to 5V Battery Positive
- pH Negative connected to 5V Battery Negative
- pH A connected to ESP32 Devkit V1 D35
5V Battery
- Positive connected to HC-SR04 Ultrasonic Sensor VCC, TDS Sensor Module TDS Positive, pH Degree Sensor Module pH Positive
- Negative connected to HC-SR04 Ultrasonic Sensor GND, TDS Sensor Module TDS Negative, pH Degree Sensor Module pH Negative
Documented Code
// Include necessary libraries
#include <Arduino.h>
// Define pins
#define TRIG_PIN 23 // Trigger pin for HC-SR04
#define ECHO_PIN 22 // Echo pin for HC-SR04
#define TDS_PIN 34 // Analog pin for TDS sensor
#define PH_PIN 35 // Analog pin for pH sensor
void setup() {
Serial.begin(115200); // Start serial communication
pinMode(TRIG_PIN, OUTPUT); // Set trigger pin as output
pinMode(ECHO_PIN, INPUT); // Set echo pin as input
}
void loop() {
// Read water level using ultrasonic sensor
long duration, distance;
// Clear the trigger
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
// Set trigger high for 10 microseconds
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
// Read the echo pin
duration = pulseIn(ECHO_PIN, HIGH);
// Calculate distance in cm (speed of sound = 340 m/s)
distance = (duration * 0.034) / 2; // Divide by 2 because the sound travels to the object and back
// Read TDS and pH values
int tdsValue = analogRead(TDS_PIN); // Read TDS value
int phValue = analogRead(PH_PIN); // Read pH value
// Print values to Serial Monitor
Serial.print("Water Level (cm): ");
Serial.println(distance);
Serial.print("TDS Value: ");
Serial.println(tdsValue);
Serial.print("pH Value: ");
Serial.println(phValue);
// Wait for a second before the next reading
delay(1000);
}
This code is designed to be uploaded to the ESP32 Devkit V1. It initializes the pins connected to the sensors, reads the distance from the HC-SR04 Ultrasonic Sensor, and the TDS and pH values from their respective sensors. The results are printed to the Serial Monitor every second.