Site icon Hackatronic

Water Quality Monitoring System: TDS Sensor + ESP32/Arduino

Water Quality Monitoring System TDS Sensor ESP32

Water Quality Monitoring System TDS Sensor & ESP32

Let’s get started with how to build a smart Water Quality Monitoring System using TDS sensor and ESP32 or Arduino. A TDS (Total Dissolved Solids) Sensor measures the total amount of dissolved solids in a liquid, typically water. Dissolved solids include inorganic salts and small organic substances. The measurement is expressed in parts per million (ppm) or milligrams per liter (mg/L).

TDS is an essential parameter for assessing water quality. A high TDS value may indicate contamination, while a very low value might suggest a lack of essential minerals.

TDS of water
TDS chart of water

Gravity Analog TDS Sensor

The Gravity Analog TDS Sensor is an Arduino-compatible kit designed for measuring Total Dissolved Solids (TDS) in water. Suitable for domestic water, hydroponics, and other water quality testing applications, it supports a wide input voltage range (3.3–5.5V) and outputs an analog signal (0–2.3V), making it compatible with both 3.3V and 5V control systems.

Key Features

Specifications

Usage Tips

TDS sensor for water
TDS sensor

Components of TDS Sensor

Working Principle of TDS Sensor

Applications of TDS Sensors

Water Quality Monitoring System by TDS sensor

You can combine the TDS sensor with other sensors (e.g., temperature sensor) to create a comprehensive water quality monitoring system. With further integration, you can send the data to IoT platforms for real-time monitoring and analysis.

Hardware Required

  1. ESP32 or Arduino development board
  2. TDS sensor module (e.g., Gravity TDS Sensor)
  3. Water container
  4. Jumper wires
  5. Power source (5V from USB cable)
  6. A display module (optional, e.g., OLED or LCD)
  7. Laptop with Arduino IDE

Interfacing TDS Sensor with Arduino

Interfacing a TDS sensor with Arduino is straightforward and requires basic connections. Below are the steps to set up the sensor and read TDS values.

TDS Sensor with Arduino Circuit
TDS Sensor with Arduino

Circuit Diagram and Connections:

Arduino Code

Here’s the code to read and display TDS values on LCD display:

#include <Wire.h>
#include <LiquidCrystal_I2C.h>

// Initialize the I2C LCD (address 0x27, 16 columns, 2 rows)
LiquidCrystal_I2C lcd(0x27, 16, 2);

// Define the TDS sensor pin
const int TDS_PIN = A0;

// Define calibration constant (adjust as per your sensor and setup)
float calibrationConstant = 0.5; // Specific to your sensor module

// Supply voltage for the TDS sensor (3.3V in this case)
float supplyVoltage = 3.3;

void setup() {
// Initialize the LCD
lcd.begin(16,2);
lcd.backlight();

// Display a startup message
lcd.setCursor(0, 0);
lcd.print("TDS Sensor Test");
lcd.setCursor(0, 1);
lcd.print("Initializing...");
delay(2000); // Wait for 2 seconds

// Clear the display
lcd.clear();

// Set up serial communication for debugging (optional)
Serial.begin(9600);
}

void loop() {
// Read the analog value from the TDS sensor
int analogValue = analogRead(TDS_PIN);

// Convert the analog value to voltage
float voltage = analogValue * (supplyVoltage / 1023.0);

// Calculate the TDS value in ppm
float tdsValue = (voltage / calibrationConstant) * 1000;

// Display the TDS value on the LCD
lcd.setCursor(0, 0);
lcd.print("TDS: ");
lcd.print(tdsValue, 1); // Display with 1 decimal place
lcd.print(" ppm");

// Print TDS value to Serial Monitor (optional)
Serial.print("TDS Value: ");
Serial.print(tdsValue, 1);
Serial.println(" ppm");

// Add a delay before the next reading
delay(1000); // 1-second delay
}

Calibrating the Sensor

Calibration ensures accuracy. To calibrate:

Interfacing TDS Sensor with ESP32

The ESP32 microcontroller is a powerful, Wi-Fi-enabled chip ideal for IoT applications. You can use it to read TDS values and transmit the data for monitoring or logging.

Water Quality Monitoring System using ESP32
TDS Sensor with ESP32

