0% found this document useful (0 votes)
19 views7 pages

Arduino TDS Sensor Code Example

The document provides sample code for the DFRobot Gravity Analog TDS Sensor/Meter, detailing how to read and calibrate TDS values using a standard buffer solution. It includes class definitions, methods for setting parameters, reading values, and performing calibration via serial commands. The code is designed for use with Arduino and includes EEPROM storage for calibration values.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views7 pages

Arduino TDS Sensor Code Example

The document provides sample code for the DFRobot Gravity Analog TDS Sensor/Meter, detailing how to read and calibrate TDS values using a standard buffer solution. It includes class definitions, methods for setting parameters, reading values, and performing calibration via serial commands. The code is designed for use with Arduino and includes EEPROM storage for calibration values.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

GravityTDS.

cpp
/***************************************************
DFRobot Gravity: Analog TDS Sensor/Meter
<[Link]
_Meter_For_Arduino_SKU:_SEN0244>

***************************************************
This sample code shows how to read the tds value and calibrate it with
the standard buffer solution.
707ppm(1413us/cm)@25^c standard buffer solution is recommended.

Created 2018-1-3
By Jason <[Link]@[Link]@[Link]>

GNU Lesser General Public License.


See <[Link] for details.
All above must be included in any redistribution.
****************************************************/

#include <EEPROM.h>
#include "GravityTDS.h"

#define EEPROM_write(address, p) {int i = 0; byte *pp =


(byte*)&(p);for(; i < sizeof(p); i++) [Link](address+i, pp[i]);}
#define EEPROM_read(address, p) {int i = 0; byte *pp =
(byte*)&(p);for(; i < sizeof(p); i++) pp[i]=[Link](address+i);}

GravityTDS::GravityTDS()
{
this->pin = 35;
this->temperature = 25.0;
this->aref = 5.0;
this->adcRange = 1024.0;
this->kValueAddress = 8;
this->kValue = 1.0;
}

GravityTDS::~GravityTDS()
{
}

void GravityTDS::setPin(int pin)


{
this->pin = pin;
}

void GravityTDS::setTemperature(float temp)


{
this->temperature = temp;
}

void GravityTDS::setAref(float value)


{
this->aref = value;
}

void GravityTDS::setAdcRange(float range)


{
this->adcRange = range;
}

void GravityTDS::setKvalueAddress(int address)


{
this->kValueAddress = address;
}

void GravityTDS::begin()
{
pinMode(this->pin,INPUT);
readKValues();
}

float GravityTDS::getKvalue()
{
return this->kValue;
}

void GravityTDS::update()
{
this->analogValue = analogRead(this->pin);
this->voltage = this->analogValue/this->adcRange*this->aref;
this->ecValue=(133.42*this->voltage*this->voltage*this->voltage -
255.86*this->voltage*this->voltage + 857.39*this->voltage)*this-
>kValue;
this->ecValue25 = this->ecValue / (1.0+0.02*(this->temperature-
25.0)); //temperature compensation
this->tdsValue = ecValue25 * TdsFactor;
if(cmdSerialDataAvailable() > 0)
{
ecCalibration(cmdParse()); // if received serial cmd from
the serial monitor, enter into the calibration mode
}
}

float GravityTDS::getTdsValue()
{
return tdsValue;
}

float GravityTDS::getEcValue()
{
return ecValue25;
}

void GravityTDS::readKValues()
{
EEPROM_read(this->kValueAddress, this->kValue);
if([Link](this->kValueAddress)==0xFF && [Link](this-
>kValueAddress+1)==0xFF && [Link](this->kValueAddress+2)==0xFF &&
[Link](this->kValueAddress+3)==0xFF)
{
this->kValue = 1.0; // default value: K = 1.0
EEPROM_write(this->kValueAddress, this->kValue);
}
}

