STM32 development board, soil moisture detection

ceyawu 注册会员
2023-02-25 02:20

If you need other mcu, please ask me. Here is an Arduino-based development board. An example of a program to read the data measured by YL69 soil moisture sensor through Analog Input and display it on the 1602 LCD screen. The code is as follows:

#include   // 导入1602液晶显示屏库

// 定义YL69土壤湿度传感器输入引脚
const int SoilSensorPin = A0;

// 初始化液晶显示屏
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {
  // 设置液晶显示行数和列数
  lcd.begin(16, 2);
  // 显示“Soil Humidity:”文本
  lcd.print("Soil Humidity:");

void loop() {
  // 读取土壤湿度传感器的模拟输入值
  int sensorValue = analogRead(SoilSensorPin);

  // 将模拟输入值转化为土壤湿度百分比(假设传感器输出值在0-1023之间,对应土壤湿度0-100%)
  int soilHumidity = map(sensorValue, 0, 1023, 0, 100);

  // 将土壤湿度百分比显示在液晶显示屏第二行
  lcd.setCursor(0, 1);

  // 延时1秒钟后继续读取土壤湿度传感器的数据

In this sample program, we first import the LiquidCrystal library for driving the LCD. Then the constant SoilSensorPin of YL69 soil moisture sensor input pin is defined and the LCD screen is initialized. In the loop() function of the program, we use analogRead() function to read the simulated input value of the soil moisture sensor and convert it into the soil moisture percentage, and then display the soil moisture on the lcd screen through the lcd.print() function. Finally, the delay() function is used to delay 1 second, waiting for the next reading of sensor data.

OK, this is a data display program of YL69 soil moisture sensor based on STM32F103C8 chip, using timer, GPIO, ADC and LCD1602 module. The program uses ADC to collect the analog voltage value of YL69 sensor, and converts it into humidity value and displays it on the LCD1602 screen. Here are the main parts of the program:

#include "stm32f10x.h"
#include "lcd1602.h"

void ADC_Init(void)
    RCC->APB2ENR |= RCC_APB2ENR_ADC1EN; // Enable ADC1 clock
    ADC1->CR2 = 0; // Disable ADC1

    // Configure ADC1 Channel 0 (PA0)
    RCC->APB2ENR |= RCC_APB2ENR_IOPAEN; // Enable GPIOA clock
    GPIOA->CRL &= ~GPIO_CRL_CNF0; // Set GPIOA Pin 0 as analog input
    GPIOA->CRL &= ~GPIO_CRL_MODE0; // Input mode (reset state)

    ADC1->SQR1 = 0; // Only one channel used
    ADC1->SQR3 = 0; // Channel 0 (PA0) is used

    ADC1->CR2 |= ADC_CR2_CAL; // Start ADC calibration
    while ((ADC1->CR2 & ADC_CR2_CAL) != 0); // Wait for calibration to finish

    ADC1->CR2 |= ADC_CR2_ADON; // Enable ADC1

uint16_t ADC_Read(uint8_t channel)
    ADC1->SQR3 = channel;
    ADC1->CR2 |= ADC_CR2_SWSTART; // Start conversion
    while ((ADC1->SR & ADC_SR_EOC) == 0); // Wait for conversion to complete
    return ADC1->DR;

int main(void)
    uint16_t adc_value;
    float voltage, humidity;

    // Initialize LCD1602 module

    // Initialize ADC

    while (1)
        // Read ADC value from channel 0 (PA0)
        adc_value = ADC_Read(0);

        // Convert ADC value to voltage
        voltage = adc_value * 3.3 / 4095;

        // Calculate humidity based on voltage
        humidity = voltage * 100 / 3.3;

        // Display humidity on LCD1602 screen
        LCD1602_SetCursor(0, 1);
        LCD1602_WriteFloat(humidity, 2);

        // Delay for some time before updating the display
        for (volatile int i = 0; i < 1000000; i++);

Note that the custom library of the LCD1602 module is used in this program. You need to include the relevant header files in the program and configure the corresponding GPIO pins. At the same time, you also need to configure the corresponding clock and GPIO parameters in STM32CubeMX.

About the Author

Question Info

Publish Time
2023-02-25 02:20
Update Time
2023-02-25 02:20