We use cookies to ensure that we give you the best experience on our websites. By continuing to visit
this site you agree to our use of cookies.
Read our cookie policy.
How To Use A Break Beam Sensor & How To Make Your Own Light Gate On The Cheap
So many uses for these cheap and versatile break beam sensors, let us show you what you need, how to use them, & how to code them for a variety of projects. You get bonus project and learn about polling and interrupt requests (IRQ).
We needed a light gate to perform various experiments for our Youtube Channel, website, and learning journey.
The cheapest light gates cost well over £100 and then you needed to buy a data logger too. We ended up with something we think is more versatile for a few pounds and had a heap of fun & learning making it.
We'll show you how to make a very cheap version, then add some bonus functionality by adding a button and screen (still very cheap). We will also show you how to recycle this to make other projects.
Learn how to make a security beam alarm, counter, speed trap, & measure acceleration.
For example here is code which we used to count coins dropped into a money box. You could use it as an electronic turn style to count people or vehicles.
Instead of polling and hoping we don't miss the beam being broken or restored we can use an interrupt request or IRQ.
Soon we will be dealing with events that take a fraction of a second, so the code below does the same as our 1st simple code but uses an IRQ instead of polling.
To set up an IRQ you run the irq method on a pin and give it a handler and trigger. The trigger is what you want to listen for on the pin to set off your code. We are listening for a rising or falling edge, so if the pin changes from low to high or vice versa.
The handler is the function you want to run when the event you are listening for occurs. In our case it's our beam_change function.
Here we are just printing true or false depending on if the beam is broken or restored like our first simple code version.
time.ticks_us() gives us a number and next time we check again with time.ticks_us() it will give us the same number but with the number of microseconds since we last checked added to it.
time.ticks_diff(time_1,time_2) will give the difference between 2 points in time so we can measure elapsed time.
It's amazing we can measure millionths of a second with such cheap equipment. When Isaac Newton was born (1689) the pendulum clock had only been around for 23 years (1656). Before this there was little point in clocks showing seconds or even minutes due to their lack of accuracy.
The code is just remembering the point in time when the beam is broken and when it's restored using the new time to calculate the duration it was broken. We are dividing by 1,000,000 because we want to work in seconds not microseconds.
Now it's trivial to measure speed/velocity which is just distance divided by time. We attach a piece of card to the object we want to measure the speed of. Our previous code already measures time so we just add in a variable we set the the length of the card and it's done.
The formula for average acceleration is the change in speed/velocity divided by the time taken for that change.
We could use 2 light gates and have the card pass through both, but it's simpler to use card with 2 tabs which will break the beam twice.
The code below includes a reset button to take multiple measurements, and an SSD1306 screen. You could replace the screen setup
and update_screen function to output the results to any kind of screen you choose. Or you could save the results to the device or any number of alternative options to suit your needs.
It took a long time to make this video, at the end we wanted to add the skateboard acceleration clip and in our haste didn't sanity check the results. Jon quite rightly points out there was an error in the measurement. Best to take multiple measurements, and it wasn't very scientific holding the device in my hand.