DHT22 Fahrenheit Reading Adds Third Digit over Time

Hello,

I have a small weather station I built and it is running an Arduino Uno, DHT22, DS18b, and a Light Diode resistor. The code is a public code that I downloaded and started to modify, and it does work fine. The issue that I am trying to track down is the temperature Fahrenheit will over time add a third digit.

I am curious if anyone else has seen the problem, or noticed something in the dht library that could cause it. The temperature in Celsius comes with a decimal point; but in the software once you convert your temperature to Fahrenheit by using dht.ReadTemperature(true) it is rounding off, and not being displayed with a decimal point. I let the weather station run for a few hours, and the reading should be 80* but it's showing 800* (I know it's supposed to be around the 80* because the Celsius reading is still correct).

I have one thing I want to try and see if it corrects the problem, but let me know if you have seen this same issue on your project. Below is the fix I will be trying tonight.

  float t = dht.readTemperature();
  //float f = dht.readTemperature(true); #Testing to see if this fixes the error over time of a 3rd digit.
  float f = (t * 9.0)/5.0 + 32.0;

How hot does it get during your testing?

You issue might be that it hit 100. If you're printing the result to an LCD, the final zero will still be left there when you print 80.

I guess since I have the DHT placed outdoors for the temp and humidity reading it is possible the temperature broke 100 at some point, if the sun hit the sensor or something. I don't think we have had any 100 degree days lately but definitely in the 90s.

I see your point though, I guess I will need to look into resetting the LCD if the temperature breaks 100 and then drops back below it.

Just print spaces on the LCD before you print the number

Don't use lcd.clear() in your loop() code to erase the old data. The clear function is slow and can cause flickering. Instead, print spaces to overwrite old data.

lcd.setCursor(0,0);
lcd.print("            "); // enough spaces to overwrite old data
lcd.setCursor(0,0);
lcd.print(temperature);

Another thing that you can do to reduce flickering is to only print to the LCD when the data changes.

That's a good plan, and shouldn't be that hard to implement. Thanks for the tips; I haven't messed with the LCD screens much until this project. Will implement this change tonight and see how it works.

I may plan to log some data to the SD card eventually, but we will see.

Can you post the rest of your code?

You can use dtostrf() to create a properly formatted, justified, and padded c-string from your float variable all in one function call. Then, just print that to the LCD.

#include <SPI.h>
#include <Adafruit_GFX.h>
#include <MCUFRIEND_kbv.h>
#include <DHT.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <math.h>

MCUFRIEND_kbv tft;

#define DHTPIN 10     // what digital pin we're connected to
#define ONE_WIRE_BUS 11

OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

int LDRpin = A5;
char lightString [4];
String str;

float ah; //absolute humidity = water vapor density in g/m*3
float rh; // relative humidity in %
float t; //temperature in celsius
float td; //dew point in celsius
float tf; //temperature in fahrenheit
float tdf; // dew point temperature in fahrenheit

int lightIntensity = 0;

// Uncomment whatever type you're using!
#define DHTTYPE DHT22   // DHT 11
//#define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321
//#define DHTTYPE DHT21   // DHT 21 (AM2301)

#define BLACK   0x0000
#define BLUE    0x001F
#define RED     0xF800
#define GREEN   0x07E0
#define CYAN    0x07FF
#define MAGENTA 0xF81F
#define YELLOW  0xFFE0
#define WHITE   0xFFFF

#define RED2RED 0
#define GREEN2GREEN 1
#define BLUE2BLUE 2
#define BLUE2RED 3
#define GREEN2RED 4
#define RED2GREEN 5

uint32_t runTime = -99999;       // time for next update
int reading1 = 0; // Value to be displayed
int d = 0; // Variable used for the sinewave test waveform
boolean alert = 0;
int8_t ramp = 1;
int tesmod = 0;

DHT dht(DHTPIN, DHTTYPE);

