cancel
Showing results for 
Search instead for 
Did you mean: 

Problem reading dht11 sensor data with stm32f429i-disc1.

CMart.2
Associate II

Hi everyone. I am using a stm32f429i-disc1 board to read data from the dht11 sensor, but it cannot read the humidity and temperature.

I attach the code that I am using.

The screen displays "Hello world!", "Humidity: 0.0" and "Temp: 0.0", and when I use the stmstudio application to get a graph of each variable, I only get a 1 in the durum, mTime1, mTime2 variables. In the other variables they give 0.

I also have a stm32f407g board, with which this code is great, and with the stmstudio application I can see how the sensor works perfectly.

Anybody knows what could be the problem?

/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "cmsis_os.h"
#include "crc.h"
#include "dma2d.h"
#include "i2c.h"
#include "ltdc.h"
#include "spi.h"
#include "tim.h"
#include "usart.h"
#include "usb_host.h"
#include "gpio.h"
#include "fmc.h"
 
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "stdio.h"
#include "stm32f4xx_hal.h"
#include "stm32f429i_discovery_lcd.h"
#include "stm32f429i_discovery.h"
 
 
/* USER CODE END Includes */
/* USER CODE BEGIN PV */
TIM_HandleTypeDef htim7;
 
 
 
/* USER CODE END PV */
 
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
void MX_FREERTOS_Init(void);
/* USER CODE BEGIN PFP */
 
/* USER CODE END PFP */
 
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
void delay (uint16_t time)
  {
  	/* change your code here for the delay in microseconds */
  	__HAL_TIM_SET_COUNTER(&htim7, 0);
  	while ((__HAL_TIM_GET_COUNTER(&htim7))<time);
  }
 
 
 
 
void Set_Pin_Output (GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin)
{
	GPIO_InitTypeDef DHT11_DATA = {0};
	DHT11_DATA.Pin = GPIO_Pin;
	DHT11_DATA.Mode = GPIO_MODE_OUTPUT_PP;
	DHT11_DATA.Pull = GPIO_NOPULL;
	DHT11_DATA.Speed = GPIO_SPEED_FREQ_LOW;
 
	HAL_GPIO_Init(GPIOx, &DHT11_DATA);
}
 
void Set_Pin_Input (GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin)
{
	GPIO_InitTypeDef DHT11_DATA = {0};
	DHT11_DATA.Pin = GPIO_Pin;
	DHT11_DATA.Mode = GPIO_MODE_INPUT;
	DHT11_DATA.Pull = GPIO_NOPULL;
	DHT11_DATA.Speed = GPIO_SPEED_FREQ_LOW;
	HAL_GPIO_Init(GPIOx, &DHT11_DATA);
}
 
#define DHT11_PORT GPIOD
#define DHT11_PIN GPIO_PIN_15
 
 
uint8_t durum=0, Humidity=0, Temperature=0;
uint16_t tempVal=0, humVal=0;
uint8_t dhtVal[2];
uint8_t mData[40];
uint16_t mTime1=0, mTime2=0;
uint16_t mbit=0;
uint8_t parityVal=0,genParity=0;
 
 
 
 
uint8_t DHT11_Read (void)
{
 
	for (int a=0;a<40;a++) mData[a]=0;
		mTime1=0, mTime2=0, durum=0, tempVal=0, humVal=0, parityVal=0,genParity=0, mbit=0;
 
		Set_Pin_Output (DHT11_PORT, DHT11_PIN);
		HAL_GPIO_WritePin (DHT11_PORT, DHT11_PIN, GPIO_PIN_RESET);
		delay (18000);
		Set_Pin_Input(DHT11_PORT, DHT11_PIN);
 
		__HAL_TIM_SET_COUNTER(&htim7, 0);
		while (HAL_GPIO_ReadPin (DHT11_PORT, DHT11_PIN)==GPIO_PIN_SET) if((uint16_t)__HAL_TIM_GET_COUNTER(&htim7)> 500) return 0;
 
		__HAL_TIM_SET_COUNTER(&htim7, 0);
		while (HAL_GPIO_ReadPin (DHT11_PORT, DHT11_PIN)==GPIO_PIN_RESET) if((uint16_t)__HAL_TIM_GET_COUNTER(&htim7)> 500) return 0;
		mTime1=(uint16_t)__HAL_TIM_GET_COUNTER(&htim7);
 
		__HAL_TIM_SET_COUNTER(&htim7, 0);
		while (HAL_GPIO_ReadPin (DHT11_PORT, DHT11_PIN)==GPIO_PIN_SET) if((uint16_t)__HAL_TIM_GET_COUNTER(&htim7)> 500) return 0;
		mTime2=(uint16_t)__HAL_TIM_GET_COUNTER(&htim7);
 
		if(mTime1<75 && mTime1>85 && mTime2<75 && mTime2>85){
 
			return 0;
		}
 
 
 
		for(int j=0; j<40; j++){
			__HAL_TIM_SET_COUNTER(&htim7, 0);
			while (HAL_GPIO_ReadPin (DHT11_PORT, DHT11_PIN)==GPIO_PIN_RESET) if((uint16_t)__HAL_TIM_GET_COUNTER(&htim7)> 500) return 0;
			__HAL_TIM_SET_COUNTER(&htim7, 0);
			while (HAL_GPIO_ReadPin (DHT11_PORT, DHT11_PIN)==GPIO_PIN_SET) if((uint16_t)__HAL_TIM_GET_COUNTER(&htim7)> 500) return 0;
			mTime1=(uint16_t)__HAL_TIM_GET_COUNTER(&htim7);
 
			if(mTime1>20 && mTime1<30){
				mbit=0;
			}
			else if (mTime1>60 && mTime1<80){
				mbit=1;
			}
			mData[j]=mbit;
		}
 
		for(int i=0;i<8;i++){
			humVal+=mData[i];
			humVal=humVal<<1;
		}
 
		for(int i=16;i<24;i++){
			tempVal+=mData[i];
			tempVal=tempVal<<1;
		}
 
		for(int i=32;i<40;i++){
			parityVal+=mData[i];
			parityVal=parityVal<<1;
		}
 
		parityVal=parityVal>>1;
		humVal=humVal>>1;
		tempVal=tempVal>>1;
 
		genParity= humVal + tempVal;
 
		dhtVal[0]=tempVal;
		dhtVal[1]=humVal;
 
		return 1;
}
/* USER CODE END 0 */
 
