ESP8266 NodeMCU with TDS Sensor (Water Quality Sensor)
To interface a TDS (Total Dissolved Solids) sensor with an ESP8266 NodeMCU using the Arduino IDE, you can follow these steps.
Components Needed:
ESP8266 NodeMCU
TDS sensor (e.g., Gravity TDS Sensor)
Analog pH meter kit (optional)
BNC to PH2.0 sensor board (optional)
Jumper wires
Breadboard
Wiring:
Connect the components as follows:
Connect the TDS sensor:
Connect the sensor's VCC pin to 5V on the NodeMCU.
Connect the sensor's GND pin to GND on the NodeMCU.
Connect the sensor's AOUT pin to an analog pin on the NodeMCU (e.g., A0).
If using an analog pH meter kit:
Connect the pH meter's BNC connector to the BNC to PH2.0 sensor board.
Connect the sensor board's GND pin to GND on the NodeMCU.
Connect the sensor board's PO pin to an analog pin on the NodeMCU (e.g., A1).
Arduino Code:
#define TdsSensorPin A0
#define VREF 3.3 // analog reference voltage(Volt) of the ADC
#define SCOUNT 30 // sum of sample point
int analogBuffer[SCOUNT]; // store the analog value in the array, read from ADC
int analogBufferTemp[SCOUNT];
int analogBufferIndex = 0;
int copyIndex = 0;
float averageVoltage = 0;
float tdsValue = 0;
float temperature = 23; // current temperature for compensation
// median filtering algorithm
int getMedianNum(int bArray[], int iFilterLen){
int bTab[iFilterLen];
for (byte i = 0; i<iFilterLen; i++)
bTab[i] = bArray[i];
int i, j, bTemp;
for (j = 0; j < iFilterLen - 1; j++) {
for (i = 0; i < iFilterLen - j - 1; i++) {
if (bTab[i] > bTab[i + 1]) {
bTemp = bTab[i];
bTab[i] = bTab[i + 1];
bTab[i + 1] = bTemp;
}
}
}
if ((iFilterLen & 1) > 0){
bTemp = bTab[(iFilterLen - 1) / 2];
}
else {
bTemp = (bTab[iFilterLen / 2] + bTab[iFilterLen / 2 - 1]) / 2;
}
return bTemp;
}
void setup(){
Serial.begin(115200);
pinMode(TdsSensorPin,INPUT);
}
void loop(){
static unsigned long analogSampleTimepoint = millis();
if(millis()-analogSampleTimepoint > 40U){ //every 40 milliseconds,read the analog value from the ADC
analogSampleTimepoint = millis();
analogBuffer[analogBufferIndex] = analogRead(TdsSensorPin); //read the analog value and store into the buffer
analogBufferIndex++;
if(analogBufferIndex == SCOUNT){
analogBufferIndex = 0;
}
}
static unsigned long printTimepoint = millis();
if(millis()-printTimepoint > 800U){
printTimepoint = millis();
for(copyIndex=0; copyIndex<SCOUNT; copyIndex++){
analogBufferTemp[copyIndex] = analogBuffer[copyIndex];
// read the analog value more stable by the median filtering algorithm, and convert to voltage value
averageVoltage = getMedianNum(analogBufferTemp,SCOUNT) * (float)VREF / 1024.0;
//temperature compensation formula: fFinalResult(25^C) = fFinalResult(current)/(1.0+0.02*(fTP-25.0));
float compensationCoefficient = 1.0+0.02*(temperature-25.0);
//temperature compensation
float compensationVoltage=averageVoltage/compensationCoefficient;
//convert voltage value to tds value
tdsValue=(133.42*compensationVoltage*compensationVoltage*compensationVoltage - 255.86*compensationVoltage*compensationVoltage + 857.39*compensationVoltage)*0.5;
//Serial.print("voltage:");
//Serial.print(averageVoltage,2);
//Serial.print("V ");
Serial.print("TDS Value:");
Serial.print(tdsValue,0);
Serial.println("ppm");
}
}
}
Instructions:
Copy the code into the Arduino IDE.
Upload the code to your ESP8266 NodeMCU.
Open the Serial Monitor to view the TDS and pH readings.
This example reads TDS values from the TDS sensor and pH values from the analog pH meter (optional). Adjust the pin configurations and scaling factors as needed for your specific setup. Note that the scaling factors provided are just examples and may need calibration based on your sensor's specifications.
water quality monitoring,water quality sensor,tds sensor,tds sensor water quality,water quality,ph sensor,water quality monitoring system,interface tds sensor with arduino,iot based water quality monitoring system,tds sensor with esp32,tds sensor arduino,tds meter with tds sensor and arduino for water quality,sensor for water quality monitoring,online water quality monitoring sensor,tds sensor based water quality meter,iot water quality monitoring