DHT11 Temperature Sensor Measurement Read

Hello Guys,

I Just started discovering embedded programming with Arduino possiblities with a little personal project. That’s pretty fun!

I have now a blocking issue with my code. I hope you guys can help me! Thank you very much in advance!!

A few details first… My board is composed of:

  • Arduino MEGA2560

  • LCD1602 display module

  • DS1307 RTC Module

  • DHT11 Temperature/Humidity Module

  • My Goal here is to Display Date, Time and Temperature on LCD Module

  • I first setted up the Time & Date only, and it was working great!

  • My current step now is to add DHT11 Temperature on LCD Screen

  • Nothing was displaying on Screen, so i simplified the code just to display temperature on Serial Monitor


The Temperature gathering part of my code is based on this sample code:

#include <dht_nonblocking.h>
#define DHT_SENSOR_TYPE DHT_TYPE_11

static const int DHT_SENSOR_PIN = 2;
DHT_nonblocking dht_sensor( DHT_SENSOR_PIN, DHT_SENSOR_TYPE );



/*
 * Initialize the serial port.
 */
void setup( )
{
  Serial.begin( 9600);
}



/*
 * Poll for a measurement, keeping the state machine alive.  Returns
 * true if a measurement is available.
 */
static bool measure_environment( float *temperature, float *humidity )
{
  static unsigned long measurement_timestamp = millis( );

  /* Measure once every four seconds. */
  if( millis( ) - measurement_timestamp > 3000ul )
  {
    if( dht_sensor.measure( temperature, humidity ) == true )
    {
      measurement_timestamp = millis( );
      return( true );
    }
  }

  return( false );
}



/*
 * Main program loop.
 */
void loop( )
{
  float temperature;
  float humidity;

  /* Measure temperature and humidity.  If the functions returns
     true, then a measurement is available. */
  if( measure_environment( &temperature, &humidity ) == true )
  {
    Serial.print( "T = " );
    Serial.print( temperature, 1 );
    Serial.print( " deg. C, H = " );
    Serial.print( humidity, 1 );
    Serial.println( "%" );
  }
}

This sample code is working great on my board. Here is the Serial Monitor Result:

Then i apply nearly the exact same lines to my own code, and run it on the same Board:

// Temperature Sensor Library & Globals

#include <dht_nonblocking.h>
#define DHT_SENSOR_TYPE DHT_TYPE_11

static const int DHT_SENSOR_PIN = 2;
DHT_nonblocking dht_sensor( DHT_SENSOR_PIN, DHT_SENSOR_TYPE );


// LCD Display Library
#include <LiquidCrystal.h>

// DS3231 RTC Clock time displayed on LCD Screen
#include <Wire.h>
#include <DS3231.h>

//Clock Variables
DS3231 clock;
RTCDateTime dt;

//LCD Interface definition
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

static bool measure_environment( float *temperature, float *humidity )
{
  static unsigned long measurement_timestamp = millis( );

  /* Measure once every four seconds. */
  if( millis( ) - measurement_timestamp > 3000ul )
  {
    if( dht_sensor.measure( temperature, humidity ) == true )
    {
      measurement_timestamp = millis( );
      return( true );
    }
  }

  return( false );
}

void setup() 
{
  // Serial Console Initialize
  Serial.begin(9600);

  //Define LCD Number of Columns & Rows
  lcd.begin(16, 2);
  
  // Initialize DS3231
  Serial.println("Initialize DS3231");
  clock.begin();

  // Clock Reference Time Setting
  //clock.setDateTime(__DATE__, __TIME__);

}

void loop()
{
  //get DateTime from RTC Clock
  dt = clock.getDateTime();
  char rtcTime[20];
  char rtcDate[20];
  sprintf(rtcTime, "%02d:%02d:%02d", dt.hour, dt.minute, dt.second);
  sprintf(rtcDate, "%02d/%02d/%02d", dt.day, dt.month, dt.year);

  //get Temperature and humidity
  float temperature;
  float humidity;
   if( measure_environment( &temperature, &humidity ) == true )
  {
    Serial.print( "T = " );
    Serial.print( temperature, 1 );
    Serial.print( " deg. C, H = " );
    Serial.print( humidity, 1 );
    Serial.println( "%" );
  }
  else
  {
    Serial.println("nope.");
  }

  
  //print variables on screen
  lcd.setCursor(0, 0);
  lcd.print(rtcTime);

  lcd.setCursor(12, 0);
  lcd.print(temperature);
  
  lcd.setCursor(0, 1);
  lcd.print(rtcDate);

  lcd.setCursor(12, 1);
  lcd.print(humidity);
  
  delay(1000);
}

When running this code, i never receive a measurement from Temp. Module. Here is Serial Monitor:

a few things i already tried:

  • Since i’m a total noob with electronic, i thought that maybe all those modules needed too much energy. I disconnected the LCD Screen, but it did not had any effet.

  • On Arduino IDE, user interface recognizes the line “DHT_nonblocking dht_sensor( DHT_SENSOR_PIN, DHT_SENSOR_TYPE );” and “DHT_nonblocking” is colored in Orange. But on The sample code, the same line is not colored (remains black). Since i’m on my first hours working with with Arduino IDE, i thought i could have some Library Linking issue. The thing is i don’t have any issue at compiling step, so i did not went further at this side… maybe i should…

Tank you very much in advance, and don’t hesitate to ask me if you need any other information!!

1 Like

I'll guess that you have a pin conflict. Try moving the DHT11 to a different one and adjust your code accordingly.

Hello Wildbill,

Thank you for your answer :slight_smile:

As you asked, i switched DHT11 Data Pin From PWM 2 to PWM 3 and ajusted line "static const int DHT_SENSOR_PIN = 2;" to "static const int DHT_SENSOR_PIN = 3;"

I still have the same behaviour... :frowning:

Also tried with PWM Pins 4 and 24, in case of...

As Additional information:

Here is my pin Assignment on the Arduino MEGA:

  • GND is shared between all modules
  • 5v is shared between all modules
  • PWM 2 is assigned to DHT11 Data Pin
  • PWM 7,8,9,10,11,12 are assigned to LCD1602 Module
  • SDA 20 and SCL 21 are assigned to SDA/SDL pins of DS1307 RTC Module

DHT11 Sample Code works well on same board, without any modification.

Thank you again

delay(1000);

I do not think that the non blocking state machine of the dht library plays well with the delay() in the code for the lcd display.

Confirm, by commenting out all the lcd display and delay and see if the serial monitor is reporting the temp/humidity correctly.

If so, then the solution is to place the lcd on a non blocking millis() timer using the "blink witout delay" method.

You may try a different library. I use the one of adafruit, it is easy to use and you won’t have any issues with delay() functions

Go to tools → Manage Libraries → type dht and install the adafruit library

Hello,

Thanks to both of you for your answer.

@Cattledog, you were right!

Temperature sensor won’t work if there is more than 10ms Delay.

Now everything is working well. I’ll have a look at the millis method to have a cleaner code.

Thank you very much.