风筝
发表于: 2022-11-15 17:02:00 | 显示全部楼层

当您听到智能花园一词时,想到的第一件事肯定是一个可以实现监视土壤水分含量并自动浇水的系统。使用该系统,只有在需要时才浇水,避免过度或不足。


如果您想构建这样的系统,毫无疑问,您将需要土壤水分传感器。


土壤水分传感器如何工作?

土壤水分传感器(Soil Moisture Sensor)以一种简单明了的方式运行。带有两个裸露导体的叉形探针充当可变电阻器(类似于电位计),其电阻随土壤的水分含量而变化。

Soil-Moisture-Sensor-Working.gif

电阻和土壤水分含量呈反比:

●    土壤中的水越多,电导率就越好,电阻越低。

●    土壤中的水越少,电导率就越低,电阻越高。

传感器根据电阻产生输出电压,通过测量我们可以确定土壤水分含量。


硬件概述

常见的土壤水分传感器由两个部分组成。传感器包括一个带有两个裸露导体的叉形探针,这些探针被插入土壤或要测量水分含量的任何地方。


如前所述,它充当可变电阻器,电阻因土壤水分而变化。

Soil-Moisture-Sensor-Probe.jpg


此外,传感器还包括一个将探针连接到Arduino的电子模块。该模块根据探针的电阻生成输出电压,可在模拟输出(AO)引脚处测量。


同一信号还被馈送到LM393高精度比较器,该比较器将其数字化,并可以在数字输出(DO)引脚获取。

Soil-Moisture-Sensor-Sensitivity-Adjustment.jpg


该模块包括一个电位器,用于调整数字输出的灵敏度。您可以使用它来设置阈值,以便当土壤水分含量超过阈值时,模块输出低电平。


当达到一定的阈值时,此设置对于触发动作非常有用。例如,如果土壤中的水分含量超过一定阈值,则可以激活继电器开始浇水。

Soil-Moisture-Sensor-Power-and-Status-LEDs.jpg


该模块还包括两个LED灯。当模块供电时时,电源指示灯点亮,当土壤水分含量超过阈值时,状态LED会点亮。


土壤水分传感器引脚

土壤水分传感器非常易于使用,只需要四个引脚即可连接。

Soil-Moisture-Sensor-Pinout.jpg

●    AO(模拟输出)产生的模拟输出电压与土壤水分含量成正比,因此较高的含量会导致更高的电压。

●    DO(数字输出)表示土壤水分含量是否在极限之内。当水分含量超过阈值时,D0会变低电平。

●    VCC 向传感器提供电源。建议传感器采用3.3V-5V。请记住,模拟输出将根据提供给传感器的电压而有所不同。

●    GND 是接地引脚。


实验1  - 使用模拟输出(A0)测量土壤水分

在第一个实验中,我们将读取模拟输出以估算土壤水分的含量。首先为传感器供电。您可以将模块的VCC引脚连接到Arduino的5V引脚。


但是,这些传感器的一个众所周知的问题是它们的寿命较短,因为它们不断暴露于水分。此外,埋在土壤中时不断向传感器施加电源会显着加速腐蚀速度。为了避免这种情况,建议仅在读取时才向传感器供电。


一种简单的方法是将传感器的电源引脚连接到Arduino上的数字引脚,并根据需要将其设置为高或低。因此,我们将VCC引脚连接到Arduino的数字引脚#7。


最后,将A0引脚连接到Arduino的ADC引脚A0。下图显示了接线方式。

Wiring-Soil-Moisture-Sensor-with-Arduino-For-Reading-Analog-Output.jpg


查找阈值

要估计土壤水分含量,请记录当土壤尽可能干燥和完全饱和时传感器输出的值。


只需运行下面的草图并进行读数即可。

  1. void setup() {
  2.         pinMode(sensorPower, OUTPUT);
  3.         
  4.         // Initially keep the sensor OFF
  5.         digitalWrite(sensorPower, LOW);
  6.         
  7.         Serial.begin(9600);
  8. }

  9. void loop() {
  10.         //get the reading from the function below and print it
  11.         Serial.print("Analog output: ");
  12.         Serial.println(readSensor());
  13.         
  14.         delay(1000);
  15. }

  16. //  This function returns the analog soil moisture measurement
  17. int readSensor() {
  18.         digitalWrite(sensorPower, HIGH);        // Turn the sensor ON
  19.         delay(10);                                                        // Allow power to settle
  20.         int val = analogRead(sensorPin);        // Read the analog value form sensor
  21.         digitalWrite(sensorPower, LOW);                // Turn the sensor OFF
  22.         return val;                                                        // Return analog moisture value
  23. }
复制代码

当您运行草图时,您应该看到与下面的读数类似的读数:

●    当土壤干燥时(读数约850)

●    当土壤全湿时(读数约400)

