17. IoT Case 08: Automated Traffic Light¶
Level:
* For more details, please refer to “Chapter 5: Object to Object communication”
17.1. Sender¶
17.1.1. Goal¶
Make a sender to send signal to another micro:bit to show if there is traffic jam or not.
17.1.2. Background¶
How to send signal to another micro:bit?
Micro:bit (sender and receiver) are connected to the same channel so wifi message can be sent between the micro:bit. Such that when the sender micro:bit sends a Wi-Fi message “trafficjam” to receiver micro:bit. When receiver micro:bit receives a WiFi message “trafficjam”, the connected traffic light will turn red.
Sender micro:bit operation
When the light value detected is too low, this would represent there is a traffic jam and and a ‘trafficjam’ wifi message to another microbit. When the light value detected is high, this would represent there is no traffic jam and send a “nojam” wifi message to another micro:bit.
17.1.5. Hardware connect¶
Connect the Light Sensor to P0 port of IoT:bit
Pull the buzzer switch ‘up’ to disconnect the buzzer in this execrise
17.1.6. Programming (MakeCode)¶
Step 1. Initialize OLED and IoT:bit and connect to WiFi
Snap
Initialize OLED with width:128, height: 64
toon start
Snap
Initialize IoT:bit TX P16 RX P8
fromIoT:bit
toon start
Snap
Set Wi-Fi to ssid pwd
fromIoT:bit
Enter your Wi-Fi name and password. Here we set
smarthon
asSSID
and12345678
aspassword
Set variable
light2
to 0 fromvariables
Step 2. Show icon “tick” after WiFi connection
Snap
show icon
frombasic
toOn WiFi connected
and select icontick
Step 3. Check traffic status
Snap
if statement
to blockforever
If
WiFi is connected
,Set
variable2
tolight value from light sensor at P0
Snap
clear OLED display
fromOLED
to avoid overlapSnap
show string
and show value of variableslight2
Pause
6000 (ms) to another checking
Step 4. Send notification when someone pass by
Snap
if statement
Set
light
<10
in to if-condition (traffic jam is detected)Snap
WiFi Sender send channel… message…
fromIoT:bit
>channel
Set channel to
tsuenwan
, messagetrafficjam
Set
else-condition
(traffic jam is not detected)Snap
WiFi Sender send channel… message…
fromIoT:bit
>channel
Set channel to
tsuenwan
, messagenojam
Full Solution
MakeCode: https://makecode.microbit.org/_P5fAujhip5Lh You could also download the program from the following website:
17.1.7. Result¶
Light sensor is used to detect if there are traffic jam. Once the light intensity is not low, it indicates that there is a traffic jam on the road. Wi-Fi message “nojam” will be sent to another micro:bit (receiver).
Once the light intensity is too low, it indicates that there is a traffic jam on the road. Wi-Fi message “trafficjam” will be sent to another micro:bit (receiver).
17.2. Receiver¶
17.2.2. Background¶
How to receive signal from another micro:bit?
Micro:bit (sender and receiver) are connected to the same channel so wifi message can be sent between the micro:bit. If receiver micro:bit receives a WiFi message “trafficjam” from sender, the connected traffic light will turn red.
Receiver micro:bit operation
When a wifi message “trafficjam” is received, it means there is traffic jam forward. The traffic LED Module will turn red. When a wifi message “nojam” is received, it means there is no traffic jam forward. The traffic LED Module will turn green. By using smart traffic light, the problem of traffic jam can be reduced as automatic traffic control is used.
17.2.4. Assembly step¶
Step 1
Attach the Traffic light Module to G1 model using M4 screw and nut. Put together all the cardboard parts (G1-G2)
Step 2
Assembly completed!
17.2.6. Programming (MakeCode)¶
Step 1. Initialize OLED and IoT:bit and connect to WiFi
Snap
Initialize OLED with width:128, height: 64
toon start
Snap
Initialize IoT:bit TX P16 RX P8
fromIoT:bit
toon start
Snap
Set Wi-Fi to ssid pwd
fromIoT:bit
Enter your Wi-Fi name and password. Here we set
smarthon
asSSID
and12345678
aspassword
Set variable
oldmsg
to “” fromvariables
Step 2. Join Channel “tsuenwan”
Snap
show icon
frombasic
toOn WiFi connected
and select icontick
Snap
WiFi Receiver join channel
fromIoT
>Chanel
Set channel name
tsuenwan
Step 3. Receive WiFi message
Snap
On WiFi Receiver received
Go to OLED
Snap the
clear OLED display
toOn WiFi received
to avoid overlapSnap the
show string
Draw variable
receivedMessage
to show string placeholder
Step 4. Set up a new function (TurnRed)
Control traffic light at P1
green on fromSmartCity
>Output
Pause
for 2000ms from basicControl traffic light at P1
yellow on,pause
for 2000msControl traffic light at P1
red on andpause
for 2000ms.
Step 5. Set up a new function (TurnGreen)
Control traffic light at P1
red on fromSmartCity
>Output
Pause
for 2000ms from basicControl traffic light at P1
red and yellow on,pause
for 2000msControl traffic light at P1
green on andpause
for 2000ms.
Step 6. Set traffic light default status
Insert function
TurnGreen
toon start
Step 7. Change traffic light status
Snap
if-statement
, setreceived Message
≠oldmsg
toif-condition
Set
oldmsg
=receivedMessage
(renew the latest traffic status)Snap another
if-statement
Set
receivedMessage
=trafficjam
toif-condition
Call function
TurnRed
Set
receivedMessage
=nojam
toelse-if-condition
Call function
TurnGreen
Full Solution
MakeCode: https://makecode.microbit.org/_V5jcy5Pvm7hF
17.2.7. Result¶
By receiving wifi message, traffic LED Module will turn to the corresponding colour automatically.
If there are no traffic jam ahead (detected by light sensor). Micro:bit (sender) will send wifi message “nojam” to the micro:bit (receiver). The traffic light will turn green.
If there are traffic jam ahead (detected by light sensor). Micro:bit (sender) will send wifi message “nojam” to the micro:bit (receiver). The traffic light will turn red.
17.2.8. Think¶
Q1. How can we add sound effect to the traffic LED Module according to the corresponding color?