Código

#include <OneWire.h> 

#include <DallasTemperature.h> 

 

const int SENSOR_PIN = 2; // Arduino pin connected to DS18B20 sensor’s DQ pin 

 

OneWire oneWire(SENSOR_PIN);         // setup a oneWire instance 

DallasTemperature tempSensor(&oneWire); // pass oneWire to DallasTemperature library 

 

float tempCelsius;    // temperature in Celsius 

float tempFahrenheit; // temperature in Fahrenheit 

 

int buf[10]; // Array to store sensor readings 

int temp;    // Temporary variable 

unsigned long int avgValue;  // Store the average value of the sensor feedback 

 

void setup() 

{ 

  Serial.begin(9600); // initialize serial 

  tempSensor.begin();    // initialize the sensor 

} 

 

void loop() 

{ 

  tempSensor.requestTemperatures();             // send the command to get temperatures 

  tempCelsius = tempSensor.getTempCByIndex(0);  // read temperature in Celsius 

  tempFahrenheit = tempCelsius * 9 / 5 + 32; // convert Celsius to Fahrenheit 

 

  Serial.print(“Temperature: “); 

  Serial.print(tempCelsius);    // print the temperature in Celsius 

  Serial.print(“°C”); 

  Serial.print(”  ~  “);        // separator between Celsius and Fahrenheit 

  Serial.print(tempFahrenheit); // print the temperature in Fahrenheit 

  Serial.println(“°F”); 

 

  delay(500); 

 

  for(int i=0;i<10;i++)       // Get 10 sample value from the sensor for smooth the value 

  {  

    buf[i]=analogRead(SENSOR_PIN); 

    delay(10); 

  } 

  for(int i=0;i<9;i++)        // Sort the analog from small to large 

  { 

    for(int j=i+1;j<10;j++) 

    { 

      if(buf[i]>buf[j]) 

      { 

        temp=buf[i]; 

        buf[i]=buf[j]; 

        buf[j]=temp; 

      } 

    } 

  } 

  avgValue=0; 

  for(int i=2;i<8;i++)                      // Take the average value of 6 center samples 

    avgValue+=buf[i]; 

  float phValue=(float)avgValue*5.0/1024/6; // Convert the analog into millivolt 

  phValue=3.5*phValue;                      // Convert the millivolt into pH value 

 

  Serial.print(“pH: “); 

  Serial.print(phValue,2); 

  Serial.println(“”); 

 

  delay(1000); 

 

  int sensorValue = analogRead(SENSOR_PIN); 

  float Voltage = sensorValue * 5.0 / 1024.0; // Convert analog reading to Voltage 

  float tdsValue = (133.42 / Voltage / Voltage / Voltage – 255.86 / Voltage / Voltage + 857.39 / Voltage) * 0.5; // Convert voltage value to TDS value 

 

  Serial.print(“TDS Value: “);  

  Serial.print(tdsValue); 

  Serial.println(” ppm”); 

 

  delay(1000); 

}