Calibrating-Soil-Moisture-Sensor.jpg

该测试可能需要一些反复试验。 读取后,您可以将它们用作阈值来触发动作。


Arduino代码

下面的草图使用以下阈值估算土壤水分的含量:

  1. /* Change these values based on your calibration values */
  2. #define soilWet 500   // Define max value we consider soil 'wet'
  3. #define soilDry 750   // Define min value we consider soil 'dry'

  4. // Sensor pins
  5. #define sensorPower 7
  6. #define sensorPin A0

  7. void setup() {
  8.         pinMode(sensorPower, OUTPUT);
  9.         
  10.         // Initially keep the sensor OFF
  11.         digitalWrite(sensorPower, LOW);
  12.         
  13.         Serial.begin(9600);
  14. }

  15. void loop() {
  16.         //get the reading from the function below and print it
  17.         int moisture = readSensor();
  18.         Serial.print("Analog Output: ");
  19.         Serial.println(moisture);

  20.         // Determine status of our soil
  21.         if (moisture < soilWet) {
  22.                 Serial.println("Status: Soil is too wet");
  23.         } else if (moisture >= soilWet && moisture < soilDry) {
  24.                 Serial.println("Status: Soil moisture is perfect");
  25.         } else {
  26.                 Serial.println("Status: Soil is too dry - time to water!");
  27.         }
  28.         
  29.         delay(1000);        // Take a reading every second for testing
  30.                                         // Normally you should take reading perhaps once or twice a day
  31.         Serial.println();
  32. }

  33. //  This function returns the analog soil moisture measurement
  34. int readSensor() {
  35.         digitalWrite(sensorPower, HIGH);        // Turn the sensor ON
  36.         delay(10);                                                        // Allow power to settle
  37.         int val = analogRead(sensorPin);        // Read the analog value form sensor
  38.         digitalWrite(sensorPower, LOW);                // Turn the sensor OFF
  39.         return val;                                                        // Return analog moisture value
  40. }
复制代码

如果一切工作正常,您应该在串口显示器上看到类似结果。

Soil-Moisture-Sensor-Analog-Output.jpg


实验2  - 使用数字输出(D0)测量土壤水分

在第二次实验中,我们将使用数字输出来确定土壤水分含量是否在可接受的阈值范围内。


我们将重复使用以前的实验电路。只需断开与ADC引脚的连接,然后将模块上的D0引脚连接到Arduino的数字引脚#8。


下图显示了接线方式。

Wiring-Soil-Moisture-Sensor-with-Arduino-For-Reading-Digital-Output.jpg


设置阈值

该模块具有内置电位器,用于设置水分阈值,超过该阈值则该模块输出低电平和,并且状态LED灯将亮起。


现在,要设置阈值,请在植物需要浇水时将探针插进土壤中,然后顺时针旋转旋钮,直到状态LED打开。然后,逆时针旋转旋钮,直到LED熄灭。


Arduino代码

现在,将下面的草图上传到Arduino开发板。

  1. // Sensor pins
  2. #define sensorPower 7
  3. #define sensorPin 8

  4. void setup() {
  5.         pinMode(sensorPower, OUTPUT);

  6.         // Initially keep the sensor OFF
  7.         digitalWrite(sensorPower, LOW);

  8.         Serial.begin(9600);
  9. }

  10. void loop() {
  11.         //get the reading from the function below and print it
  12.         int val = readSensor();
  13.         Serial.print("Digital Output: ");
  14.         Serial.println(val);

  15.         // Determine status of our soil moisture situation
  16.         if (val) {
  17.                 Serial.println("Status: Soil is too dry - time to water!");
  18.         } else {
  19.                 Serial.println("Status: Soil moisture is perfect");
  20.         }

  21.         delay(1000);        // Take a reading every second for testing
  22.                                         // Normally you shoul take reading perhaps every 12 hours
  23.         Serial.println();
  24. }

  25. //  This function returns the analog soil moisture measurement
  26. int readSensor() {
  27.         digitalWrite(sensorPower, HIGH);  // Turn the sensor ON
  28.         delay(10);              // Allow power to settle
  29.         int val = digitalRead(sensorPin); // Read the analog value form sensor
  30.         digitalWrite(sensorPower, LOW);   // Turn the sensor OFF
  31.         return val;             // Return analog moisture value
  32. }
复制代码

如果一切工作正常,您应该在串口显示器上看到类似的输出。

Soil-Moisture-Sensor-Digital-Output.jpg

跳转到指定楼层
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题 700 | 回复: 1480



手机版|

GMT+8, 2024-4-19 23:25 , Processed in 0.151826 second(s), 6 queries , Gzip On, MemCache On. Powered by Discuz! X3.5

YiBoard一板网 © 2015-2022 地址:河北省石家庄市长安区高营大街 ( 冀ICP备18020117号 )

快速回复 返回顶部 返回列表