Play with Raspberry Pi (12) - U-shaped photoelectric sensor

Play with Raspberry Pi (12) - U-shaped photoelectric sensor

    In this blog, we will use a U-shaped photoelectric sensor to implement a simple speed measurement tool. The components used in this experiment are simple, and the principle of the experiment is also very simple.

1. About U-shaped photoelectric sensor

    The U-shaped photoelectric sensor is named because it has a U-shaped groove. One side of the groove emits infrared light, and the other side of the groove is equipped with a receiving device. By blocking and conducting the infrared light, the level of the signal source pin of the component is changed. Based on this characteristic, we can easily use a U-shaped photoelectric sensor to measure whether there is an object blocking the light. In the field of speed measurement, it is widely used.

    The sensor module used in this experiment is shown in the following figure:

It can be seen that this component has 3 pins, the GND pin is the ground pin, the VCC pin is the power supply pin, and the OUT pin is the output pin. When the receiver can receive the light emitted from the emission source, The output pin is low and when the light is blocked, the output pin is high.

2. Counter OR Tachometer

    U-shaped photoelectric sensors have a very wide range of applications. Just imagine, if a workshop assembly line is constantly producing goods, how can it be convenient to automatically count the number of goods produced? At this time, we can install a U-shaped photoelectric sensor on the conveyor belt of the commodity. As long as a commodity passes through the conveyor belt, it will block the infrared light emitted by the sensor emission source, thereby triggering the jump of the level and realizing the counting function. In addition, the U-shaped photoelectric sensor can also easily measure the speed. As long as we know the length of the object and the time it takes for the object to pass through the U-shaped sensor, we can use the formula ** v = s / t ** to calculate the movement of the object speed. Let's write code to implement these two functions.

    First, connect the components to the Raspberry Pi as follows:

U-shaped photoelectric sensor raspberry pie
VCC 3.3V power supply
GND GND
OUT GPIO17 (BCM code)

Write the following code:

#coding:utf-8

import RPi.GPIO as GPIO
import time
# 采用物理编码
GPIO.setmode(GPIO.BOARD)
# BCM GPIO17的物理编码是11
out_pin = 11
# 0 计数模式 1 测速模式
MODE = 0
count = 0
# 测速模式下,需要初始化长度S 单位为毫米
S = 10
t1 = 0
t2 = 0

# 进行引脚的初始化,不被遮挡时为低电平,使用低电平的下拉电阻
GPIO.setup(out_pin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

# 定义回调函数
def switch_state(pin):
	global count,t1,t2
	# 如果是高电平
	if GPIO.input(pin):
		print("物体遮挡")
		if MODE == 0:
			count += 1
		else:
			t1 = time.time()
	else:
		print("物体遮挡消失")
		if MODE == 0:
			print('计数器-数量:%d'%(count))
		else:
			t2 = time.time()
			t = t2 - t1
			v = S / t
			print('测速器-速度%fmm/s'%(v))

GPIO.add_event_detect(out_pin, GPIO.BOTH, callback=switch_state)

while True:
	pass

As shown in the above code, we can set the MODE variable to make the component work in counting mode or speed measurement mode. Run this code on the Raspberry Pi and try the counting and speed measurement functions.

Focus on technology, understand love, be willing to share, be a friend

QQ:316045346

{{o.name}}
{{m.name}}

Guess you like

Origin my.oschina.net/u/2340880/blog/5235864