Overview: IoT Based TDS Meter using ESP8266
In this project, we will learn how to make our own IoT Based TDS Meter using NodeMCU ESP8266 & TDS Sensor for Water Quality Monitoring. The TDS value (Total Dissolved Solids) gives the sum of dissolved solids in water. These solids include for instance salts, minerals, and conductive metals ions. The value is additionally called the conductivity of the water. Because the more such solids or ions are within the water, the higher it conducts electricity. TDS meters typically quantify this conductivity in micro siemens or ppm. The latter stands for parts per million, i.e. the number of solid particles per a million water mixture particles. The value of 40 ppm means from a million particles there are 40 dissolved ions and therefore the rest (= 999 960) are water molecules.
The TDS Sensor can not only measure the Liquid TDS but also the EC (Electrical Conductivity). An electrical conductivity meter (EC meter) measures the electrical conductivity in a solution. It has multiple applications in research and engineering, with common usage in hydroponics, aquaculture, aquaponics, pisciculture, and freshwater systems to monitor the number of nutrients, salts, or impurities in the water.
To make our own IoT based TDS Meter, we will need a TDS Sensor as well as Temperature Sensor which should be Waterproof. For sending the measured TDS, EC & Temperature data, we need an IoT platform. The best free IoT platform is Blynk. The ESP8266 based TDS Meter connects to wifi and continuously sends the data to the Blynk application. Thus you can get a 24×7 report related to your water quality. Thus TDS sensor is the best sensor for Water Quality Monitoring. Apart from all this, you can also add a Ph Sensor and a Turbidity Sensor to learn more about the Water Quality.
Bill of Materials
We need the following components to make an IoT Based TDS Meter. The ESP8266 & TDS Sensor is required along with the Temperature Sensor. All the componets can be purchased online from Amazon. The components purchase link is given below.
| S.N. | Components | Quantity | Purchase Links |
|---|---|---|---|
| 1 | Nodemcu ESP8266 Board | 1 | Amazon | AliExpress |
| 2 | TDS Sensor | 1 | Amazon | AliExpress |
| 3 | DS18B20 Sensor | 1 | Amazon | AliExpress |
| 4 | Resistor 4.7K | 1 | Amazon | AliExpress |
| 5 | Connecting Jumper Wires | 10 | Amazon | AliExpress |
| 6 | Breadboard | 1 | Amazon | AliExpress |
TDS Meter Circuit Diagram & Connections
The connection diagram between NodeMCU ESP8266 & TDS Sensor is given below. The anlog pin of TDS Sensor is connected to A0 of NodeMCU. The VCC pin is connected to 3.3V & GND to GND.
We already discussed the basics of TDS Sensor and Arduino Interfacing in our previous tutorials. You can follow the basic guide here: Arduino TDS Meter.
The use of the DS18B0 Temperature Sensor is for calibration purposes. This is because the TDS value varies as per change in Temperature, so there is a need for temperature compensation as per the change in liquid temperature. This is done by DS18B20, Waterproof Temperature Sensor.
Blynk Application Setup
The IoT Water Quality Monitoring Project is incomplete without the Blynk Application. Blynk is designed for the Internet of Things. It can control hardware remotely, it can display sensor data, it can store data, visualize it, and do many other cool things.
Download and install the Blynk Application from Google Play Store. IOS users can download from the App Store. Once the installation is completed, open the app & sign-up using your Email id and Password.
Create your UI by dragging dropping and filling up details along with the virtual pin assignment. Once the UI is created, you can ask for the authentication Token by sending the mail. You will need the Authentication Token for the code.
Source Code/Program
The source code or program for IoT Based TDS Meter using TDS Sensor & ESP8266 is given below. You can upload this code to the NodeMCU Board. Before compiling the sketch, you need to add following library to the Arduino IDE.
From the code part, change the wifi SSID, Password & Blynk Authentication Code.
|
1 2 3 |
char auth[] = "******************************"; char ssid[] = "******************************"; char pass[] = "******************************"; |
The complete code is given below.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
#include <Blynk.h> #include <ESP8266WiFi.h> #include <BlynkSimpleEsp8266.h> char auth[] = "fqMvEno5LtFrd_UcIpyyc6U03ehbgeQU"; // You should get Auth Token in the Blynk App. char ssid[] = "Alexahome"; // Your WiFi credentials. char pass[] = "loranthus"; int DSPIN = D5; // Dallas Temperature Sensor namespace pin { const byte tds_sensor = A0; // TDS Sensor } namespace device { float aref = 3.3; } namespace sensor { float ec = 0; unsigned int tds = 0; float ecCalibration = 1; } void setup() { Serial.begin(115200); // Dubugging on hardware Serial 0 Blynk.begin(auth, ssid, pass); } void loop() { Blynk.run(); double waterTemp = TempRead(); waterTemp = waterTemp*0.0625; // conversion accuracy is 0.0625 / LSB 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 * (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(waterTemp,2); Serial.print(F("")); Blynk.virtualWrite(V1, sensor::tds); Blynk.virtualWrite(V2, sensor::ec); Blynk.virtualWrite(V3, waterTemp); delay(1000); } boolean DS18B20_Init() { pinMode(DSPIN, OUTPUT); digitalWrite(DSPIN, HIGH); delayMicroseconds(5); digitalWrite(DSPIN, LOW); delayMicroseconds(750);//480-960 digitalWrite(DSPIN, HIGH); pinMode(DSPIN, INPUT); int t = 0; while(digitalRead(DSPIN)) { t++; if(t > 60) return false; delayMicroseconds(1); } t = 480 - t; pinMode(DSPIN, OUTPUT); delayMicroseconds(t); digitalWrite(DSPIN, HIGH); return true; } void DS18B20_Write(byte data) { pinMode(DSPIN, OUTPUT); for(int i=0; i<8; i++) { digitalWrite(DSPIN, LOW); delayMicroseconds(10); if(data & 1) digitalWrite(DSPIN, HIGH); else digitalWrite(DSPIN, LOW); data >>= 1; delayMicroseconds(50); digitalWrite(DSPIN, HIGH); } } byte DS18B20_Read() { pinMode(DSPIN, OUTPUT); digitalWrite(DSPIN, HIGH); delayMicroseconds(2); byte data = 0; for(int i=0; i<8; i++) { digitalWrite(DSPIN, LOW); delayMicroseconds(1); digitalWrite(DSPIN, HIGH); pinMode(DSPIN, INPUT); delayMicroseconds(5); data >>= 1; if(digitalRead(DSPIN)) data |= 0x80; delayMicroseconds(55); pinMode(DSPIN, OUTPUT); digitalWrite(DSPIN, HIGH); } return data; } int TempRead() { if(!DS18B20_Init()) return 0; DS18B20_Write (0xCC); // Send skip ROM command DS18B20_Write (0x44); // Send reading start conversion command if(!DS18B20_Init()) return 0; DS18B20_Write (0xCC); // Send skip ROM command DS18B20_Write (0xBE); // Read the register, a total of nine bytes, the first two bytes are the conversion value int waterTemp = DS18B20_Read (); // Low byte waterTemp |= DS18B20_Read () << 8; // High byte return waterTemp; } |
IoT Based TDS Meter using ESP8266 for Water Quality Monitoring
After the code is uploaded, the Nodemcu tries connecting to network. Once, it is connected to wifi network, it will start pushing the data to Blynk Server.
You can take different samples of water and check the water TDS Value, EC Value, and also the temperature. Adding salts or any ionic solute can rapidly raise the TDS value.
The data can be monitored on the Blynk Application Dashboard.










4 Comments
What board did you pick and its settings?
I used the generic 8266 as its the only one that worked.
Though the temp sensor doesn’t. I also have to change the pin from “D5” to “5” and the sketch works for
EC and TDS but not Temp……..
me to, have same problem
hi, i need to do exactly like this project but seems like the tds value does not come out 🙁 can you help
can u make a tutorial videos?