Docs

Micro-controllers, wireless transmission and database

Head

DHT22 Humidity & Temperature Sensor with Blue Pill using STM32CubeIDE

Prerequisites

This project assumes you have already installed STM32CubeIDE. You need to have previously done a basic blink sketch with blue-pill using STM32CubeIDE. I have made a complete video from installing STM32CubeIDE to LED blink program. You can watch it by clicking this link. https://www.youtube.com/watch?v=kXg467nVd_A

Wiring Diagram

Diagram

Image1

Image2

STM32CubeIDE Settings

Click RCC → High Speed Clock (HSE) to Crystal/Ceramic Resonator

Click Clock Configuration tab → HCLK (MHz) to 72

Click Pinout and Configuration tab

Click Timer → Click TIM1 →

Clock Source set to Internal Clock

Configuration → Parameter Settings →

Prescaler set to 71

Set PB9 GPIO_Output

Click connectivity --> Click I2C1

For I2C select I2C

Configuration --> Parameter Settings

For I2C speed select Fast Mode

Libraries

Inside Core/Inc Folder

fonts.h ssd1306.h

Inside Core/Src Folder

fonts.c ssd1306.c

Additional code on top of STM32CubeIDE generated code

/* USER CODE BEGIN Includes */
#include "fonts.h"
#include "ssd1306.h"
#include "stdio.h"
/* USER CODE END Includes */

/* USER CODE BEGIN 0 */
#define DHT22_PORT GPIOB
#define DHT22_PIN GPIO_PIN_9
uint8_t RH1, RH2, TC1, TC2, SUM, CHECK;
uint32_t pMillis, cMillis;
float tCelsius = 0;
float tFahrenheit = 0;
float RH = 0;
uint8_t RHI, RHD, TCI, TCD, TFI, TFD;
char strCopy[15];

void microDelay (uint16_t delay)
{
  __HAL_TIM_SET_COUNTER(&htim1, 0);
  while (__HAL_TIM_GET_COUNTER(&htim1) < delay);
}

uint8_t DHT22_Start (void)
{
  uint8_t Response = 0;
  GPIO_InitTypeDef GPIO_InitStructPrivate = {0};
  GPIO_InitStructPrivate.Pin = DHT22_PIN;
  GPIO_InitStructPrivate.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStructPrivate.Speed = GPIO_SPEED_FREQ_LOW;
  GPIO_InitStructPrivate.Pull = GPIO_NOPULL;
  HAL_GPIO_Init(DHT22_PORT, &GPIO_InitStructPrivate); // set the pin as output
  HAL_GPIO_WritePin (DHT22_PORT, DHT22_PIN, 0);   // pull the pin low
  microDelay (1300);   // wait for 1300us
  HAL_GPIO_WritePin (DHT22_PORT, DHT22_PIN, 1);   // pull the pin high
  microDelay (30);   // wait for 30us
  GPIO_InitStructPrivate.Mode = GPIO_MODE_INPUT;
  GPIO_InitStructPrivate.Pull = GPIO_PULLUP;
  HAL_GPIO_Init(DHT22_PORT, &GPIO_InitStructPrivate); // set the pin as input
  microDelay (40);
  if (!(HAL_GPIO_ReadPin (DHT22_PORT, DHT22_PIN)))
  {
    microDelay (80);
    if ((HAL_GPIO_ReadPin (DHT22_PORT, DHT22_PIN))) Response = 1;
  }
  pMillis = HAL_GetTick();
  cMillis = HAL_GetTick();
  while ((HAL_GPIO_ReadPin (DHT22_PORT, DHT22_PIN)) && pMillis + 2 > cMillis)
  {
    cMillis = HAL_GetTick();
  }
  return Response;
}

uint8_t DHT22_Read (void)
{
  uint8_t a,b;
  for (a=0;a<8;a++)
  {
    pMillis = HAL_GetTick();
    cMillis = HAL_GetTick();
    while (!(HAL_GPIO_ReadPin (DHT22_PORT, DHT22_PIN)) && pMillis + 2 > cMillis)
    {  // wait for the pin to go high
      cMillis = HAL_GetTick();
    }
    microDelay (40);   // wait for 40 us
    if (!(HAL_GPIO_ReadPin (DHT22_PORT, DHT22_PIN)))   // if the pin is low
      b&= ~(1<<(7-a));
    else
      b|= (1<<(7-a));
    pMillis = HAL_GetTick();
    cMillis = HAL_GetTick();
    while ((HAL_GPIO_ReadPin (DHT22_PORT, DHT22_PIN)) && pMillis + 2 > cMillis)
    {  // wait for the pin to go low
      cMillis = HAL_GetTick();
    }
  }
  return b;
}
/* USER CODE END 0 */

  /* USER CODE BEGIN 2 */
  HAL_TIM_Base_Start(&htim1);
  SSD1306_Init();
  /* USER CODE END 2 */

/* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    if(DHT22_Start())
    {
      RH1 = DHT22_Read(); // First 8bits of humidity
      RH2 = DHT22_Read(); // Second 8bits of Relative humidity
      TC1 = DHT22_Read(); // First 8bits of Celsius
      TC2 = DHT22_Read(); // Second 8bits of Celsius
      SUM = DHT22_Read(); // Check sum
      CHECK = RH1 + RH2 + TC1 + TC2;
      if (CHECK == SUM)
      {
        if (TC1>127) // If TC1=10000000, negative temperature
        {
          tCelsius = (float)TC2/10*(-1);
        }
        else
        {
          tCelsius = (float)((TC1<<8)|TC2)/10;
        }
        tFahrenheit = tCelsius * 9/5 + 32;
        RH = (float) ((RH1<<8)|RH2)/10;
        SSD1306_GotoXY (0, 0);
        RHI = RH;  // Relative humidity integral
        RHD = RH*10-RHI*10; // Relative humidity decimal
        sprintf(strCopy,"%d.%d %%  ", RHI, RHD);
        SSD1306_Puts (strCopy, &Font_11x18, 1);
        SSD1306_GotoXY (0, 20);
        if (tCelsius < 0)
        {
          TCI = tCelsius *(-1);  // Celsius integral
          TCD = tCelsius*(-10)-TCI*10; // Celsius decimal
          sprintf(strCopy,"-%d.%d C   ", TCI, TCD);
        }
        else
        {
          TCI = tCelsius;  // Celsius integral
          TCD = tCelsius*10-TCI*10; // Celsius decimal
          sprintf(strCopy,"%d.%d C   ", TCI, TCD);
        }
        SSD1306_Puts (strCopy, &Font_11x18, 1);
        SSD1306_GotoXY (0, 40);
        if(tFahrenheit < 0)
        {
          TFI = tFahrenheit*(-1);  // Fahrenheit integral
          TFD = tFahrenheit*(-10)-TFI*10; // Fahrenheit decimal
          sprintf(strCopy,"-%d.%d F   ", TFI, TFD);
        }
        else
        {
          TFI = tFahrenheit;  // Fahrenheit integral
          TFD = tFahrenheit*10-TFI*10; // Fahrenheit decimal
          sprintf(strCopy,"%d.%d F   ", TFI, TFD);
        }
        SSD1306_Puts (strCopy, &Font_11x18, 1);
        SSD1306_UpdateScreen();
      }
    }
    HAL_Delay(1000);
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */