STM32 capacitive soil moisture sensor

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

Picture

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 0 */
uint16_t readValue;
/* USER CODE END 0 */

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

 /* USER CODE BEGIN WHILE */
  while (1)
  {
	  HAL_ADC_PollForConversion(&hadc1,1000);
	  readValue = HAL_ADC_GetValue(&hadc1);
	  if (readValue > 2300) {
		  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_7, 1);
		  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, 0);
		  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_3, 0);
		  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, 0);
	  }
	  else if (readValue > 1800 ) {
		  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_7, 0);
		  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 > 1500) {
		  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_7, 0);
		  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_7, 0);
		  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 */

Demonstration