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: Problem with DHT11

Part Number: TM4C123GH6PM

Tool/software: Code Composer Studio

Hello everybody,

I try to connect  temperature / humidity sensor DHT11 with TM4C123GH6PM. I had success - temperature and humidity were ok. Suddenly , when I try to read data again some problem occur. Please dont say look at connection because I checked many time. Problem occur because sensor give High or 1 all time after 0. Can someone who have DHT11 try my code. And 1 more thing, I use 10k or 5k pull up resistor on data line.

And one more question : Why code cant be builded when I use <math.h> ?

#include <stdbool.h>
#include <stdint.h>
#include <string.h>
//#include <math.h>
#include "hw_memmap.h"
#include "hw_types.h"
#include "hw_gpio.h"
#include "debug.h"
#include "rom.h"
#include "sysctl.h"
#include "gpio.h"
#include "rom_map.h"
#include "abi_prefix.h"
#include "crc_defines.h"
#include "rtos_bindings.h"
#include "fpu.h"
#include "adc.h"
#include "aes.h"
#include "arm_acle.h"
#include "assert.h"
#include "can.h"
#include "comp.h"
#include "complex.h"
#include "cpu.h"
#include "cpy_tbl.h"
#include "crc.h"
#include "ctype.h"
#include "des.h"
#include "etsi.h"
#include "interrupt.h"
#include "inttypes.h"
#include "lcd.h"
#include "iso646.h"
#include "limits.h"
#include "lowlev.h"
#include "locale.h"
#include "mpu.h"
#include "onewire.h"
#include "pprof.h"
#include "pin_map.h"
#include "stdio.h"
#include "stdnoreturn.h"
#include "stdlib.h"
#include "sysexc.h"
#include "signal.h"
#include "shamd5.h"
#include "tm4c123gh6pm.h"
#include "i2c.h"
#include "time.h"
#include "timer.h"
#include "pwm.h"

uint32_t variable[40];
unsigned long Humidity, HumidityDecimalPart, Temperature, TemperatureDecimalPart, Validation;

/************************ PA3 connect to data pin of DHT11 ************************************/
/************************ Put 10k pull up resistor between data line and Vcc ******************/
/************************ Temperature accuracy between +-2C and +-1C **************************/
/************************ Temperature measurement range 0 - 50C *******************************/
/************************ Humidity accuracy between +-4% and +-5% *****************************/
/************************ Humidity measurement range 20 - 90% *********************************/

unsigned long ConversionBinaryToDecimal(int startBit){
    int i = 0;
    unsigned long convertedNum = 0;
    for(i = startBit; i <= (startBit + 7); i++){
        convertedNum = convertedNum + variable[i] * pow(2, i);
    }
    return convertedNum;
}

void Interrupt_Function_GPIO_PORTF(void){
    GPIOIntClear(GPIO_PORTF_BASE, GPIO_INT_PIN_0);
    GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_3, ~GPIO_PIN_3); // low voltage on PA3
    SysCtlDelay(302000); // 18.12mS delay
    GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_3, GPIO_PIN_3); // high voltage on PA3
    SysCtlDelay(500); // 500 = 30uS
    GPIOPinTypeGPIOInput(GPIO_PORTA_BASE, GPIO_PIN_3); // input pins PF0 and PF4
    GPIOPadConfigSet(GPIO_PORTA_BASE, GPIO_PIN_3, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD_WPU);  //  pull up resistor , 2mA max
    while(GPIOPinRead(GPIO_PORTA_BASE, GPIO_PIN_3) == 0){};
    while(GPIOPinRead(GPIO_PORTA_BASE, GPIO_PIN_3) == GPIO_PIN_3){};
    int i;
    for(i = 0; i < 40; i++){
        while(GPIOPinRead(GPIO_PORTA_BASE, GPIO_PIN_3) == 0){};
        SysCtlDelay(450); // 45 uS = 750
        if(GPIOPinRead(GPIO_PORTA_BASE, GPIO_PIN_3) == GPIO_PIN_3){
            variable[i] = 1;
            while(GPIOPinRead(GPIO_PORTA_BASE, GPIO_PIN_3) == 8){};
        }else{
            variable[i] = 0;
        }

    }
    //SysCtlDelay(50000000); // 1sec delay
   /* Humidity = ConversionBinaryToDecimal(0);
    HumidityDecimalPart = ConversionBinaryToDecimal(8);
    Temperature = ConversionBinaryToDecimal(16);
    TemperatureDecimalPart = ConversionBinaryToDecimal(24);
    Validation = ConversionBinaryToDecimal(32);
    */
}

void main(void){
/**************************** CLOCK and ENABLE PERIPHERALS *********************************/
    SysCtlClockSet(SYSCTL_USE_PLL | SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ | SYSCTL_SYSDIV_4); // clock set 50MHz
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);    // enable portF
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);    // enable portA
/*******************************************************************************************/

/**************************** GPIO CONFIG **************************************************/
    GPIO_PORTF_LOCK_R = GPIO_LOCK_KEY;
    GPIO_PORTF_CR_R = 0x01;
    GPIO_PORTA_LOCK_R = GPIO_LOCK_KEY;
    GPIO_PORTA_CR_R = 0x08;

    GPIOPinTypeGPIOInput(GPIO_PORTF_BASE, GPIO_PIN_0); // input pins PF0
    GPIOPadConfigSet(GPIO_PORTF_BASE, GPIO_PIN_0, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD_WPU);  //  pull up resistor , 2mA max

    GPIOPinTypeGPIOOutput(GPIO_PORTA_BASE, GPIO_PIN_3);  // output pin PA3
    GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_3, GPIO_PIN_3); // high voltage on PA3
