Other: Waterproof probe
Package includes:1 x TDS signal adapter board
1 x Waterproof TDS probe
1 x Analog sensor line
source code:
#define TdsSensorPin A1
#define kValue 1.8 //kValue = value of calibrator TDS / measurement to get TDS
#define VREF 5.0 // 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, copyIndex = 0;
Float averageVoltage = 0, tdsValue = 0, temperature = 25;
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
analogBufferTemp[copyIndex]= analogBuffer[copyIndex];
averageVoltage = getMedianNum(analogBufferTemp,SCOUNT) * (float)VREF / 1024.0; // read the analog value more stable by the median filtering algorithm, and convert to voltage value
Float compensationCoefficient=1.0+0.02*(temperature-25.0); //temperature compensation formula: fFinalResult(25^C) = fFinalResult(current)/(1.0+0.02*(fTP-25.0));
Float compensationVolatge=averageVoltage/compensationCoefficient; //temperature compensation
tdsValue=(133.42*compensationVolatge*compensationVolatge*compensationVolatge - 255.86*compensationVolatge*compensationVolatge + 857.39*compensationVolatge)*0.5*kValue; //convert voltage value to tds value
//Serial.print("voltage:");
//Serial.print(averageVoltage,2);
//Serial.print("V ");
Serial.print("TDS Value:");
Serial.print(tdsValue,0);
Serial.println("ppm");
}
}
Int getMedianNum(int bArray[], int iFilterLen)
{
Int bTab[iFilterLen];
For (byte i = 0; 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;
}