DFRobot Gravity
DFRobot Gravity
<https://www.dfrobot.com/wiki/index.php/Gravity:_Analog_TDS_Sensor_/
_Meter_For_Arduino_SKU:_SEN0244>
Created 2017-8-22
By Jason <[email protected]@dfrobot.com>
1. This code is tested on Arduino Uno and Leonardo with Arduino IDE 1.0.5 r2 and 1.8.2.
****************************************************/
#include <OneWire.h>
#include <DallasTemperature.h>
#include <SD.h>
#include <SPI.h>
File myFile;
#define ONE_WIRE_BUS 7
#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];
float voltage,phValue;
float Celcius=0;
float Fahrenheit=0;
DFRobot_PH ph;
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
void setup()
Serial.begin(115200);
sensors.begin();
pinMode(TdsSensorPin,INPUT);
// SD Card Initialization
if (SD.begin())
{
Serial.println("SD card is ready to use.");
} else
return;
void loop()
volt += ((float)analogRead(sensorPin)/1023)*5;
volt = volt/800;
volt = round_to_dp(volt,1);
ntu = 1540;
}else{
ntu = -576.12*square(volt)+3393.2*volt-3443.2;
// ntu = -1120.4*square(volt)+5742.3*volt-4353.8;
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;
printTimepoint = millis();
for(copyIndex=0;copyIndex<SCOUNT;copyIndex++)
analogBufferTemp[copyIndex]= analogBuffer[copyIndex];
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(" C ");
Serial.print(Celcius);
Serial.print(" F ");
Serial.println(Fahrenheit);
Serial.print(volt );
Serial.print(" V ");
Serial.print(ntu );
Serial.print("pH:");
Serial.print(phValue);
Serial.print(tdsValue,0);
Serial.println("ppm");
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(",");
else {
} delay (300000); // use 500 for short term troubleshooting/lab testing,300000 for field testing
return in_value;
int bTab[iFilterLen];
bTab[i] = bArray[i];
int i, j, bTemp;
bTemp = bTab[i];
bTab[i + 1] = bTemp;
else
return bTemp;