水滴检测模块如下:

树莓派练习程序(雨水检测)

树莓派的引脚如下图:

树莓派练习程序(雨水检测)

我们将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("no water\n");
            delay(333);
        }
        else
        {
            printf("water detected\n");
            delay(333);
        }
    }
        
    return 0;
}

输出结果:

树莓派练习程序(雨水检测)

相关文章:

  • 2022-03-14
  • 2022-03-14
  • 2021-07-01
  • 2021-12-19
  • 2022-02-03
  • 2021-12-24
  • 2021-11-28
  • 2021-12-26
猜你喜欢
  • 2022-03-15
  • 2021-05-26
  • 2021-11-15
  • 2021-12-12
  • 2021-11-14
  • 2022-03-14
  • 2021-06-26
相关资源
相似解决方案