/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
  /* USER CODE BEGIN 1 */
	char str[50];
  /* USER CODE END 1 */
 
  /* MCU Configuration--------------------------------------------------------*/
 
  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();
 
  /* USER CODE BEGIN Init */
 
  /* USER CODE END Init */
 
  /* Configure the system clock */
  SystemClock_Config();
 
  /* USER CODE BEGIN SysInit */
 
  /* USER CODE END SysInit */
 
  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_CRC_Init();
  MX_FMC_Init();
  MX_I2C3_Init();
  MX_LTDC_Init();
  MX_TIM1_Init();
  MX_USART1_UART_Init();
  MX_TIM7_Init();
  MX_DMA2D_Init();
  MX_SPI5_Init();
  /* USER CODE BEGIN 2 */
  HAL_TIM_Base_Start(&htim7);
 
 
 
  BSP_LCD_Init();
    BSP_LED_Init(LED3);
    BSP_LED_Init(LED4);
    /* Layer2 Init */
    BSP_LCD_LayerDefaultInit(1, LCD_FRAME_BUFFER);
    /* Set Foreground Layer */
    BSP_LCD_SelectLayer(1);
    /* Clear the LCD */
    BSP_LCD_Clear(LCD_COLOR_WHITE);
    BSP_LCD_SetColorKeying(1, LCD_COLOR_WHITE);
    BSP_LCD_SetLayerVisible(1, DISABLE);
    /* Layer1 Init */
    BSP_LCD_LayerDefaultInit(0, LCD_FRAME_BUFFER + 0x130000);
    /* Set Foreground Layer */
    BSP_LCD_SelectLayer(0);
    /* Enable The LCD */
    BSP_LCD_DisplayOn();
    /* Clear the LCD */
    BSP_LCD_Clear(LCD_COLOR_GREEN);
    BSP_LCD_DisplayStringAt(10, 10,(uint8_t*) "HELLO MELO!", LEFT_MODE);
 
 
    durum=DHT11_Read();
    	  	  if(durum==1){
    	  		  Temperature=tempVal;
    	  		  Humidity=humVal;
    	  		sprintf(str, "Humidity:%2.1f", (float)Humidity/ 10);
    	  		 BSP_LCD_DisplayStringAt(0, 160,(uint8_t*) str, LEFT_MODE);
    	  		sprintf(str, "Temp:%2.1f", (float)Temperature/ 10);
    	  		BSP_LCD_DisplayStringAt(0, 200,(uint8_t*) str, LEFT_MODE);
    	  		}
 
 
    	  	  HAL_Delay(1000);
 
 
  /* USER CODE END 2 */
 
  /* Call init function for freertos objects (in freertos.c) */
  MX_FREERTOS_Init(); 
  /* Start scheduler */
  osKernelStart();
 
  /* We should never get here as control is now taken by the scheduler */
  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
 
 
    /* USER CODE END WHILE */
 
    /* USER CODE BEGIN 3 */
 
  }
  /* USER CODE END 3 */
}

2 REPLIES 2
Nizar Tenzekhti
Associate II
CMart.2
Associate II

Thank you very much, it has helped me a lot!!!