boolean GravityTDS::cmdSerialDataAvailable()
{
char cmdReceivedChar;
static unsigned long cmdReceivedTimeOut = millis();
while ([Link]()>0)
{
if (millis() - cmdReceivedTimeOut > 500U)
{
cmdReceivedBufferIndex = 0;
memset(cmdReceivedBuffer,0,(ReceivedBufferLength+1));
}
cmdReceivedTimeOut = millis();
cmdReceivedChar = [Link]();
if (cmdReceivedChar == '\n' ||
cmdReceivedBufferIndex==ReceivedBufferLength){
cmdReceivedBufferIndex = 0;
strupr(cmdReceivedBuffer);
return true;
}else{
cmdReceivedBuffer[cmdReceivedBufferIndex] = cmdReceivedChar;
cmdReceivedBufferIndex++;
}
}
return false;
}

byte GravityTDS::cmdParse()
{
byte modeIndex = 0;
if(strstr(cmdReceivedBuffer, "ENTER") != NULL)
modeIndex = 1;
else if(strstr(cmdReceivedBuffer, "EXIT") != NULL)
modeIndex = 3;
else if(strstr(cmdReceivedBuffer, "CAL:") != NULL)
modeIndex = 2;
return modeIndex;
}

void GravityTDS::ecCalibration(byte mode)


