ESP32 with TDS sensor and BLYNK
ESP32-Based Water Quality Monitoring System Using TDS Sensor and BLYNK
Introduction
Water quality is a crucial aspect of our daily lives, impacting our health, agriculture, and environment. Monitoring the Total Dissolved Solids (TDS) levels in water is essential for assessing its purity and suitability for various purposes. In this blog post, we’ll explore how to create a cost-effective and efficient water quality monitoring system using an ESP32 microcontroller, a TDS sensor, and the BLYNK IoT platform.
Understanding TDS
Total Dissolved Solids (TDS) refer to the amount of dissolved substances in water, including minerals, salts, and organic matter. High TDS levels can indicate contamination from pollutants or make water unsuitable for drinking or irrigation. By monitoring TDS, we can ensure the safety and quality of our water sources.
Components Required
- ESP32 Development Board: A powerful microcontroller capable of handling sensor data, communication, and IoT integration.
- TDS Sensor: Measures the conductivity of water, which is directly related to TDS levels.
- BLYNK IoT Platform: Provides a user-friendly interface for visualizing and controlling IoT devices.
- Power Supply: A 3.3V power supply for the ESP32 and TDS sensor.
- Breadboard: For prototyping and connecting the components.
- Jumper Wires: To connect the components on the breadboard.
Hardware Setup
- Connect the 3.3V power supply to the ESP32 and TDS sensor.
- Connect the TDS sensor’s output pin to an analog input pin on the ESP32.
- Ensure proper grounding for all components.
Software Setup
- Install the Arduino IDE: Download and install the Arduino IDE for ESP32 development.
- Install BLYNK Library: Add the BLYNK library to your Arduino IDE.
- Create a BLYNK Project: Sign up for a BLYNK account and create a new project.
- Obtain Authentication Token: Get the authentication token for your BLYNK project.
Code Implementation
C++
#define BLYNK_TEMPLATE_NAME "XXX"
#define BLYNK_AUTH_TOKEN "XXXXX"
#define BLYNK_TEMPLATE_ID "XXXXX"
// Comment this out to disable prints and save space
#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <WiFiClient.h>
#include <OneWire.h>
#include <DallasTemperature.h>
//#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "SSID";
char pass[] = "psw";
char auth[] = BLYNK_AUTH_TOKEN;
namespace pin {
const byte tds_sensor = 35;
const byte one_wire_bus = 15; // Dallas Temperature Sensor
}
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 waterTemp = 0;
float ecCalibration = 1;
}
OneWire oneWire(pin::one_wire_bus);
DallasTemperature dallasTemperature(&oneWire);
void setup() {
Serial.begin(115200); // Dubugging on hardware Serial 0
Blynk.begin(auth, ssid, pass);
// dallasTemperature.begin();
lcd.begin();
// Turn on the blacklight and print a message.
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("IOT TDS MTR V.1");
lcd.setCursor(0,1);
lcd.print("Loading.");
for(int x=0;x<10;x++)
{
lcd.print(".");
delay(300);
}
lcd.clear();
lcd.setCursor(0,0);
lcd.print("TEMP. : TDS");
}
void loop() {
Blynk.run();
readTdsQuick();
delay(2000);
}
void readTdsQuick() {
dallasTemperature.requestTemperatures();
//sensor::waterTemp = dallasTemperature.getTempCByIndex(0);
sensor::waterTemp=25;
float rawEc = analogRead(pin::tds_sensor) * device::aref / 1024.0; // read the analog value more stable by the median filtering algorithm, and convert to voltage value
float temperatureCoefficient = 1.0 + 0.02 * (sensor::waterTemp - 25.0); // temperature compensation formula: fFinalResult(25^C) = fFinalResult(current)/(1.0+0.02*(fTP-25.0));
sensor::ec = (rawEc / temperatureCoefficient) * sensor::ecCalibration; // temperature and calibration compensation
sensor::tds = (133.42 * pow(sensor::ec, 3) - 255.86 * sensor::ec * sensor::ec + 857.39 * sensor::ec) * 0.5; //convert voltage value to tds value
Serial.print(F("TDS:")); Serial.println(sensor::tds);
Serial.print(F("EC:")); Serial.println(sensor::ec, 2);
Serial.print(F("Temperature:")); Serial.println(sensor::waterTemp,2);
Blynk.virtualWrite(V0,(sensor::tds));
lcd.setCursor(8,1);
lcd.print(sensor::tds);
Blynk.virtualWrite(V1,(sensor::ec));
Blynk.virtualWrite(V2,(sensor::waterTemp));
lcd.setCursor(0,1);
lcd.print(sensor::waterTemp);
//lcd.clear();
}
Explanation
- The code initializes the ESP32, connects to Wi-Fi, and authenticates with BLYNK.
- It reads the analog value from the TDS sensor.
- The raw analog value is converted to voltage.
- The voltage is used to calculate the TDS value using a suitable conversion formula.
- The calculated TDS value is sent to a virtual pin on the BLYNK dashboard.
- The TDS value is optionally printed to the serial monitor for debugging.
Visualization on BLYNK
Create a gauge widget on your BLYNK dashboard and associate it with the virtual pin where you’re sending the TDS value. This will provide a real-time visualization of the measured TDS levels.
Conclusion
By combining the capabilities of the ESP32, TDS sensor, and BLYNK, you can create a reliable and affordable water quality monitoring system. This system can be used in various applications, including home water filtration, agricultural monitoring, and environmental studies.
Leave a Reply
You must be logged in to post a comment.