https://www.microbit.co.uk/blocks/lessons/hack-your-headphones/activity
About this project
In this project, students learn how to program the light sensor on the BBC micro:bit, telling the device to react to different intensities of light. By attaching the micro:bit to a speaker or a pair of headphones, students can also tell the device to play different sounds when exposed to different light intensities.
What you'll need
1 x BBC micro:bit
1 x Micro USB
1 x Computer or tablet
2 x AAA Batteries & Battery Holder
1 x mini speaker or set of general purpose headphones
2 x Crocodile clips
Project walkthrough
- Go to the BBC micro:bit website, click 'create code' and begin a new Microsoft Block Editor project.
- Create a new variable called 'light' and set it to the input 'light level'
- In the first instance, we're going to write a code that makes the LED array increase or decrease in brightness in relation to the intensity of light the sensor is exposed to.
- Attach an if/do tile and create 4 x else/if and 1 x else options by clicking the blue cog icon in the tile.
- Each of these options will be used to tell the LED array to display a different brightness depending on the intensity of the light.
- To tell the device how to do this, connect a [0] < [0] block next to 'if' and place a 'light' variable in the left-hand dialog box. In the first instance, set the right dialog box to 25, so that the code reads if [light] < [25].
- For 'do' attach a 'set brightness' tile from the LED commands menu, and a 'show leds' tile from the Basic commands menu. Set the brightness to 25, and check all of the individual LED boxes on the 'show leds' tile, telling the device to light up at a very dim illumination when the detected light level is lower than 25.
- Duplicate all of these blocks for the four else/if options, and set the light levels to 50, 100, 150 and 180 respectively.
- Set the brightness levels to 50, 100, 150 and 200 respectively.
- Under the 'else' option, attach another 'set brightness' tile and set it to 250, telling the device to display the highest brightness when it detects any light intensity over 180.
- Finally, wrap your code in an 'on Button A pressed' tile to create a function that switches on the light sensor.
- You now have a basic code that can should tell your BBC micro:bit to get brighter or darker depending on the light intensity. You can compile this right away and test it out on your device.
- However, if you want to make things a little more interesting, you can also expand the code so that your device also emits a different note depending on the light intensity detected by the sensor.
- To do this, simply add a 'ring tone (hz) [c]' tile for every brightness level, and set each to a different ascending note. (i.e. C, D. E. F. G, A)
- Compile your code and transfer it to your BBC micro:bit device. To take advantage of the musical element, you'll need to attach the device to a general purpose mini speaker or a set of headphones. Take two separate pairs of crocodile clips, attach the end of one to the 0 pin on your BBC micro:bit, and the end of the other to the GND pin.
- Attach the other ends to the positive and negative inputs for your speaker, or attach the other end of the GND crocodile clip to the base of your headphone jack, and the other end of the 0 pin crocodile clip to the tip. You should now be able to hear the notes being played on the BBC micro:bit through your audio device. Try it out by moving your BBC micro:bit into lighter and darker areas.
Once your pupils have mastered the light box challenge, encourage them to see what else they can do to make the BBC micro:bit respond to its surroundings. They might want to experiment with other input settings such as temperature and acceleration. Let us know how they get on in the comments section below.
Top Comments