This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

CCS/TM4C123GH6PM: DHT22 Temp and Humidity sensor: Read values = 0.0 C and 0.0%

Part Number: TM4C123GH6PM

Tool/software: Code Composer Studio

Hello,

I am trying to interface the DHT22 sensor to my Tiva board. I used the code posted by someone (for DHT11 sensor) in a similar thread and modified it a little to suit my application. The program debugs without error but when I flash it to the uC the readings I get are all zeros. I tried adjusting the timing according to the datasheet of the DHT22 but something still seems to be missing. Can someone please help.

I am attaching the main code along with the driver files. Connections:

Pin1 --> 3.3V

Pin2--> PF4

Pin3-->

Pin4--> GND

Regards

SwapnaDHT22_Sensor_Error.7z

  • Hi,

       I have never worked with DHT22. Doing some searches, I found these two posts with code in it that will help. We don't have any specific library for DHT22 and you will need to take a grain of salt when reading these code in the post. I will suggest you try to operate the processor at 16MHz as done in the post instead of 80Mhz. Perhaps that will make a difference for your timing to the DHT22.

    https://e2e.ti.com/support/microcontrollers/other/f/908/p/430790/1541482?tisearch=e2e-sitesearch&keymatch=dht22#1541482

    www.makerguides.com/.../

  • Hi Charles,

    Thank you for your response.

    I am using the same driver files that you pointed out in the other post. Infact, that is the same post I was referring to, in my post.

    I tried to run the code with 12.5MHz processor speed but to no avail. The Temp and humidity still reads 0.0.

    Swapna

  • Hi,

      I'm not familiar with the DHT22. As I mentioned we don't have any library supporting the DHT22 and our scope of expertise and support is limited to the MCU. If you have questions regarding the usage of the MCU then we will be able to support. If your question is how to use the DHT22 then we don't have that knowledge. 

      What is the expected protocol of communication? Can you show an expected timing diagram? I will suggest you use the scope to capture the timing diagram running your own code and  compare to the expected timing and protocol. That is the only way to debug. 

  • Hi Charles,

    Yeah, I understand, I have attached the datasheet of the DHT22 which has the detailed timing of the sensor (on page 6). Hope this helps.

    Thanks again!

    SwapnaDHT22_detailed_Datasheet.pdf

  • Hi Charles :)

    I have some good news! I tried and modified a new code I found @ https://www.instructables.com/Weather-Monitoring-System-Using-TIVA/

    Finally, I have some non-zero readings from the sensor. The humidity value seems accurate but the temperature value is off. I suppose I need to review the binary to decimal conversion in the code. But that is something I can figure out comfortably.

    Thank you very much for your help. Maybe when I have the code working accurately, I will share it here.

    Regards

    Swapna

  • Hello,

    Here is the code that works for interfacing the DHT22 sensor to the Tiva C Series. My connections were 

    Pin1 --> 3.3V

    Pin2--> PC4

    Pin3-->

    Pin4--> GND

    #include <stdint.h>
    #include <stdio.h>
    #include <stdbool.h>
    #include "inc/hw_types.h"
    #include "inc/hw_memmap.h"
    #include "driverlib/sysctl.h"
    #include "driverlib/gpio.h"
    #include "driverlib/pin_map.h"
    #include "utils/uartstdio.h"
    #include "drivers/ms_dht11.h"

    // Constants for Temperature and Humidity Sensor
    #define Clock_GPIO_C 0x04
    #define DIR_PIN4_In ~0x10
    #define DIR_PIN4_Out 0x10
    #define High_In 0x10
    #define Low_In ~0x10
    #define High_Out 0x10
    #define Low_Out 0x00
    #define DEN_ENABLE_C4 0x10

    void ConfigureUART(void);

    //-----for the display output on Putty-------//
    void ConfigureUART(void)
    {

    SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);

    GPIOPinConfigure(GPIO_PA0_U0RX);
    GPIOPinConfigure(GPIO_PA1_U0TX);
    GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);

    UARTStdioConfig(0, 115200, SysCtlClockGet());

    }

    int Binary2Decimal(int Bit_L, int Bit_H, unsigned int Binary[])
    {
    int temp=0,i;
    for(i = Bit_L;i<Bit_H;i++)
    temp += Binary[i] * (1<<((Bit_H-1)-i)); //1 << n is the same as raising 2 to the power n, or 2^n
    return temp;
    }

    /* This function generates the delay. */

    void Delay(unsigned long int period)
    {
    int i=0;
    for(i=0;i<12*(period/10);i++);
    }


    int main(void)

    {
    //---------------- Variables for Humidity and Temperature Sensor----------------------------------//


    ConfigureUART(); //Enabling UART display (putty)
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
    GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3);


    UARTprintf("Let's get started (Freq=%d)!\r\n", SysCtlClockGet());


    int i=0;
    unsigned int Data[40];
    int Temp_16, Temp_High, Temp_Low; // Varaibles for Temperature
    int RH_16, RH_High, RH_Low; // Variables for Relative Humidity
    int Check_Sum, temp, temp1, temp2, sum; // Variables for Parity Checking

    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOC);
    // RCGC_GPIO_R |= Clock_GPIO_C; // Clock for Port C
    Delay(100); // Delay to make Clock Smooth
    //GPIO_PORTC_DEN_R |= DEN_ENABLE_C4; // Set Pin C4 as Digital

    //------------------Collecting the data from the sensor------------------------------------------//
    while(1)
    {
    GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1, 0x02);// For taking and displaying values of Sensors again and gain (infinitely)
    while(1) // Iterates untill temperature and humidity values are true
    {
    GPIOPinTypeGPIOOutput(GPIO_PORTC_BASE, GPIO_PIN_4); // Set Pin C4 as output
    GPIOPinWrite(GPIO_PORTC_BASE,GPIO_PIN_4,Low_In);
    //GPIO_PORTC_DATA_R &= Low_In; // Set data line low for 1ms to awake the sensor
    Delay(1000);
    GPIOPinWrite(GPIO_PORTC_BASE,GPIO_PIN_4,High_In);
    //GPIO_PORTC_DATA_R |= High_In; // Set data line high for 30us
    Delay(30);
    GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1, 0x00);
    GPIOPinTypeGPIOInput(GPIO_PORTC_BASE, GPIO_PIN_4); // Set Pin C4 as Input
    while ((GPIOPinRead(GPIO_PORTC_BASE, GPIO_PIN_4)) == Low_Out) // Iterates untill data line is low
    {}
    while(GPIOPinRead(GPIO_PORTC_BASE, GPIO_PIN_4) == High_Out) // Iterates untill data line is high
    {}
    while(i<40)
    { // Iterates to get 40 bits data from the sensor
    while(GPIOPinRead(GPIO_PORTC_BASE, GPIO_PIN_4) == Low_Out) // Iterates untill the data line is low
    {}
    Delay(45);
    if(GPIOPinRead(GPIO_PORTC_BASE, GPIO_PIN_4) == Low_Out) // Check after 45us
    Data[i] = 0; // if data line is low // 0 bit by sensor
    else // else
    Data[i] = 1; // 1 bit by sensor
    while(GPIOPinRead(GPIO_PORTC_BASE, GPIO_PIN_4) == High_Out) // Iterates till the data line is high
    {}
    i++;
    }
    //------------------Processing the data from the sensor------------------------------------------//
    i=0;
    RH_High = Binary2Decimal(0,8,Data); // Converts first 8 bits (Humidity bits) of the data into Decimal value
    RH_Low = Binary2Decimal(8,16,Data); // Converts 8-16 bits of (Humidity bits) the data into Decimal value
    Temp_High = Binary2Decimal(17,24,Data); // Converts 17-24 bits of (Temperature bits) the data into Decimal value
    Temp_Low = Binary2Decimal(24,32,Data); // Converts 24-32 bits of (Temperature bits) the data into Decimal value
    Check_Sum = Binary2Decimal(32,40,Data); // Converts last 8 bits of (Parity bits) the data into Decimal value
    sum = RH_High + RH_Low + Temp_High + Temp_Low; // Sum the 8 high and 8 low bits of both humidity and temperature
    if(sum>255)
    sum -= 256; // To keep the sum 8 bit long
    if(Check_Sum == sum) // Parity checking
    break; // if no error, breaks the above while loop
    }
    RH_16 = Binary2Decimal(0,16,Data); // Converts Humidity bits into Decimal value
    temp1 = RH_16/10;
    Temp_16 = Binary2Decimal(17,32,Data); // Converts Temperature bits into Decimal value
    temp2 = Temp_16/10;


    //------------------Displaying the data from the sensor through UART and PuTTY------------------------------------------//

    UARTprintf(" H=%d.%d T=%d.%d \r\n", temp1,RH_16%10, temp2,Temp_16%10);
    // UARTprintf(" H=%d T=%d\r\n", temp1/10+48,temp2/10+48 );
    // Wait for two seconds
    Delay(2000000);
    }
    }

  • Thank you for sharing.