Docs

Micro-controllers, wireless transmission and database

Head

STM32 Soil moisture sensor 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

ADC1 - IN9 (tick)

Parameter Settings --> ADC Settings --> Continuous Conversion Mode (Enabled)

Set PA1, PA3, PA5 and PA7 to GPIO_Output

Additional code on top of STM32CubeIDE generated code

/* USER CODE BEGIN PV */
uint16_t readValue;
/* USER CODE END PV */
 /* USER CODE BEGIN 2 */
  HAL_ADC_Start(&hadc1);
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_7, 1);
    HAL_Delay(20);
    HAL_ADC_PollForConversion(&hadc1,1000);
    readValue = HAL_ADC_GetValue(&hadc1);
    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_7, 0);
    if (readValue > 3200)
    {
      HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, 1);
      HAL_GPIO_WritePin(GPIOA, GPIO_PIN_3, 0);
      HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, 0);
    }
    else if (readValue > 2700 )
    {
      HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, 0);
      HAL_GPIO_WritePin(GPIOA, GPIO_PIN_3, 1);
      HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, 0);
    }
    else
    {
      HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, 0);
      HAL_GPIO_WritePin(GPIOA, GPIO_PIN_3, 0);
      HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, 1);
    }
    HAL_Delay(500);
    /* USER CODE END WHILE */

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