树莓派练习程序(光敏电阻)

光敏电阻模块如下图:

树莓派的引脚如下图:

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

 输出结果:

posted @ 2018-01-24 11:36  Dsp Tian  阅读(3437)  评论(0编辑  收藏  举报