Circuit Diagram and Connections

Features of Water Quality Monitoring System

Software Implementation

Follow these steps to program the ESP32:

1. Install the Required Libraries

Watch this video for better understanding

Smart Water Quality Monitoring with TDS Sensor, ESP32, and Blynk IoT | DIY IoT Project

2. Code for Reading TDS Sensor
#define BLYNK_TEMPLATE_ID "YOUR_TEMPLATE_ID"
#define BLYNK_TEMPLATE_NAME "Water Quality Monitoring"
#define BLYNK_AUTH_TOKEN "YOUR_AUTH_TOKEN"

// Comment this out to disable prints and save space
#define BLYNK_PRINT Serial
#include <BlynkSimpleEsp32.h>
#include <WiFi.h>

#include <WiFiClient.h>
#include <LiquidCrystal_I2C.h>

// LCD configuration
LiquidCrystal_I2C lcd(0x27, 16, 2);

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "WiFi Username";
char pass[] = "WiFi Password";

char auth[] = BLYNK_AUTH_TOKEN;

namespace pin {
const byte tds_sensor = 34;
}

namespace device {
float aref = 3.3; // Vref, this is for 3.3v compatible controller boards, for Arduino use 5.0v.
}

namespace sensor {
float ec = 0;
unsigned int tds = 0;
float ecCalibration = 1;
}

void setup() {
Serial.begin(115200); // Debugging on hardware Serial 0
Blynk.begin(auth, ssid, pass);
// Initialize LCD
lcd.init();
lcd.backlight();
// Display "Tech Trends Shameer"
lcd.setCursor(3, 0);
lcd.print("Tech Trends ");
lcd.setCursor(3, 1);
lcd.print(" Shameer ");
delay(2000); // Wait for 3 seconds
// Clear the LCD and display "Water Quality Monitoring"
lcd.clear();
lcd.setCursor(1, 0);
lcd.print("Water Quality");
lcd.setCursor(3, 1);
lcd.print("Monitoring ");
lcd.setCursor(12, 0); // Adjust the position for the heart symbol 

delay(2000);
lcd.clear();

}

void loop() {
Blynk.run();
readTdsQuick();
delay(1000);
}

void readTdsQuick() {
// Read the raw analog value and convert to voltage
float rawEc = analogRead(pin::tds_sensor) * device::aref / 1024.0;

// Debugging: Print the raw analog value
Serial.print(F("Raw Analog Value: "));
Serial.println(rawEc);

// Adjust this offset based on the sensor's dry reading (without immersion)
float offset = 0.14; // Set this to the observed raw analog value in air

// Apply calibration and offset compensation
sensor::ec = (rawEc * sensor::ecCalibration) - offset;

// If the EC is below zero after adjustment, set it to zero
if (sensor::ec < 0) sensor::ec = 0;

// Convert voltage value to TDS value using a cubic equation
sensor::tds = (133.42 * pow(sensor::ec, 3) - 255.86 * sensor::ec * sensor::ec + 857.39 * sensor::ec) * 0.5;

// Debugging: Print the TDS and EC values
Serial.print(F("TDS: "));
Serial.println(sensor::tds);
Serial.print(F("EC: "));
Serial.println(sensor::ec, 2);
lcd.setCursor(0, 0);
lcd.print("TDS: ");

lcd.setCursor(4,0);
lcd.print(sensor::tds);

lcd.setCursor(0,1);
lcd.print("EC: ");

lcd.setCursor(4,1);
lcd.print(sensor::ec);

// Send data to Blynk virtual pins
Blynk.virtualWrite(V0, sensor::tds);
Blynk.virtualWrite(V1, sensor::ec);
}

Steps to Run the Program

Calibration

To ensure accurate readings:

Tips for Accurate Readings

Conclusion:

A TDS sensor, when interfaced with an ESP32 or Arduino, provides a versatile and efficient solution for real-time water quality monitoring. It simplifies the measurement and monitoring of TDS levels for environmental, agricultural, and industrial purposes. It is ideal for applications such as home aquariums and industrial water treatment systems. TDS sensor is useful in advanced and reliable water quality monitoring systems for a wide range of needs.

Air Quality Monitoring System – ESP32 + Blynk IoT Project

Exit mobile version