Micro:Bit PhotoGates
Lesson Objectives:
- Students will learn about conditionals
- Students will learn to send signals between micro:bits using the radio functionality
Materials:
- 2 BBC micro:bits
- Laptop with Mu Software
- 2 flashlights OR 1 bright LED
- Assorted LEGO or other materials to construct testing set-up
Let's Get Started!
A photogate is a sensor used for timing events. It can detect a change in state when something passes in front of it, either from low light level to high light level or vice versa. When you measure the time between these events (either on a single photogate or with multiple), you can accurately determine how long it took an object to travel a known distance. This can be used for a variety of applications, such as timing how long it takes a car to travel a certain distance, or determining the period of a pendulum.
More Information
Goal:
Create a photogate system using micro:bits. The LED's in the micro:bit's display can be reversed and used as a light sensor. We can detect changes in the light level the micro:bit measures and then communicate between 2 micro:bits to determine the time it takes for an object to pass between 2 photogates. We will want to write 2 separate pieces of code:
A photogate is a sensor used for timing events. It can detect a change in state when something passes in front of it, either from low light level to high light level or vice versa. When you measure the time between these events (either on a single photogate or with multiple), you can accurately determine how long it took an object to travel a known distance. This can be used for a variety of applications, such as timing how long it takes a car to travel a certain distance, or determining the period of a pendulum.
More Information
Goal:
Create a photogate system using micro:bits. The LED's in the micro:bit's display can be reversed and used as a light sensor. We can detect changes in the light level the micro:bit measures and then communicate between 2 micro:bits to determine the time it takes for an object to pass between 2 photogates. We will want to write 2 separate pieces of code:
- Start Photogate: This micro:bit should send a signal to the end photogate when it detects an object passing through it.
- End Photogate: This micro:bit should wait for a signal from the start photogate, then start a timer and continue until it detects the object. It should then display the time elapsed.
Photogate Detection Logic:
The photogates should detect the transition from dimness to brightness. Dimness (or LOW) is defined as being below a defined threshold and brightness (or HIGH) is above that threshold. The photogate only should trigger when the light level just was LOW and now is HIGH. This is so it will only trigger once instead of multiple times in a row. You also can write your code to detect the HIGH to LOW transition by slightly tweaking the logic.
See the USEFUL FUNCTIONS at the bottom of this page for how to measure light level on the micro:bit.
Physical Setup:
There are 2 ways you can set up your photogates:
Start Photogate:
The start photogate should continually read the light level and look for a LOW to HIGH transition. When this occurs, it should send the message 'start' via the radio. You may also want to briefly display an image on the micro:bit's screen and sleep for a short time to provide visual feedback to the user and to prevent the photogate from triggering multiple times in rapid succession (something that is likely unintended in this application). The micro:bit should then loop back to the beginning of the code, so it can time another object.
End Photogate:
The end photogate should continually receive messages from the radio until it receives the message 'start'. Then, it should record the current time as the start time and continually read the light level until it sees a LOW to HIGH transition. When this occurs, it should calculate the elapsed time from the start time and scroll this across the display. The micro:bit should then loop back to the beginning of the code, so it can time another object.
The photogates should detect the transition from dimness to brightness. Dimness (or LOW) is defined as being below a defined threshold and brightness (or HIGH) is above that threshold. The photogate only should trigger when the light level just was LOW and now is HIGH. This is so it will only trigger once instead of multiple times in a row. You also can write your code to detect the HIGH to LOW transition by slightly tweaking the logic.
See the USEFUL FUNCTIONS at the bottom of this page for how to measure light level on the micro:bit.
Physical Setup:
There are 2 ways you can set up your photogates:
- Shine flashlights at micro:bits and use object you are timing to block the light
- Mount a small light to object you are timing to illuminate micro:bits (It may be difficult to find a bright enough LED)
Start Photogate:
The start photogate should continually read the light level and look for a LOW to HIGH transition. When this occurs, it should send the message 'start' via the radio. You may also want to briefly display an image on the micro:bit's screen and sleep for a short time to provide visual feedback to the user and to prevent the photogate from triggering multiple times in rapid succession (something that is likely unintended in this application). The micro:bit should then loop back to the beginning of the code, so it can time another object.
End Photogate:
The end photogate should continually receive messages from the radio until it receives the message 'start'. Then, it should record the current time as the start time and continually read the light level until it sees a LOW to HIGH transition. When this occurs, it should calculate the elapsed time from the start time and scroll this across the display. The micro:bit should then loop back to the beginning of the code, so it can time another object.
Start Photogate Code
End PhotoGate Code
Experiment:
Set up the 2 photogates vertically, so that an object can be dropped and travel between both photogates. Measure the distance between the photogates and use kinematics equations to calculate how long you expect an object in freefall that starts at rest at the top gate to take to reach the bottom gate. Drop an object from the top gate and compare your results.
Set up the 2 photogates vertically, so that an object can be dropped and travel between both photogates. Measure the distance between the photogates and use kinematics equations to calculate how long you expect an object in freefall that starts at rest at the top gate to take to reach the bottom gate. Drop an object from the top gate and compare your results.
References
The following functions and documentation may be helpful.
The full documentation may be found here:
The following functions and documentation may be helpful.
The full documentation may be found here:
Useful Functions
microbit.display.read_light_level()
Use the display’s LEDs in reverse-bias mode to sense the amount of light falling on the display. Returns an integer between 0 and 255 representing the light level, with larger meaning more light.
radio.on()
Turns the radio on. This needs to be explicitly called since the radio draws power and takes up memory that you may otherwise need.
radio.send(message)
Sends a message string. This is the equivalent of send_bytes(bytes(message, 'utf8')) but with b'\x01\x00\x01' prepended to the front (to make it compatible with other platforms that target the micro:bit).
radio.receive()
Works in exactly the same way as receive_bytes but returns whatever was sent.
Currently, it’s equivalent to str(receive_bytes(), 'utf8') but with a check that the the first three bytes are b'\x01\x00\x01' (to make it compatible with other platforms that may target the micro:bit). It strips the prepended bytes before converting to a string.
A ValueError exception is raised if conversion to string fails.
microbit.running_time()
Return the number of milliseconds since the board was switched on or restarted.
Use the display’s LEDs in reverse-bias mode to sense the amount of light falling on the display. Returns an integer between 0 and 255 representing the light level, with larger meaning more light.
radio.on()
Turns the radio on. This needs to be explicitly called since the radio draws power and takes up memory that you may otherwise need.
radio.send(message)
Sends a message string. This is the equivalent of send_bytes(bytes(message, 'utf8')) but with b'\x01\x00\x01' prepended to the front (to make it compatible with other platforms that target the micro:bit).
radio.receive()
Works in exactly the same way as receive_bytes but returns whatever was sent.
Currently, it’s equivalent to str(receive_bytes(), 'utf8') but with a check that the the first three bytes are b'\x01\x00\x01' (to make it compatible with other platforms that may target the micro:bit). It strips the prepended bytes before converting to a string.
A ValueError exception is raised if conversion to string fails.
microbit.running_time()
Return the number of milliseconds since the board was switched on or restarted.