EXPERIMENT--05-SOIL-MOISTURE-SENSOR-INTERFACE-TO-IOT-DEVELOPMENT-BOARD-

Aim: To Interface a Analog Input (soil moisture sensor) to ARM IOT development board and write a program to obtain the data on the com port

Components required: STM32 CUBE IDE, ARM IOT development board, STM programmer tool.

Theory

Hardware Overview

A typical soil moisture sensor consists of two parts.

The Probe The sensor includes a fork-shaped probe with two exposed conductors that is inserted into the soil or wherever the moisture content is to be measured.

As previously stated, it acts as a variable resistor, with resistance varying according to soil moisture. image The Module In addition, the sensor includes an electronic module that connects the probe to the Arduino.

The module generates an output voltage based on the resistance of the probe, which is available at an Analog Output (AO) pin.

The same signal is fed to an LM393 High Precision Comparator, which digitizes it and makes it available at a Digital Output (DO) pin.

image The module includes a potentiometer for adjusting the sensitivity of the digital output (DO).

You can use it to set a threshold, so that when the soil moisture level exceeds the threshold, the module outputs LOW otherwise HIGH.

This setup is very useful for triggering an action when a certain threshold is reached. For example, if the moisture level in the soil exceeds a certain threshold, you can activate a relay to start watering the plant.

Soil Moisture Sensor Pinout The soil moisture sensor is extremely simple to use and only requires four pins to connect.

soil moisture sensor pinout AO (Analog Output) generates analog output voltage proportional to the soil moisture level, so a higher level results in a higher voltage and a lower level results in a lower voltage.

DO (Digital Output) indicates whether the soil moisture level is within the limit. D0 becomes LOW when the moisture level exceeds the threshold value (as set by the potentiometer), and HIGH otherwise.

VCC supplies power to the sensor. It is recommended that the sensor be powered from 3.3V to 5V. Please keep in mind that the analog output will vary depending on the voltage supplied to the sensor.

GND is the ground pin.

image

Procedure:

  1. click on STM 32 CUBE IDE, the following screen will appear image

  2. click on FILE, click on new stm 32 project image image

  3. select the target to be programmed as shown below and click on next

image

4.select the program name image

  1. corresponding ioc file will be generated automatically image

6.select the appropriate pins as gipo, in or out, USART or required options and configure image image

7.click on cntrl+S , automaticall C program will be generated image image 8. edit the program and as per required image

  1. use project and build all image

  2. once the project is bulild image

  3. click on debug option image

  4. connect the iot board to power supply and usb

  5. After connecting open the STM cube programmer image

  6. click on UART and click on connect image

  7. once it is connected , click on Erasing and programming option image

  8. flash the bin or hex file as shown below by switching the switch to flash mode

image

  1. check for execution of the output by switching the board to run mode
  2. open serial port utility and select appropritate com port, run and verify the reuslts of moisture content .

STM 32 CUBE PROGRAM :

#include "main.h"
#include "stdio.h"

#if defined (__ICCARM__) || defined (__ARMCC_VERSION)
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#elif defined(__GNUC__)
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#endif

PUTCHAR_PROTOTYPE
{
  HAL_UART_Transmit(&huart2, (uint8_t *)&ch, 1, 0xFFFF);
  return ch;
}

while (1)
  {
	  HAL_ADC_Start(&hadc);
	  		HAL_ADC_PollForConversion(&hadc,100);
	  		adc_val = HAL_ADC_GetValue(&hadc);
	  		uint32_t soilmoisture;
	  		soilmoisture=adc_val/10.24;
	  		HAL_ADC_Stop(&hadc);
	  		HAL_Delay(500);
	  		printf("soilmoisture=:%ld\n",soilmoisture);
	  		if(adc_val<500)
	  		{
	  			 HAL_GPIO_WritePin(GPIOA, GPIO_PIN_0, GPIO_PIN_RESET);;
	  		}
	  		if(adc_val>500)
	  		{
	  			 HAL_GPIO_WritePin(GPIOA, GPIO_PIN_0, GPIO_PIN_SET);;
	  		}
  }
}

Output screen shots on serial monitor :

WhatsApp Image 2024-03-27 at 09 17 19_e3372396

WhatsApp Image 2024-03-27 at 09 00 08_bf719a26

Result :

Interfacing a Analog Input (soil moisture sensor) with ARM microcontroller based IOT development is executed and the results visualized on serial monitor