photo interrupter u型光电断路器 模块 树莓派3

photo interrupter u型光电断路器,有一个发射端和接收端组成,是一种对射式光电传感器。障碍物从两端之间穿过,遮挡光线,从而改变输出电平。广泛用于速度测试。文章内容包括模块原理图,模块接线图,相关代码。

原理图根据模块测量来的,这里有个疑问:接上vcc和gnd后led灯为什么会亮?????????????

photo interrupter

模块接线图:

photo interrupter

相关代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <wiringPi.h>
#include <stdio.h>

#define LBPin0 // light break pin set to GPIO0
#define Gpin1
#define Rpin2

void LED(int color)
{
pinMode(Gpin, OUTPUT);
pinMode(Rpin, OUTPUT);
if (color == 0){
digitalWrite(Rpin, HIGH);
digitalWrite(Gpin, LOW);
}
else if (color == 1){
digitalWrite(Rpin, LOW);
digitalWrite(Gpin, HIGH);
}
}

void Print(int x){
if ( x == 0 ){
printf("Light was blocked\n");
} else {
printf("Light was not blocked\n");
}
}

int main(void){

if(wiringPiSetup() == -1){ //when initialize wiring failed,print messageto screen
printf("setup wiringPi failed !");
return 1;
}

pinMode(LBPin, INPUT);
int temp;
while(1){
//Reverse the input of LBPin
if ( digitalRead(LBPin) == 0 ){
temp = 1;
}
if ( digitalRead(LBPin) == 1 ){
temp = 0;
}

system("clear");
LED(temp);
Print(temp);
delay(10);
}
return 0;
}

原理图Onedrive分享请点击https://1drv.ms/u/s!Ajlt0jS9cFmqkOEY56Ht2hWxKd8awQ?e=KRPvKr

从入门到放弃的学习RASPBERRYPI请点击https://tl8517.com/category/raspberrypi/