cancel
Showing results for 
Search instead for 
Did you mean: 

Debugging temperature sensor data collection on STM32F303K8, cannot collect DHT11 sensor data?

MJone.2
Associate

I am using UART to display the live readings of my DHT11 sensor similar to this blog post https://controllerstech.com/using-dht11-sensor-with-stm32/ and my program seems to stop around the DHT11_Read() or DHT11_Check_Response() functions. It seems to be some problem with reading the pin and getting a response that I'm not sure if the problem is timing or some problem with the physical device. It won't let me post 100% of the code but here is most of the non-auto generated stuff:

0693W000005B02OQAS.png#include "main.h"

#include "stdio.h"

#include "stdlib.h"

TIM_HandleTypeDef htim6;

UART_HandleTypeDef huart1;

void SystemClock_Config(void);

static void MX_GPIO_Init(void);

static void MX_TIM6_Init(void);

static void MX_USART1_UART_Init(void);

void delay (uint16_t time)

{

      /* change your code here for the delay in microseconds */

      __HAL_TIM_SET_COUNTER(&htim6, 0);

      while ((__HAL_TIM_GET_COUNTER(&htim6))<time);

}

/* USER CODE END 0 */

uint8_t Rh_byte1, Rh_byte2, Temp_byte1, Temp_byte2;

uint16_t SUM, RH, TEMP;

float Temperature = 0;

float Humidity = 0;

uint8_t Presence = 0;

void Set_Pin_Output (GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin)

{

      GPIO_InitTypeDef GPIO_InitStruct = {0};

      GPIO_InitStruct.Pin = GPIO_Pin;

      GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;

      GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;

      HAL_GPIO_Init(GPIOx, &GPIO_InitStruct);

}

void Set_Pin_Input (GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin)

{

      GPIO_InitTypeDef GPIO_InitStruct = {0};

      GPIO_InitStruct.Pin = GPIO_Pin;

      GPIO_InitStruct.Mode = GPIO_MODE_INPUT;

      GPIO_InitStruct.Pull = GPIO_PULLUP;

      HAL_GPIO_Init(GPIOx, &GPIO_InitStruct);

}

#define DHT11_PORT GPIOA

#define DHT11_PIN GPIO_PIN_1

void DHT11_Start (void)

{

      Set_Pin_Output (DHT11_PORT, DHT11_PIN); // set the pin as output

      HAL_GPIO_WritePin (DHT11_PORT, DHT11_PIN, 0);  // pull the pin low

      delay (18000);  // wait for 18ms

   HAL_GPIO_WritePin (DHT11_PORT, DHT11_PIN, 1);  // pull the pin high

      delay (20);  // wait for 20us

      Set_Pin_Input(DHT11_PORT, DHT11_PIN);   // set as input

}

uint8_t DHT11_Check_Response (void)

{

      uint8_t Response = 0;

      delay (40);

      if (!(HAL_GPIO_ReadPin (DHT11_PORT, DHT11_PIN)))

      {

            delay (80);

            if ((HAL_GPIO_ReadPin (DHT11_PORT, DHT11_PIN))) Response = 1;

            else Response = -1; // 255

      }

      while ((HAL_GPIO_ReadPin (DHT11_PORT, DHT11_PIN)));  // wait for the pin to go low

      return Response;

}

uint8_t DHT11_Read (void)

{

      uint8_t i,j;

      for (j=0;j<8;j++)

      {

            while (!(HAL_GPIO_ReadPin (DHT11_PORT, DHT11_PIN)));  // wait for the pin to go high

            delay (40);  // wait for 40 us

            if (!(HAL_GPIO_ReadPin (DHT11_PORT, DHT11_PIN)))  // if the pin is low

            {

                   i&= ~(1<<(7-j));  // write 0

            }

            else i|= (1<<(7-j)); // if the pin is high, write 1

            while ((HAL_GPIO_ReadPin (DHT11_PORT, DHT11_PIN))); // wait for the pin to go low

      }

      return i;

}

/**

 * @brief The application entry point.

 * @retval int

 */

int main(void)

{

 MX_GPIO_Init();

 MX_TIM6_Init();

 MX_USART1_UART_Init();

 /* USER CODE BEGIN 2 */

 HAL_TIM_Base_Start(&htim6);

 while (1)

 {

       uint8_t d[] = "Temp: \n";

       //printf("TEST HELLO");

       //HAL_UART_Transmit(&huart1, (uint8_t)Temperature, sizeof(Temperature), 50);

       //HAL_UART_Transmit(&huart1, d , sizeof(d), 50);

   /* USER CODE END WHILE */

       DHT11_Start();

             Presence = DHT11_Check_Response();

             Rh_byte1 = DHT11_Read ();

             Rh_byte2 = DHT11_Read ();

             Temp_byte1 = DHT11_Read ();

             Temp_byte2 = DHT11_Read ();

             SUM = DHT11_Read();

             TEMP = Temp_byte1;

             RH = Rh_byte1;

             Temperature = (float) TEMP;

             Humidity = (float) RH;

             uint8_t data[] = "Temp: \n";

            // HAL_UART_Receive(&huart1, (uint8_t)Temperature, sizeof(Temperature), 50);

             HAL_UART_Transmit(&huart1, (uint8_t)Temperature, sizeof(Temperature), 50);

             HAL_UART_Transmit(&huart1, data , sizeof(data), 50);

            // HAL_UART_Transmit(&huart1, TEMP , sizeof(TEMP), 50);

             HAL_Delay(500);

   /* USER CODE BEGIN 3 */

 }

 /* USER CODE END 3 */

}

0693W000005B011QAC.jpg

0 REPLIES 0