Print values of AM2302 H/T sensor on 4x20 LCD

HI everyone!! I wish I get help for display problem on my LCD 4x20. I wrote the program to read Temp. and Humid. floats of my AM2302 sensor made by Asair. Then I want to display the values on the LCD.
I get my title '' Parameters'' on the LCD. I get the print of ''Humidity: '' on my LCD. I get the print of ''Temperature: '' on my LCD.
But the values don't print where supposed exept for ''Humidity: '', I get only one digit (7or 6 or 8...) after Humidity:

For the Serial Monitor, I get the Humidity and temperature printed perfectly. Only on LCD that I get erratic printing of the floats. Any Ideas? Here is the program.

Using Genuine Arduino Uno


#include <DHT_Async.h>
#include "DHT.h"
#include "LCDIC2.h" //POur imprimer sur mon LCD

#define DHTPIN 7     
#define DHTTYPE DHT22   

DHT dht(DHTPIN, DHTTYPE);
LCDIC2 lcd(0x27, 20, 4); // Initialiser l'ecran LCD

void setup() {
  Serial.begin(9600);
  lcd.begin();
  Serial.println(F("DHT22+LCD test!"));
  lcd.setCursor(5,0);
  lcd.print("Parametres");

  dht.begin();
}

void loop() {
 
  delay(2000);
  float h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  float t = dht.readTemperature();
  // Read temperature as Fahrenheit (isFahrenheit = true)
  float f = dht.readTemperature(true);

  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t)|| isnan(f) ) {
    Serial.println(F("Failed to read from DHT sensor!"));
    lcd.print("Failed to read from DHT sensor!");
    return;
  }

  Serial.print(F("Humidity: "));
  Serial.print(h);
  Serial.print(F("%  Temperature: "));
  Serial.print( t);
  Serial.print(F("°C "));
  //Serial.print(f);
  //Serial.print(F("°F  Heat index: "));
  //Serial.print(hic);
  //Serial.print(F("°C "));
  //Serial.print(hif);
  //Serial.println(F("°F"));

  //MA PARTIE LCD
  lcd.setCursor(0,2);
  lcd.print(F("Humidity: "));
  lcd.setCursor(9,2);
  lcd.print(h);
  lcd.setCursor(15,2);
  lcd.print("%");
  lcd.setCursor(0,3);
  lcd.print(F("Temperature: "));
  lcd.setCursor(12,3);
  lcd.print(t);
  lcd.setCursor(17,3);
  lcd.print(F("C "));

}


1 Like

The LCDIC2 library does not support printing floats.

It also does not support printing ints. Or character arrays. Or anything that isn't a character or a String.

Does yourself a big favour and forget you ever heard of it. Use something like LiquidCrystal_I2C that's based on the Print class and properly supports the print method. Anything but LCDIC2.

1 Like

Wow, thanks for the hint! I wil try with LiquidCrystal_IC2. The best safe source to find it ?

1 Like

I2C, not IC2. Look in the library manager.

1 Like

Already found it, up and running. Many thanks!

1 Like

you are using the F-Makro for Serial.println already.
As you use a more common LCD library now, you can use the F-Makro for the lcd.print also.

1 Like

okay thanks

1 Like