{
char *cmdReceivedBufferPtr;
static boolean ecCalibrationFinish = 0;
static boolean enterCalibrationFlag = 0;
float KValueTemp,rawECsolution;
switch(mode)
{
case 0:
if(enterCalibrationFlag)
[Link](F("Command Error"));
break;

case 1:
enterCalibrationFlag = 1;
ecCalibrationFinish = 0;
[Link]();
[Link](F(">>>Enter Calibration Mode<<<"));
[Link](F(">>>Please put the probe into the standard
buffer solution<<<"));
[Link]();
break;

case 2:
cmdReceivedBufferPtr=strstr(cmdReceivedBuffer, "CAL:");
cmdReceivedBufferPtr+=strlen("CAL:");
rawECsolution = strtod(cmdReceivedBufferPtr,NULL)/(float)
(TdsFactor);
rawECsolution = rawECsolution*(1.0+0.02*(temperature-25.0));
if(enterCalibrationFlag)
{
// [Link]("rawECsolution:");
// [Link](rawECsolution);
// [Link](" ecvalue:");
// [Link](ecValue);
KValueTemp = rawECsolution/(133.42*voltage*voltage*voltage -
255.86*voltage*voltage + 857.39*voltage); //calibrate in the buffer
solution, such as 707ppm(1413us/cm)@25^c
if((rawECsolution>0) && (rawECsolution<2000) &&
(KValueTemp>0.25) && (KValueTemp<4.0))
{
[Link]();
[Link](F(">>>Confrim Successful,K:"));
[Link](KValueTemp);
[Link](F(", Send EXIT to Save and Exit<<<"));
kValue = KValueTemp;
ecCalibrationFinish = 1;
}
else{
[Link]();
[Link](F(">>>Confirm Failed,Try Again<<<"));
[Link]();
ecCalibrationFinish = 0;
}
}
break;

case 3:
if(enterCalibrationFlag)
{
[Link]();
if(ecCalibrationFinish)
{
EEPROM_write(kValueAddress, kValue);
[Link](F(">>>Calibration Successful,K Value
Saved"));
}
else [Link](F(">>>Calibration Failed"));
[Link](F(",Exit Calibration Mode<<<"));
[Link]();
ecCalibrationFinish = 0;
enterCalibrationFlag = 0;
}
break;
}
}
GravityTDS.h
/***************************************************
DFRobot Gravity: Analog TDS Sensor/Meter
<[Link]
_Meter_For_Arduino_SKU:_SEN0244>

***************************************************
This sample code shows how to read the tds value and calibrate it with
the standard buffer solution.
707ppm(1413us/cm)@25^c standard buffer solution is recommended.

Created 2018-1-3
By Jason <[Link]@[Link]@[Link]>

GNU Lesser General Public License.


See <[Link] for details.
All above must be included in any redistribution.
****************************************************/

#ifndef GRAVITY_TDS_H
#define GRAVITY_TDS_H

#include "Arduino.h"

#define ReceivedBufferLength 15
#define TdsFactor 0.5 // tds = ec / 2

class GravityTDS
{
public:
GravityTDS();
~GravityTDS();

void begin(); //initialization


void update(); //read and calculate
void setPin(int pin);
void setTemperature(float temp); //set the temperature and execute
temperature compensation
void setAref(float value); //reference voltage on ADC, default
5.0V on Arduino UNO
void setAdcRange(float range); //1024 for 10bit ADC;4096 for 12bit
ADC
void setKvalueAddress(int address); //set the EEPROM address to
store the k value,default address:0x08
float getKvalue();
float getTdsValue();
float getEcValue();
private:
int pin;
float aref; // default 5.0V on Arduino UNO
float adcRange;
float temperature;
int kValueAddress; //the address of the K value stored in the
EEPROM
char cmdReceivedBuffer[ReceivedBufferLength+1]; // store the
serial cmd from the serial monitor
byte cmdReceivedBufferIndex;

float kValue; // k value of the probe,you can calibrate in


buffer solution ,such as 706.5ppm(1413us/cm)@25^C
float analogValue;
float voltage;
float ecValue; //before temperature compensation
float ecValue25; //after temperature compensation
float tdsValue;

void readKValues();
boolean cmdSerialDataAvailable();
byte cmdParse();
void ecCalibration(byte mode);
};

#endif

Common questions

Powered by AI

The library optimizes the analog-to-digital conversion by setting adcRange and aref defaults specifically for different microcontrollers. These settings ensure accurate voltage representation from the ADC, which influences the precision of the ecValue calculations critical for accurate TDS readings .

The GravityTDS library handles temperature compensation by adjusting the electric conductivity value (ecValue) based on the difference of the solution's temperature from 25°C. It calculates ecValue25 by dividing ecValue by (1.0 + 0.02 * (temperature - 25.0)) to account for the temperature's impact on conductivity .

EEPROM is used to store the calibration constant 'kValue' for the GravityTDS sensor. When the sensor is calibrated, a new 'kValue' is calculated and saved to EEPROM so that the sensor maintains its calibration settings even after power cycles .

The GravityTDS sensor reads the analog voltage from the defined pin, converts this to a voltage level, and uses this to calculate the conductivity (ecValue). This raw conductivity value is adjusted for temperature to yield ecValue25. By applying the TdsFactor, the sensor then converts the adjusted conductivity to a TDS value (tdsValue), updating this measurement in real-time .

Memory is managed in the GravityTDS project by using specific EEPROM addresses to store the calibration constant 'kValue'. The system checks if the EEPROM location contains default values, and if so, initializes the 'kValue' to a default of 1.0. This approach ensures that calibration data isn't lost and is efficiently retrieved whenever required .

The instructions for entering and exiting calibration mode involve serial command inputs, requiring clear user inputs (such as "ENTER" and "EXIT"). Improvements could include providing feedback prompts for incorrect inputs, detailed step-by-step guidance during calibration, and offering default error recovery advice to enhance user experience and reduce errors during calibration .

The electrical conductivity is calculated using a cubic polynomial formula: ecValue = (133.42 * voltage^3 - 255.86 * voltage^2 + 857.39 * voltage) * kValue. This formula calculates the conductivity based on the measured voltage, which is then adjusted by a calibration constant 'kValue' to account for known differences in probe characteristics .

The 'kValue' is a calibration constant critical for accurate measurement in the GravityTDS sensor. It compensates for variations in probe characteristics and environmental conditions, thus directly impacting the precision of TDS readings. An incorrect 'kValue' would lead to significant deviations in the sensor output, highlighting its role in ensuring measurement fidelity .

Potential errors during calibration include not immersing the probe fully in the standard buffer solution, incorrect buffer solution use, and user errors in serial command input. Moreover, variations in solution temperature and improper kValue adjustments can lead to inaccurate calibration which would misrepresent the sensor's reading accuracy .

The GravityTDS library employs serial communication to receive commands from a user through the serial monitor. Commands such as "ENTER" are used to initiate calibration mode, and "EXIT" to exit. The calibration command "CAL:" allows the setting of a calibration value to adjust the probe's measurement accuracy. This mechanism ensures interactive calibration adjustments directly via the serial interface .

You might also like