float tempC = 0;

char TempCelciusFahrenheit[6];

float tempF = 0;

double dewPoint(double celsius, double humidity) {
  double RATIO = 373.15 / (273.15 + celsius);
  double RHS = -7.90298 * (RATIO - 1);
  RHS += 5.02808 * log10(RATIO);
  RHS += -1.3816e-7 * (pow(10, (11.344 * (1 - 1 / RATIO ))) - 1) ;
  RHS += 8.1328e-3 * (pow(10, (-3.49149 * (RATIO - 1))) - 1) ;
  RHS += log10(1013.246);
  double VP = pow(10, RHS - 3) * humidity;
  double T = log(VP / 0.61078); // temp var
  return (241.88 * T) / (17.558 - T);
}

void setup()
{
  Serial.begin(9600);
  Serial.println("DHTxx test!");
  tft.reset(); //Reset LCD to begin
  uint16_t id = tft.readID();
  tft.begin(id);

  tft.setRotation(1); // Set Rotation at 0 degress (default)

  tft.fillScreen(BLACK); //Set Background Color with BLACK
  tft.setCursor (7, 20);
  tft.setTextSize (2);
  tft.setTextColor (GREEN, BLACK);
  tft.print ("Temperature ");

  tft.setCursor (7, 50);
  tft.setTextSize (2);
  tft.setTextColor (GREEN, BLACK);
  tft.print ("Fahrenheit ");

  tft.setCursor (7, 85);
  tft.setTextSize (2);
  tft.setTextColor (GREEN, BLACK);
  tft.print ("Humidity");

  tft.setCursor (7, 120);
  tft.setTextSize (2);
  tft.setTextColor (GREEN, BLACK);
  tft.print ("Heat Index F");

  tft.setCursor (7, 155);
  tft.setTextSize (2);
  tft.setTextColor (GREEN, BLACK);
  tft.print ("Heat Index C");

  tft.setCursor (7, 220);
  tft.setTextSize (2);
  tft.setTextColor (GREEN, BLACK);
  tft.print ("Dew Point");

  tft.setCursor (7, 187);
  tft.setTextSize (2);
  tft.setTextColor (GREEN, BLACK);
  tft.print ("Light");

  tft.setCursor (150, 187);
  tft.setTextSize (2);
  tft.setTextColor (GREEN, BLACK);
  tft.print ("Ds temp");

  tft.setCursor (250, 20);
  tft.setTextSize (2);
  tft.setTextColor (GREEN, BLACK);
  tft.print ("AH:");



  tft.fillRect(0, 238, 320, 2, BLUE);
  tft.fillRect(0, 210, 320, 2, BLUE);
  tft.fillRect(0, 178, 320, 2, BLUE);
  tft.fillRect(0, 145, 320, 2, BLUE);
  tft.fillRect(0, 110, 320, 2, BLUE);
  tft.fillRect(0, 75, 320, 2, BLUE);
  tft.fillRect(0, 41, 320, 2, BLUE);
  tft.fillRect(0, 10, 320, 2, BLUE);


  dht.begin();
}

