土壤湿度检测模块如下:

树莓派练习程序(土壤湿度检测)

树莓派的引脚如下图:

树莓派练习程序(土壤湿度检测)

我们将Vcc引脚连接物理接口2,GND引脚连接物理接口39,DO引脚连接物理接口40。

实物连接如下图:

树莓派练习程序(土壤湿度检测)

编程使用WiringPi库,使用wpi引脚编码方式控制GPIO。

代码如下:

#include <wiringPi.h>
#include <stdio.h>
#include <sys/time.h>

#define GuanMin    29

int main(void)
{

    if (wiringPiSetup() == -1) 
    { 
        printf("setup wiringPi failed !");
        return 1; 
    }
    
    pinMode(GuanMin, INPUT);        
    
    while (1) 
    {
        if (digitalRead(GuanMin) == 1)
        {
            printf("Dry\n");
            delay(333);
        }
        else
        {
            printf("water detected\n");
            delay(333);
        }
    }
        
    return 0;
}

输出结果:

树莓派练习程序(土壤湿度检测)

相关文章:

  • 2021-09-30
  • 2021-12-12
  • 2022-03-14
  • 2021-12-10
  • 2021-06-26
  • 2022-03-14
  • 2022-03-14
  • 2021-09-13
猜你喜欢
  • 2021-11-14
  • 2021-08-20
  • 2022-03-15
  • 2021-10-27
  • 2021-05-26
  • 2021-12-05
  • 2022-12-23
相关资源
相似解决方案