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

ESP32-Based Water Quality Monitoring System with OLED Display and Relay Control

View and Edit Project In Cirkit DesignerImage of ESP32-Based Water Quality Monitoring System with OLED Display and Relay Control

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

  1. 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
  2. OLED 128x64 I2C Monochrome Display GND-VDD

    • Description: A monochrome OLED display with I2C interface.
    • Pins: GND, VDD, SCK, SDA
  3. DS18B20

    • Description: A digital temperature sensor.
    • Pins: signal, GND, vcc
  4. Resistor

    • Description: A resistor with a resistance of 220 Ohms.
    • Pins: pin1, pin2
  5. ADS1115

    • Description: A 16-bit ADC with I2C interface.
    • Pins: VDD, GND, SCL, SDA, ADDR, ALRT, A0, A1, A2, A3
  6. TDS Sensor Board

    • Description: A sensor board for measuring Total Dissolved Solids (TDS) in water.
    • Pins: GND, VCC, A
  7. 5V Adapter

    • Description: A 5V power adapter.
    • Pins: AC In 1, AC In 2, 5V, GND
  8. 1-Channel Relay (5V 10A)

    • Description: A relay module capable of switching high current loads.
    • Pins: NC, signal, C, power, NO, ground
  9. AC Bulb

    • Description: An AC-powered light bulb.
    • Pins: P, N
  10. AC Supply

    • Description: An AC power supply.
    • Pins: +ve, -ve

Wiring Details

ESP32

OLED 128x64 I2C Monochrome Display GND-VDD

DS18B20

Resistor

ADS1115

TDS Sensor Board

5V Adapter

1-Channel Relay (5V 10A)

AC Bulb

AC Supply

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