void loop() {


  float h = dht.readHumidity();
  float t = dht.readTemperature();
  //float f = dht.readTemperature(true); #Testing to see if this fixes the error over time of a 3rd digit.
  float f = (t * 9.0)/5.0 + 32.0;
  float hif = dht.computeHeatIndex(f, h);
  float hic = dht.computeHeatIndex(t, h, false);

  rh = dht.readHumidity();
  t = dht.readTemperature();

  lightIntensity = analogRead(LDRpin);

  lightIntensity = map(lightIntensity, 1025, 20, 0, 98);

  str = String(lightIntensity) + "%";
  str.toCharArray(lightString, 5);

  float tempC = 0;

  float tempF = 0;

  sensors.requestTemperatures();

  tempC = sensors.getTempCByIndex(0);

  tempF = sensors.toFahrenheit(tempC);

  ah = (6.112 * pow(2.71828, ((17.67 * t) / (243.5 + t))) * rh * 2.1674) / (273.15 + t);

  if (isnan(h) || isnan(t) || isnan(f)) {
    Serial.println("Failed to read DHT sensor!");
    return;
  }

  tft.setCursor(245, 187);  // Temperature from DS18B20
  tft.setTextSize (2);
  tft.setTextColor ( RED , BLACK);
  tft.print (tempC);
  tesmod = 1;

  tft.setCursor(185, 50);
  tft.setTextSize (2);
  tft.setTextColor ( YELLOW , BLACK);
  tft.print (ah);

  tft.setCursor(233, 50);
  tft.setTextSize (2);
  tft.setTextColor ( YELLOW , BLACK);
  tft.print (" ");

  tft.setCursor(250, 50);
  tft.setTextSize (2);
  tft.setTextColor ( YELLOW , BLACK);
  tft.print ("g/m3");

  tft.setCursor (160, 120); // Heat index in Fahrenheit
  tft.setTextSize (2);
  tft.setTextColor (BLUE, BLACK);
  tft.print (hif, 1);
  tesmod = 1;

  tft.setCursor (160, 155); // Heat index in celsius
  tft.setTextSize (2);
  tft.setTextColor (MAGENTA, BLACK);
  tft.print (hic, 0);
  tesmod = 1;

  tft.setCursor (150, 220); // Print the dew point
  tft.setTextSize (2);
  tft.setTextColor (GREEN, BLACK);
  tft.print (dewPoint(t, h ));
  tesmod = 1;

  tft.setCursor (198, 220);
  tft.setTextSize (2);
  tft.setTextColor (GREEN, BLACK);
  tft.print ("C");


  tft.setCursor (140, 50); // Heat index in fahrenheit added space before f, 0
  tft.setTextSize (2);
  tft.setTextColor (BLUE, BLACK);
  tft.print (f, 0);

  tft.setCursor (120, 85); // Print the Humidity
  tft.setTextSize (2);
  tft.setTextColor (YELLOW, BLACK);
  tft.print (h, 1); tft.print ('%');
  tesmod = 1;

  tft.setCursor (90, 187); // Print the light intensity in percentage
  tft.setTextSize(2);
  tft.setTextColor( CYAN, BLACK);
  tft.print(lightString);
  tesmod = 1;

  tft.setCursor (150, 20); // Print temperature from DHT sensor
  tft.setTextSize (2);
  tft.setTextColor (MAGENTA, BLACK);
  tft.print (t, 1); tft.print (' C');
  tesmod = 1;

  if (millis() - runTime >= 1000) { // Execute every 500ms
    runTime = millis();

  if (f >= 100) {   //Writing blank space to see if it fixes the 100* problem
      tft.setCursor (140, 50); // Heat index in fahrenheit added space before f, 0
  tft.setTextSize (2);
  tft.setTextColor (BLUE, BLACK);
  tft.print ("       ");
  }


  }
  Serial.print("Temperature: ");
  Serial.print(t);
  Serial.println(" *C ");
  Serial.print("Fahrenheight: ");
  Serial.print(f);
  Serial.println(" *F\t");
  Serial.print("Humidity: ");
  Serial.print(h);
  Serial.println(" %\t");
  Serial.print("Light: ");
  Serial.print(lightString);
  Serial.println(" % ");
  Serial.print("DS Temp: ");
  Serial.print(tempC);
  Serial.println(" *C ");
  Serial.print("Heat index F: ");
  Serial.print(hif);
  Serial.println(" *F ");
  Serial.print("Heat index C: ");
  Serial.print(hic);
  Serial.println(" *C ");
  Serial.print("Dew Point: ");
  Serial.print(dewPoint(t, h ));
  Serial.println(" Celsius ");
  Serial.println();

  delay(3000);
}