Build a Raspberry Pi infrared light barrier and measure speed

In this tutorial, I want to show you how to set up and use a small infrared light barrier, which gives a signal as soon as the light barrier is interrupted. By traversing both photocells one behind the other, the speed can be measured.





As soon as the light barrier is interrupted, a positive signal should be sent, otherwise a negative (0). I use a pull-up resistor with the IR receiver (black) instead of the switch.

EGHS-PullUpDownSwitchProtectedThe two photocells are 10cm apart for me. For the speed to be calculated correctly, you may need to adjust the distance in your script. The actual structure is as follows:

lichtschranke_SteckplatineThe little red LED is only for testing and can be omitted.

Once you’ve set up the circuit, the LED should light up as soon as the light barrier is closed (for example, fingers in between).


Measure speed

So that the speed can now also be measured, the time is measured, which passes, until both light barriers are run through. If the distance is not exactly 10cm, you should adjust this.

sudo nano

And add the following code

Save and exit with CTRL + O and CTRL + X. Now we can start it.

sudo python

If the script does not work properly, it is most likely due to the fact that the photocells do not point exactly to each other and so, some electricity can flow, but this is out of the accepted range and it is not determined whether 0 or 1

Concluding with another picture of my structure::

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Subscribe to Raspberry Pi Tutorials and don't miss any new Tutorial!