/***************************************************
DFRobot Gravity: Analog TDS Sensor / Meter For Arduino
<https://www.dfrobot.com/wiki/index.php/Gravity:_Analog_TDS_Sensor_/
_Meter_For_Arduino_SKU:_SEN0244>
Created 2017-8-22
By Jason <jason.ling@dfrobot.com@dfrobot.com>
GNU Lesser General Public License.
See <http://www.gnu.org/licenses/> for details.
All above must be included in any redistribution
/***********Notice and Trouble shooting***************
1. This code is tested on Arduino Uno and Leonardo with Arduino IDE 1.0.5 r2 and 1.8.2.
2. More details, please click this link:
<https://www.dfrobot.com/wiki/index.php/Gravity:_Analog_TDS_Sensor_/
_Meter_For_Arduino_SKU:_SEN0244>
****************************************************/
// also used https://www.youtube.com/watch?v=5Dp-XatLySM
#include <OneWire.h>
#include <DallasTemperature.h>
#include <SD.h>
#include <SPI.h>
#include "DFRobot_PH.h" //added from PH Code
#include <EEPROM.h> //Added from PH Code
File myFile;
#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 pinCS = 53; // Pin 10 on Arduino UNO
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);
void setup()
Serial.begin(115200);
ph.begin(); //added from ph
sensors.begin();
pinMode (pinCS, OUTPUT);
pinMode(TdsSensorPin,INPUT);
// SD Card Initialization
if (SD.begin())
{
Serial.println("SD card is ready to use.");
} else
Serial.println("SD card initialization failed");
return;
void loop()
volt = 0; //beginning of added from turbidity
for(int i=0; i<800; i++)
volt += ((float)analogRead(sensorPin)/1023)*5;
volt = volt/800;
volt = round_to_dp(volt,1);
if(volt < 2.8){
ntu = 1540;
}else{
ntu = -576.12*square(volt)+3393.2*volt-3443.2;
// ntu = -1120.4*square(volt)+5742.3*volt-4353.8;
} //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");
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
myFile = SD.open("test.txt", FILE_WRITE);
if (myFile){
myFile.print(millis());
myFile.print(",");
myFile.print(Celcius);
myFile.print(",");
myFile.print(Fahrenheit);
myFile.print(",");
myFile.print(phValue);
myFile.print(",");
myFile.print(volt);
myFile.print(",");
myFile.print(ntu);
myFile.print(",");
myFile.println(tdsValue,0);
//myFile.print(",");
myFile.close(); // close the file
//if the file didn't open, print an error:
else {
Serial.println("error opening test.txt");
} delay (300000); // use 500 for short term troubleshooting/lab testing,300000 for field testing
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;