This is my first project and I am trying to read the temperature readings from a DHT22 sensor using a STM32F401RE-NUCLEO board.

My code is failing at reading the input from the DHT22 sensor when I use the CCR1 command. I have verified my circuit is working as I previously tested it using the MBED compiler.

I set up the timer to use TIM_CHANNEL_1 and TIM3 instance, which according to the STM32CubeMX GUI corresponds to STM32 pin PA6.

My DHT22 structure looks like the following:

typedef struct{
    uint8_t             bitsRX[5];
    float               temp;
    float               hum;
    uint8_t             crcErrorFlag;
    DHT22_STATE         state;
    TIM_HandleTypeDef   timHandle;
    TIM_IC_InitTypeDef  timICHandle;
    uint32_t            timChannel;
    uint16_t            gpioPin;
    GPIO_TypeDef*       gpioPort;
    int                 bitPos;
    IRQn_Type           timerIRQn;
    uint32_t            gpioAlternateFunction;
    uint16_t            lastVal;
    void                (*errorCallback)(DHT22_RESULT);

I set up my DHT object as the following:

dht.gpioPin = GPIO_PIN_6;
dht.gpioPort = GPIOA;
dht.timChannel = TIM_CHANNEL_1;
dht.gpioAlternateFunction = GPIO_AF2_TIM3;
dht.timHandle.Instance = TIM3;

I am running the following command to try and capture the value:

uint16_t val = HAL_TIM_ReadCapturedValue(&dht->timHandle, dht->timChannel);

Where the function is defined as the following (snipped off the later parts since I only care about channel 1):

uint32_t HAL_TIM_ReadCapturedValue(TIM_HandleTypeDef *htim, uint32_t Channel)
uint32_t tmpreg = 0U;

switch (Channel)
  /* Check the parameters */
  /* Return the capture 1 value */

  tmpreg = htim->Instance->CCR1; // This line is giving me the problem


The tmpreg = htim->Instance->CCR1 is always returning 0.

Any solutions or pointers that may help me?


1 Answer 1


In cases like these keep the following points in mind:

  1. Haven't used on MBED but use a debugging tool like OpenOCD bundled with gdb and try to debug the code while looking at the input registers. In eclipse you can do it with a tool like EmbSysRegisters. Google for some tutorials and you'll save a lot of time.
  2. Looking at the datasheet of DHT22, it says it needs a MCU to sensor signal to initiate the process. Have you checked that?
  • 1
    \$\begingroup\$ Plus: There must be a pullup resistor on the data line. \$\endgroup\$
    – sweber
    Nov 18, 2018 at 20:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.