DHT11 With Mbed os 6.0+

Hey all

I have tried making my own code to output a temperature and humidity value from the Dht11 sensor as I could not get the communities Dht11 classes to work with my board.

It would seem I get the same output with all the different classes as I do this one.

I am using the Nucleo -G071RB development board.
Code is :
#include “mbed.h”

DigitalInOut data_line(ARDUINO_UNO_D3);

unsigned char data[5] = {0,0,0,0,0};
int byteindex = 0;

int Humidity, Temperature;

bool validateChecksum() {
return data[4] == ((data[0] + data[1] + data[2] + data[3]) & 0xFF);
}

void readDHT11() {
// Initialize the communication process
data_line.output();
data_line = 0;
wait_us(18000);
data_line = 1;
wait_us(40);

    // Switch to input mode to receive data
    data_line.input();
    if (data_line.read() == 0){
        wait_us(80);

        if(data_line.read() == 1){
            wait_us(80);

            // Read 40 bits of data
            for (int byteindex = 0; byteindex <5; byteindex++){

                for (int i = 0; i < 8; i++){
                    while (data_line.read() == 0){
                        wait_us(50);
                        if (data_line.read() == 0){
                            wait_us(30);
                            if(data_line.read() == 1){
                                data[byteindex] |= (1 << (7 - (i % 8)));

                                if((i % 8) == 7){
                                    byteindex++;
                                }
                            }
                        }
                    }
                }
            }
        }
    }

}

int main() {

data_line = 1;



while(1) {
    readDHT11();

    printf("Raw Data: \n");
    for(int i = 0; i< 5; i++){
        printf("%02X \n", data[i]);
    }

    if (validateChecksum()) {
        int humidity_integral = data[0];
        int humidity_decimal = data[1];
        Humidity = humidity_integral + (humidity_decimal / 10.0);

        int temperature_integral = data[2];
        int temperature_decimal = data[3];
        Temperature = temperature_integral + (temperature_decimal / 10.0);

        printf("Humidity: %d\n", Humidity);
        printf("Temperature: %d\n", Temperature);
    } else {
        printf("Checksum validation failed. Data may be corrupted.\n");
    }
    wait_us(10000000);
}

} ’

The output I get is :
Raw Data:
00
00
00
00
00
Humidity: 0
Temperature: 0

Thanks All!

Hello,

I am sure both libraries below are OK (with MbedOS 6+) and both contain example code in .h file.

DHT11 - Fork of https://os.mbed.com/users/s_inoue_mbed/co… | Mbed
DHT - Fork of DHT component - https://os.mbed.com/teams… | Mbed

BR, Jan

Hey,

I have tried both of the librarys you suggested,

With the first, using example code, I get an error output.
With the second, I get Error:5 output.

I am confident the sensor is working and wired correctly, as I have an arduino library that works fine with an arduino uno board and sensor.

Thanks for your speedy response.

Jon

I do not have any DHT11 and any STM32GXX board so I can not test it right now.

Arduino is 5V logic STM32 is 3v3 logic so power supply and value of pull up resistor could have also an inmpact.

If you like Arduino then in Arduino eco system also exist stm32duino/Arduino_Core_STM32: STM32 core support for Arduino (github.com)

BR, Jan

Hello @Jonathon_Dunderdale the below project of mine might help you. It was from a few years ago and I am engaged in other projects at the moment so do not have time to tell you or investigate what may have changed, if any at all. Still, here it is, and it may work out of the box.
Enjoy. And if it does work for you, let me know via nuertey_odzeyem[at]hotmail[dot]com
Nuertey