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
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.
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.
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.
-
select the target to be programmed as shown below and click on next
6.select the appropriate pins as gipo, in or out, USART or required options and configure
7.click on cntrl+S , automaticall C program will be generated
8. edit the program and as per required
-
connect the iot board to power supply and usb
-
once it is connected , click on Erasing and programming option
-
flash the bin or hex file as shown below by switching the switch to flash mode
- check for execution of the output by switching the board to run mode
- open serial port utility and select appropritate com port, run and verify the reuslts of moisture content .
#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);;
}
}
}
Interfacing a Analog Input (soil moisture sensor) with ARM microcontroller based IOT development is executed and the results visualized on serial monitor