Code attached works with serial monitor not with LCD

this is the AHT20 libraries example code i modified to print to a 16,2 i2c lcd, it prints to the serial monitor no problem but on the lcd it prints the first number replaces the second with a period then prints the rest as normal ex. temp = 24.56C / humidity = 45.87% lcd prints "2.56C 4.87%" couldnt find much online

#include <Wire.h>
#include <AHT20.h>
#include <LiquidCrystal_I2C.h>

AHT20 aht20;
LiquidCrystal_I2C lcd (0x27,16,2);

void setup()
{
  Serial.begin(115200);
  Serial.println("Humidity AHT20 examples");

  Wire.begin(); //Join I2C bus
  //Check if the AHT20 will acknowledge
  if (aht20.begin() == false)
  {
    Serial.println("AHT20 not detected. Please check wiring. Freezing.");
    while (1);
  }
  Serial.println("AHT20 acknowledged.");
  lcd.begin();
  lcd.backlight();
  lcd.setBacklight (HIGH);
}

void loop()
{
  //If a new measurement is available
  if (aht20.available() == true)
  {
    //Get the new temperature and humidity value
    float temp = aht20.getTemperature();  // temp = temerature0
    float hum = aht20.getHumidity();     // hum = humidity

    //Print the results
    Serial.print("Temperature: ");
    Serial.print(temp, 2);
    Serial.print(" C\t");
    Serial.print("Humidity: ");
    Serial.print(hum, 2);
    Serial.print("% RH");

    Serial.println();

    lcd.setCursor (0,0);
    
    lcd.print (temp);
    lcd.print("C");
   // humidity beside temp
   
    lcd.setCursor (8,0);
    lcd.print (hum);
    //lcd.setCursor (11,0);
    lcd.print ("%");
  
    delay (1000);
  }

  //The AHT20 can respond with a reading every ~50ms. However, increased read time can cause the IC to heat around 1.0C above ambient.
  //The datasheet recommends reading every 2 seconds.
  delay(2000);
}
  • When printing to an LCD, I follow this format:
    example only

  lcd.setCursor(0, 0);

  //                   111111
  //         0123456789012345
  lcd.print("Temperatura     "); //show temperature on LCD

  lcd.setCursor(12, 0);

  //                   111111
  //         0123456789012345
  //         Temperatura xx'C
  lcd.print(temp);  //append the actual value
  lcd.print((char)223);
  lcd.print("C");

what is ((char)223) ?

i dont get it serial monitor and lcd should print the same thing (temp = aht20.getTemperature

I'll try it out but would that effect how the LCD displays the numbers themselves, or is it just a label?

lcd.print((char)223);
  • It prints the degree symbol as in 25°C.

Oh ya, that'd be nice, thanks

I only want "25.75°C 63.57%" on the LCD, the labels "temperature and humidity" aren't needed on the lcd.

The difference: 2.56 vs 25.7

Using the number above as an example the LCD will take the ones place and turn it into a period, changing the temp from 25.75°C to 2.75°C same with the humidity, the serial monitor, however, will print 25.75. Im just confused because they should print the same data using the same print (temp); and (hum); which is the data from the sensor

Is it your formatting ?
Are the vals in parentheses (col, row) or (row, col) ?
Print the temperature and nothing else.

As far as setCursor it's (col, row) the two values are beside each other on the LCD with a couple blank spaces in-between, is that what you mean?

Yes, that's what I mean.
What goes if you modify (and Upload) the sketch where it only prints the temperature value (and nothing else)?

Ill give it a try, but I won't be able to untill around 9pm est, I will get back to you when I do though

I'm recalling how, another chap, not forum, the float had to be converted to a string --
dtostrf
dtostrf() - turn your floats into strings - Programming Electronics Academy

i tried to print just the temp, same issue. i tried using the code from post #3, now im going to try the dtostrf you suggested

dtostrf(temp, 6, 2,
this is what i have so far, not sure what to put for "where_to_put_string"

That would be the name of the additional char array that you have to declare --
e.g. call it temperaturestring
char temperaturestring [10];

alright so do i lcd.print (tempstring); then? and whats the 10, number of digits?

dtostrf (temp, 3, 1, temperaturestring);