ESP32-Based Water Quality Monitoring System with OLED Display and Relay Control
View and Edit Project In Cirkit Designer
Circuit Documentation
Summary
This circuit is designed to monitor water quality using a DS18B20 temperature sensor and a TDS sensor connected via an ADS1115 ADC. The readings are displayed on an OLED display. If the pH level exceeds a certain threshold, a relay is turned off and a warning indicator is activated.
Component List
ESP32
- Description: A powerful microcontroller with built-in Wi-Fi and Bluetooth capabilities.
- Pins: EN, VP, VN, D34, D35, D32, D33, D25, D26, D27, D14, D12, D13, GND, VIN, 3V3, D15, D2, D4, RX2, TX2, D5, D18, D19, D21, RX0, TX0, D22, D23, BOOT
OLED 128x64 I2C Monochrome Display GND-VDD
- Description: A monochrome OLED display with I2C interface.
- Pins: GND, VDD, SCK, SDA
DS18B20
- Description: A digital temperature sensor.
- Pins: signal, GND, vcc
Resistor
- Description: A resistor with a resistance of 220 Ohms.
- Pins: pin1, pin2
ADS1115
- Description: A 16-bit ADC with I2C interface.
- Pins: VDD, GND, SCL, SDA, ADDR, ALRT, A0, A1, A2, A3
TDS Sensor Board
- Description: A sensor board for measuring Total Dissolved Solids (TDS) in water.
- Pins: GND, VCC, A
5V Adapter
- Description: A 5V power adapter.
- Pins: AC In 1, AC In 2, 5V, GND
1-Channel Relay (5V 10A)
- Description: A relay module capable of switching high current loads.
- Pins: NC, signal, C, power, NO, ground
AC Bulb
- Description: An AC-powered light bulb.
- Pins: P, N
AC Supply
- Description: An AC power supply.
- Pins: +ve, -ve
Wiring Details
ESP32
GND connected to:
- OLED 128x64 I2C Monochrome Display GND
- 5V Adapter GND
- TDS Sensor Board GND
- 1-Channel Relay ground
- Resistor pin2
- DS18B20 GND
- ADS1115 GND
VIN connected to:
- 5V Adapter 5V
- TDS Sensor Board VCC
3V3 connected to:
- 1-Channel Relay power
- Resistor pin1
- OLED 128x64 I2C Monochrome Display VDD
- DS18B20 vcc
- ADS1115 VDD
D18 connected to:
- DS18B20 signal
D19 connected to:
- 1-Channel Relay signal
D21 connected to:
- OLED 128x64 I2C Monochrome Display SDA
- ADS1115 SDA
D22 connected to:
- OLED 128x64 I2C Monochrome Display SCK
- ADS1115 SCL
OLED 128x64 I2C Monochrome Display GND-VDD
GND connected to:
- ESP32 GND
- 5V Adapter GND
- TDS Sensor Board GND
VDD connected to:
- ESP32 3V3
SCK connected to:
- ESP32 D22
- ADS1115 SCL
SDA connected to:
- ESP32 D21
- ADS1115 SDA
DS18B20
signal connected to:
- ESP32 D18
GND connected to:
- ESP32 GND
vcc connected to:
- ESP32 3V3
Resistor
pin1 connected to:
- ESP32 3V3
pin2 connected to:
- ESP32 GND
ADS1115
VDD connected to:
- ESP32 3V3
GND connected to:
- ESP32 GND
SCL connected to:
- ESP32 D22
- OLED 128x64 I2C Monochrome Display SCK
SDA connected to:
- ESP32 D21
- OLED 128x64 I2C Monochrome Display SDA
A0 connected to:
- TDS Sensor Board A
TDS Sensor Board
GND connected to:
- ESP32 GND
VCC connected to:
- ESP32 VIN
A connected to:
- ADS1115 A0
5V Adapter
GND connected to:
- ESP32 GND
- OLED 128x64 I2C Monochrome Display GND
- TDS Sensor Board GND
5V connected to:
- ESP32 VIN
- TDS Sensor Board VCC
1-Channel Relay (5V 10A)
ground connected to:
- ESP32 GND
power connected to:
- ESP32 3V3
signal connected to:
- ESP32 D19
C connected to:
- AC Supply +ve
NO connected to:
- AC Bulb P
AC Bulb
P connected to:
- 1-Channel Relay NO
N connected to:
- AC Supply -ve
AC Supply
+ve connected to:
- 1-Channel Relay C
-ve connected to:
- AC Bulb N
Code Documentation
/*
* This Arduino Sketch monitors water quality using a DS18B20 temperature sensor
* and a TDS sensor connected via an ADS1115 ADC. It displays the readings on
* an OLED display. If the pH level goes above a certain threshold, it turns off
* a relay and activates a warning indicator.
*/
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_ADS1015.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
Adafruit_ADS1115 ads;
#define ONE_WIRE_BUS 18
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
#define RELAY_PIN 5
#define WARNING_PIN 4
const float PH_THRESHOLD = 7.0;
void setup() {
pinMode(RELAY_PIN, OUTPUT);
pinMode(WARNING_PIN, OUTPUT);
digitalWrite(RELAY_PIN, HIGH); // Relay off
digitalWrite(WARNING_PIN, LOW); // Warning off
Serial.begin(115200);
Wire.begin();
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.display();
delay(2000);
display.clearDisplay();
ads.begin();
sensors.begin();
}
void loop() {
sensors.requestTemperatures();
float temperature = sensors.getTempCByIndex(0);
int16_t adc0 = ads.readADC_SingleEnded(0);
float voltage = adc0 * 0.1875 / 1000;
float tdsValue = (133.42 * voltage * voltage * voltage - 255.86 * voltage * voltage + 857.39 * voltage) * 0.5;
float phValue = calculatePH(tdsValue);
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.print("Temp: ");
display.print(temperature);
display.println(" C");
display.print("TDS: ");
display.print(tdsValue);
display.println(" ppm");
display.print("pH: ");
display.print(phValue);
display.display();
if (phValue > PH_THRESHOLD) {
digitalWrite(RELAY_PIN, LOW); // Turn off relay
digitalWrite(WARNING_PIN, HIGH); // Turn on warning
} else {
digitalWrite(RELAY_PIN, HIGH); // Turn on relay
digitalWrite(WARNING_PIN, LOW); // Turn off warning
}
delay(2000);
}
float calculatePH