4.2 Street Light
-
In this lesson, we will create our own version of a smart streetlight. It will work like a normal streetlight (turning on when the light sensor detects that it’s dark and off when the light sensor detects enough light). However, by the time we’re done, it'll do a lot more than that!
We can add a crash detection system to our streetlight which changes the colour of the light if the light has been hit by a car, to help repair crews find it quickly. We can also give our streetlight a fun new party mode which can be used to change the colour of the light to help celebrate during special occasions.For this lesson, we will build on our work each time we start a new challenge. You can save yourself some time by making sure you save your work whenever you complete a challenge and using that code as a starting point for the next one.
-
The Challenge
- When the light level is low, the streetlight will turn on.
- When the light level is high, the streetlight will turn off.
Required Devices
- 1 micro:bit
- 1 sensor:bit
- 1 analog light sensor
- 1 digital RGB LED
Suggested Blocks
- on start
- forever
- if true then else
- 0 > 0
- 0 < 0
- 0 = 0
- set [variable]
- [variable]
- set strip to neopixel at pin with leds as RGB
- strip show color
- analog read pin
Hints
- Make sure you save your work at the end of this challenge and give your project a descriptive name so you can find it easily later. You can use the code you write for this challenge as the starting point for the next one.
-
Crafting Supplies
- 1 metal wire coat-hanger
- 1 pliers
- 1 sticky tape / Blu-Tack
- 1 piece of cardboard
- 1 scissors
Instructions
Our goal with this activity is to create a sturdy and flexible streetlight post, that can support the weight of our micro:bit, and have a way of fastening the RGB LED and the Analog Light Sensor to the post in the appropriate spots.
*The Teacher may elect to complete Step 1 and 2 themselves, in preparation for this activity. In this case, begin the activity from Step 3.
- Untwist the metal wire coat-hanger and use the pliers to cut the coat-hanger into 2 or 3 lengths of straight wire.
- Using 1 wire segment of the coat hanger and the pliers, twist one end of the wire to form a base that will hold the street light upright. Bend the opposite end of the wire into an arch to resemble the shape of a street light.
- Cut out a circular portion of cardboard and stick this to the base with the sticky tape / Blu-tack. This is mainly used to reinforce the base and help give it some added ‘grounding’ and support.
- Cut out 2 small sections of cardboard and stick to the opposite end of the street light using the sticky tape.
- Use the sticky tape / Blu-Tack to stick the RGB LED to the top portion of the street light, facing the floor. Stick the Analog light sensor to the top portion of the street light, facing the roof.
- Make sure that all sensors and RGB LEDs are connected to the micro:bit correctly and if needed, stick the micro:bit and cables to the light post.
Plug in the micro:bit to a power-source (either a laptop or battery pack).
*Make sure that you have downloaded your code to the micro:bit. If the code is downloaded, check that your traffic light shows the correct colours in the correct places!
-
The Challenge
From the previous challenge:
- When the light level is low, the streetlight will turn on.
- When the light level is high, the streetlight will turn off.
For this challenge:
- When the accelerometer detects a shake, switch the streetlight colour to red.
- The light remains red until the B button is pressed.
- When you press the B button, return to normal behaviour.
Required Devices
- 1 micro:bit
- 1 sensor:bit
- 1 analog light sensor
- 1 digital RGB LED
Suggested Blocks
- on start
- forever
- on button pressed
- on shake
- if true then else
- true
- false
- 0 > 0
- 0 < 0
- 0 = 0
- set [variable] to
- [variable]
- set strip to neopixel at pin with leds as RGB
- strip show color
- analog read pin
Hints
- Make sure you save your work at the end of this challenge and give your project a descriptive name so you can find it easily later. You can use the code you write for this challenge as the starting point for the next one.
- You will need to think about how you prevent your code which automatically turns the light on and off from turning off or changing the colour of the red crash detector LED. Think about how you might be able to create a normal mode (for when a crash hasn’t been detected) and a crash detected mode (which is triggered by detecting a shake and turned off again by pressing the B button).
-
The challenge
From the previous challenges:
- When the light level is low, the streetlight will turn on.
- When the light level is high, the streetlight will turn off.
- When the accelerometer detects a shake, switch the streetlight colour to red.
- The light remains red until the B button is pressed.
- When you press the B button, return to normal behaviour.
For this challenge:
- When you press the A Button your streetlight enters party mode.
- In party mode, your streetlight flashes between two or more colours to celebrate a special occasion or event.
- You can use whatever colours you like for this challenge.
Required Devices
- 1 microbit
- 1 sensor:bit
- 1 analog light sensor
- 1 digital RGB LED
Suggested Blocks
- on start
- forever
- on button pressed
- on shake
- if true then else
- true
- false
- 0 > 0
- 0 < 0
- 0 = 0
- set [variable] to
- [variable]
- set strip to neopixel at pin with leds as RGB
- strip show color
- analog read pin
Hints
- Remember, you will still need to be able to resume normal operation when the B button is pressed, and now that will need to disable crash mode and party mode.
- Think about how to manage all of these different modes. You’ll also need to make a decision about how you want to manage situations like a crash occurring while party mode is active: how do you want your code to behave in that situation?
- If you’re having trouble deciding on what colours you’d like to use for party mode, think about using the colours of your school or your favourite sports team, or red and green which are good colours to use around Christmas.
Bonus Challenges
- In this lesson we’ve used 1 RGB LED, but we can connect multiple RGB LEDs to 1 micro:bit. Try adapting your project so that you have 3 streetlights instead of 1. Use 1 micro:bit and 1 light sensor, but set things up so that 3 lights react to the inputs, rather than just 1.
- If you want to take this lesson even further, think about how you might build a network of streetlights that includes multiple micro:bits and uses the radio to communicate between them. Can you build a network where one light sensor and crash sensor can control street lights attached to multiple micro:bits simultaneously?
-
In this lesson we've built our very own smart streetlight, with three different modes and several different functions.
In the next lesson, we'll learn how to program a traffic light!