我们将Vcc引脚连接物理接口2,GND引脚连接物理接口39,DO引脚连接物理接口40。电源5v
#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;
}
编译命令:
gcc water.c -o water -lwiringPi
python 版
#coding=utf-8
#导入 GPIO库
import RPi.GPIO as GPIO
import time
#设置 GPIO 模式为 BCM
GPIO.setmode(GPIO.BCM)
GPIO.setup(18,GPIO.IN)
while True:
if GPIO.input(18):
print("no rain")
else:
print("rain")
time.sleep(1)
网友评论