Hello! I have a project, Arduino-Based Water Quality Monitoring Device. I've got temperature, pH, turbidity, and TDS Sensor. Here is the code:
#include <OneWire.h>
#include <DallasTemperature.h>
#include "DFRobot_PH.h" //added from PH Code
#include <EEPROM.h> //Added from PH Code
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4); // I2C address 0x27, 20 column and 4 rows
#define ONE_WIRE_BUS 7
#define TdsSensorPin A1 //verify plugged in here
#define PH_PIN A2 //
#define VREF 5.0 // analog reference voltage(Volt) of the ADC 0ther option is 3.3/5.0
#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;
int sensorPin = A0; //added from turbidity
float averageVoltage = 0,tdsValue = 0,temperature = 25;
float voltage,phValue;
float Celcius=0;
float Fahrenheit=0;
float volt; //added from turbidity
float ntu; //added from turbidity
DFRobot_PH ph;
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
namespace pin {
const byte tds_sensor = A1;
const byte one_wire_bus = 7; // Dallas Temperature Sensor
const byte ph_sensor = A2;
}
void setup()
{
Serial.begin(115200);
ph.begin(); //added from ph
sensors.begin();
pinMode(TdsSensorPin,INPUT);
lcd.init(); // initialize the lcd
lcd.backlight();
}
void loop()
{
volt = 0; //beginning of added from turbidity
for(int i=0; i<800; i++)
{
volt += ((float)analogRead(sensorPin));
}
volt = volt;
if(volt > 777750){
ntu = 1;
}else{
ntu = -0.303*(volt)+235654;
} //end of added from turbidity
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 timepoint = millis(); //added from ph
if(millis()-timepoint>1000U){ //time interval added from ph
timepoint = millis(); //added from ph
voltage = analogRead(PH_PIN)/1024.0*5000; // read the voltage
phValue = ph.readPH(voltage,temperature); //added from ph
}
static unsigned long printTimepoint = millis();
if(millis()-printTimepoint > 800U)
{
printTimepoint = millis();
for(copyIndex=0;copyIndex<SCOUNT;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; //convert voltage value to tds value
// Serial.print("voltage:");
// Serial.print(averageVoltage,2);
//Serial.print("V ");
sensors.requestTemperatures();
Celcius=sensors.getTempCByIndex(0);
Fahrenheit=sensors.toFahrenheit(Celcius);
//Serial.print(compensationCoefficient);
//Serial.print("compensationCoefficient");
//Serial.print(temperature);
//Serial.print("temperature");
delay(1500);
Serial.print(millis());
Serial.print("time since start in milliseconds");
Serial.print(" C ");
Serial.print(Celcius);
Serial.print(" F ");
Serial.println(Fahrenheit);
Serial.print(volt );
Serial.print(" V ");
Serial.print(ntu );
Serial.print(" NTU ");
Serial.print("pH:");
Serial.print(phValue);
Serial.print(" TDS Value:");
Serial.print(tdsValue,0);
Serial.println("ppm");
}
ph.calibration(voltage,temperature); // calibration process by Serail CMD
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Temp:");
lcd.setCursor(5,0);
lcd.print(Celcius);
lcd.print(" C ");
lcd.setCursor(17,0);
if(Celcius>=25) lcd.print("BAD");
if(Celcius<25 & Celcius>50) lcd.print("OK");
lcd.setCursor(0,1);
lcd.print("Turb:");
lcd.setCursor(5,1);
lcd.print(ntu);
lcd.print(" FTU ");
lcd.setCursor(17,1);
if(ntu<0.5) lcd.print("OK");
if(ntu>=0.5) lcd.print("BAD");
lcd.setCursor(0,2);
lcd.print("pH:");
lcd.setCursor(5,2);
lcd.print(phValue);
lcd.setCursor(17,2);
if(phValue>=6.5 & phValue<=8.5) lcd.print("OK");
if(phValue<6.5) lcd.print("BAD");
if(phValue>8.5) lcd.print("BAD");
lcd.setCursor(0,3);
lcd.print("TDS:");
lcd.setCursor (5,3);
lcd.print(tdsValue, 0);
lcd.setCursor(12,3);
lcd.print("ppm");
lcd.setCursor(17,3);
if(tdsValue<1000) lcd.print("OK");
if(tdsValue>=1000) lcd.print("BAD");
}
float round_to_dp( float in_value, int decimal_place ) //begin
{
float multiplier = powf( 10.0f, decimal_place );
in_value = roundf( in_value * multiplier ) / multiplier;
return in_value;
}
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;
}
now, i'm pretty confident with my other sensors, and my problems is the TDS sensor
the tds sensor reads 87 ppm although it is not submerged in water, and when I put the probe in water mixed with soy sauce and salt, it only shows around 94 ppm, which I think should be far more than that.
can someone help me with this?