/*******************************************************************************************/

/**************************** GPIO INTERRUPT CONFIG ****************************************/
    IntMasterEnable();
    IntEnable(INT_GPIOF);
    IntPrioritySet(INT_GPIOF, 4);
    GPIOIntEnable(GPIO_PORTF_BASE, GPIO_INT_PIN_0);
    GPIOIntTypeSet(GPIO_PORTF_BASE, GPIO_PIN_0, GPIO_FALLING_EDGE);
    IntRegister(INT_GPIOF, Interrupt_Function_GPIO_PORTF);
/*******************************************************************************************/
    while(1){

    }
}

   

  • Hi,

      I don't have the DHT11. I think the first thing I will recommend is to capture the bus on the scope and measure if you are sending the start bit in the subsequent reads. When you said it suddenly doesn't work then you want to know if the MCU is sending the proper starting bit and the required duration. If you have the scope or logic analyzer it will make your debug much easier. 

  • Hello,

    Past few days I work on this problem and only what i catch is this. When program calibrating DHT11 , sequence is ok, except this part. In this sequence DHT11 send ~ 80uS low signal (this is ok) and then send ~ 80uS high signal (this is problem i think) but on oscilloscope I catch different signal. Can someone tell me why this drop from 3.3V to ~0.4V happen? Sensor sometimes work fine which means this drop is happen, but when dont work sensor give high signal all time. I try with 3.3 , 4.5, 5V power supply and noone of them didnt help to stability sensor to work ok all time.

      

  • Insufficient detail and scope caps to make a 'real' diagnosis.

    May we suggest:

    • has a 2nd DHT been acquired & tested - to prevent (wasted) time invested in diagnosing a 'faulty device.'
    • are both the ground & supply voltage 'solid' between MCU & DHT?   Especially so the common ground.
    • is power 'adequate' for both the MCU board & DHT?    Has voltage been measured 'under actual operation?'

    It is 'unclear' as to 'which condition' your scope cap is revealing.   You called it (we think) a 'different signal' - and it reveals as:

    • just ~15µS high
    • then drops to ~0.4V (not to ground) which may suggest 'Chip Damage' or power/connection issues
    • and later (around 44µS) drops all the way to ground - might this indicate (some) contention between MCU (switch-over) & DHT?   (or a too low a value pull-up resistor?)
    • how often does this (or similar) 'different (illegal waveform) signals' appear?   (Percent wise would be fine - and IS important.   Do certain 'sequenced commands' work 'better' than others?    If so - which ones?

    Milan S said:
    When program calibrating DHT11 , sequence is ok, except this part.

    May we see scope caps - showing the correct signal display?    Ideally (both) the 80µS low AND 80µS high intervals.   (You'll have to move your initial trigger point 'far to the left' to show both 'low & high' on the scope.)   The more detail you can provide - the higher the odds of your 'issue' being noted.   Recall - you ALONE are the forum's EYES!

    It REALLY is important that a 2nd sensor be acquired - and properly ESD Safeguarded at all times - to prevent (much) wasted time - caused by a failed or intermittent device.    (so unfair to your hapless helpers...)

  • I concur with cb1 on his diagnostic suggestions. It will be good if you have a second DHT11 to compare with. Did you remember if you changed your code resulting in the current not-working behavior? You mentioned it was working ok until it suddenly doesn't work anymore. Please check if you modify the code such that you are driving the bus while the sensor is returning the data and hence causing a drive conflict on the bus. If this is not the case, then can you reproduce the behavior in the second DHT11 sensor. If you cannot reproduce on the second sensor then it means your first sensor is somehow broken. 

  • That is my idea too , to put new DHT11 and check result. MCU and DHT11 have common ground, power supply and ground are solid attach with wires and protoboard. Voltage isnt fine when sensor work properly as you can see at that picture. Sensor give 3.3V ~15uS and then give ~0.4V which isnt ok, datasheet  say that sensor need to give 3.3v ~80uS. So once again, sensor sequence routine is to give 0V ~80uS and then 3.3V ~80uS but sensor give 3.3V ~15uS and then drop to ~0.4V which isnt correct. When this happen sensor give me fine results for temperature, humidity and validation bytes. Sensor work fine some period with sequence which I described ( 1. 80uS 0V  |   2. 15uS 3.3V   | 3. 25uS 0.4V ) but then go dump (sensor give  80uS 0V and then give 3.3V all time which isnt correct). Pull up resistor is ok, resistor value is 10k omhs(I try with 5k too). Code looks fine because I receive data in Interrupt routine and nothing can access to data line.    

    I will try with another DHT11 and I will write the results here. Some my friends think that resistance of output stages of DHT11 and MCU cause this problem.

  • Hi Milan,

      

    Milan S said:
    I will try with another DHT11 and I will write the results here

    Do you have any update with a new DHT11? Is your problem resolved?

  • Hi Milan,

      I have not heard back from you. I will assume your problem is somewhat resolved. I will close the thread for now. If you have some update you can reply back to this post to open the thread again. If you have new questions, please open a new thread. As a heads up, I will be out of office next week.