DHT sensor and 16x2 LCD Screen; How to show 1 decimal place

Hi,

this is all new, so apologies for such a simple answer. I've been running through the inbuilt projects, just trying to get familiar with it all.

I'm using a project from online, which I'm trying to modify. Again, just to get used to the language etc

For some reason I can not figure out how to get the LCD display to show one decimal place, so then I can adjust the limits for the LEDs. For example, how do I get it do display 21.9?

Do I have to insert an int function?

Thank you

P.S.
The code is attached, rather than pasting

P.P.S.
I’ve just read how to insert the code properly but I’m on the mobile now and currently unable to edit the post. Thank you.

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

DHT dht(DHTPIN, DHTTYPE);

void setup()
{
  Serial.begin(9600);
  for (int DigitalPin = 7; DigitalPin <= 9; DigitalPin++) 
 {
  pinMode(DigitalPin, OUTPUT);
 }
  lcd.begin(16,2); //16 by 2 character display
  
dht.begin();
}
 
void loop()
{
  delay(1000);
  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  float t = dht.readTemperature();


  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Temp: ");   
  //lcd.print(t);
  lcd.print(t,1);
  lcd.print("'C");
  
  lcd.setCursor(0,1);
  lcd.print("Humid: ");
  //lcd.print(h);
  lcd.print(h,1);
  lcd.print("%");
  
  if (t<=21)
  {
  digitalWrite(7, HIGH);
  digitalWrite(8, LOW);
  digitalWrite(9, LOW);
  }
  else if (t>=21)
  {
  digitalWrite(8, HIGH);
  digitalWrite(7, LOW);
  digitalWrite(9, LOW);
  }
  else if (t>=24)
  {
  digitalWrite(9, HIGH);
  digitalWrite(7, LOW);
  digitalWrite(8, LOW);
  } 
  
}

temp and humidity sensor.txt (1.27 KB)

I tested this:

  float ab = 12.34;
  Serial.println(ab,1);

It prints out as 12.3

ab = 12.36 prints out as 12.4

I want to enter the project. What to do

Railroader:
I tested this:

  float ab = 12.34;

Serial.println(ab,1);




It prints out as 12.3

ab = 12.36 prints out as 12.4

Thanks for that.

SO I can just copy the code which you included? I'm assuming not as it says 12.34. I've not used the 'ab' code before.

Pasiocave:
I want to enter the project. What to do

I used this tutorial, which I found very useful;

Ah I just thought. I have gotten the display to show one decimal place, but it always reads zero. I can not figure out how to get the sensor to display a figure for the decimal place.

Post your code. Read the how to use this forum-please read sticky to see how to post code.

groundFungus:
Post your code. Read the how to use this forum-please read sticky to see how to post code.

I attached as a text document, as I’m on my mobile and it won’t let me.
I’ll amend the original post when I’m back on the PC. Thanks

Although t and h are declared as float the values returned by the read functions are integers for the DHT11.
Examine the library files.

In DHT.h:

uint8_t data[5];

Is used in the DHT.cpp file in the read function to get the temperature and humidity. The data array is an array of uint8_t or unsigned integers.

Then in the read temperature function:

float DHT::readTemperature(bool S, bool force) {
  float f = NAN;

  if (read(force)) {
    switch (_type) {
    case DHT11:
      f = data[2];
//more code
  return f;

So, you see that the value returned by the readTemperature function is an uint8_t put into a float and has no numbers after the decimal. The same goes for the readHumidity function.

The values returned from the readTemperature and readHumidity functions for DHT21 and DHT22 are floats and can have numbers after the decimal.

groundFungus:
Although t and h are declared as float the values returned by the read functions are integers for the DHT11.
Examine the library files.

In DHT.h:

uint8_t data[5];

Is used in the DHT.cpp file in the read function to get the temperature and humidity. The data array is an array of uint8_t or unsigned integers.

Then in the read temperature function:

float DHT::readTemperature(bool S, bool force) {

float f = NAN;

if (read(force)) {
    switch (_type) {
    case DHT11:
      f = data[2];
//more code
  return f;



So, you see that the value returned by the readTemperature function is an uint8_t put into a float and has no numbers after the decimal. The same goes for the readHumidity function.

The values returned from the readTemperature and readHumidity functions for DHT21 and DHT22 are floats and can have numbers after the decimal.

Right, thank you. I’m just reading into what that all means. Google is my friend.

So, I’m currently displaying the integer vale and need to write DHT21 and DHT22 into my code? Can I copy what you’ve written then into the code?

need to write DHT21 and DHT22 into my code?

No, you would have to buy and use a DHT21 or DHT22 device instead of DHT11 and change the code to reflect the device used (see below). They are not interchangeable.

Can I copy what you've written then into the code?

That is not code that I wrote. It is excerpts from the DHT library to show why the library will not provide data with decimals for the DHT11.

If you want data to one decimal place you need to use a DHT21 or DHT22 and change the #declare at the top of the code from:

#define DHTTYPE DHT11

To:

#define DHTTYPE DHT22

Or:

#define DHTTYPE DHT21

groundFungus:
No, you would have to buy and use a DHT21 or DHT22 device instead of DHT11 and change the code to reflect the device used (see below). They are not interchangeable.
That is not code that I wrote. It is excerpts from the DHT library to show why the library will not provide data with decimals for the DHT11.

If you want data to one decimal place you need to use a DHT21 or DHT22 and change the #declare at the top of the code from:

#define DHTTYPE DHT11

To:

#define DHTTYPE DHT22

Or:

#define DHTTYPE DHT21

I just realised that before I read your comments, that they’re different sensors.

Okay, that makes sense. I tried a variety of things and just could not get the decimal place to read anything other than zero. I make have done the correct code, but probably not.

I’ll get another sensor ordered to have a play. Thanks for your reply and answer.