ESP32 with TDS Sensor
ESP32 with TDS Sensor Water Quality Monitoring using ESP32 TDS Sensor
Water quality plays a crucial role in drinking water safety, aquariums, hydroponics, agriculture, and industrial applications. One of the easiest ways to monitor water quality is by measuring the Total Dissolved Solids (TDS) using a TDS Sensor.
In this tutorial, you’ll learn how to interface a TDS Sensor with ESP32, understand its working principle, connect the hardware, upload the Arduino code, and calculate TDS values in PPM (Parts Per Million). This guide focuses on the ESP32 TDS Sensor for effective monitoring.
Whether you’re a beginner or an IoT enthusiast, this guide will help you build a reliable ESP32 Water Quality Monitoring System.
What is a TDS Sensor?
The ESP32 TDS Sensor provides accurate readings of water quality, ensuring safety and reliability in various applications.
5
A TDS (Total Dissolved Solids) Sensor measures the concentration of dissolved substances such as:
- Minerals
- Salts
- Metals
- Organic Matter
- Nutrients
The sensor outputs an analog voltage, which the ESP32 converts into digital values using its 12-bit ADC.
The measured value is represented in:
PPM (Parts Per Million)
Higher TDS values generally indicate higher concentrations of dissolved solids in water.
Components Required
| Component | Quantity |
|---|---|
| ESP32 Development Board | 1 |
| Gravity Analog TDS Sensor | 1 |
| TDS Probe | 1 |
| Jumper Wires | As Required |
| Breadboard | 1 |
| USB Cable | 1 |
| Arduino IDE | Installed |
ESP32 Pin Connection
| TDS Module | ESP32 |
|---|---|
| VCC | 3.3V |
| GND | GND |
| AOUT | GPIO32 |
Note: GPIO32 is an ADC pin and works perfectly for analog sensing.
Circuit Diagram
The wiring is very simple:
- TDS VCC → ESP32 3.3V
- TDS GND → ESP32 GND
- TDS Analog Output → GPIO32

How Does the TDS Sensor Work?
The probe measures the electrical conductivity (EC) of water.
Since dissolved minerals increase conductivity, the sensor estimates the TDS concentration.
The ESP32:
- Reads Analog Voltage
- Converts Voltage into ADC Value
- Applies Temperature Compensation
- Calculates TDS
- Displays the result in PPM
Arduino Code
Use the following Arduino sketch to measure the TDS value.
/*
ESP32 + TDS Sensor
*/
#define TDS_PIN 32
#define VREF 3.3
void setup() {
Serial.begin(115200);
analogReadResolution(12);
analogSetPinAttenuation(TDS_PIN, ADC_11db);
}
void loop() {
int adcValue = analogRead(TDS_PIN);
float voltage = adcValue * VREF / 4095.0;
float temperature = 25.0;
float compensationCoefficient = 1.0 + 0.02 * (temperature - 25.0);
float compensationVoltage = voltage / compensationCoefficient;
float tds =
(133.42 * compensationVoltage * compensationVoltage * compensationVoltage
-255.86 * compensationVoltage * compensationVoltage
+857.39 * compensationVoltage) * 0.5;
Serial.print("ADC: ");
Serial.print(adcValue);
Serial.print(" Voltage: ");
Serial.print(voltage);
Serial.print(" V");
Serial.print(" TDS: ");
Serial.print(tds);
Serial.println(" ppm");
delay(1000);
}Code Explanation
Step 1: Define Pins
#define TDS_PIN 32GPIO32 is configured as the analog input.
Step 2: Configure ADC
analogReadResolution(12);ESP32 provides a 12-bit ADC, giving values from 0 to 4095.
Step 3: Read Analog Value
int adcValue = analogRead(TDS_PIN);Reads the sensor output voltage.
Step 4: Convert ADC into Voltage
float voltage = adcValue * 3.3 / 4095.0;The analog reading is converted into voltage.
Step 5: Temperature Compensation
float compensationCoefficient = 1.0 + 0.02*(temperature-25.0);Water conductivity changes with temperature.
This formula compensates the reading to improve accuracy.
Step 6: Calculate TDS
The polynomial equation converts voltage into TDS value.
float tds =
(133.42*x*x*x
-255.86*x*x
+857.39*x)
*0.5;The result is displayed in PPM.
Water Quality Reference
| TDS (PPM) | Water Quality |
|---|---|
| 0–50 | Excellent |
| 50–150 | Good |
| 150–300 | Fair |
| 300–500 | Poor |
| Above 500 | Unsafe |
Sample Output
ADC: 1485
Voltage: 1.197 V
TDS: 165 ppm
Status: FairApplications
- Drinking Water Monitoring
- Smart Water Purifiers
- Aquarium Monitoring
- Hydroponics
- Fish Farming
- Water Treatment Plants
- IoT Water Quality Systems
- Environmental Monitoring
- Agriculture
- Smart Irrigation
Advantages of ESP32 with TDS Sensor
- Easy to Interface
- Low Cost
- High Accuracy
- Wi-Fi and Bluetooth Support
- Low Power Consumption
- Real-Time Monitoring
- IoT Ready
- Compact Design
Future Improvements
You can enhance this project by integrating:
- DS18B20 Temperature Sensor
- OLED Display
- Blynk IoT Dashboard
- MQTT Cloud Monitoring
- Firebase Database
- ThingSpeak Analytics
- Telegram Alerts
- Mobile App Notifications
- Data Logging to SD Card
Troubleshooting
No Readings
- Check wiring.
- Verify GPIO32 connection.
- Ensure the sensor is powered with the correct voltage.
Fluctuating Values
- Immerse the probe fully in water.
- Avoid air bubbles.
- Average multiple readings.
- Use stable power.
Incorrect TDS Values
- Calibrate the sensor using a standard calibration solution.
- Apply temperature compensation.
- Clean the probe regularly.
Conclusion
Interfacing a TDS Sensor with ESP32 is an excellent way to build an IoT-based water quality monitoring system. By measuring the Total Dissolved Solids (TDS) in water, you can determine water quality for applications such as drinking water, hydroponics, aquariums, agriculture, and industrial monitoring.
With built-in Wi-Fi and Bluetooth, the ESP32 makes it easy to send water quality data to cloud platforms, mobile apps, or web dashboards for real-time monitoring. This project serves as a strong foundation for advanced smart water management systems.

Leave a Reply
You must be logged in to post a comment.