17. IoT Case 08: Automated Traffic Light

Level: level

* For more details, please refer to “Chapter 5: Object to Object communication” auto_fit

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.

auto_fit

17.1.3. Part List


auto_fit

17.1.4. Assembly step


N/A

17.1.5. Hardware connect


Connect the Light Sensor to P0 port of IoT:bit

auto_fit

17.1.6. Programming (MakeCode)


Step 1. Initialize OLED and IoT:bit and connect to WiFi

  • Snap Initialize OLED with width:128, height: 64 to on start

  • Snap Initialize IoT:bit TX P16 RX P8 from IoT:bit to on start

  • Snap Set Wi-Fi to ssid pwd from IoT:bit

  • Enter your Wi-Fi name and password. Here we set smarthon as SSID and 12345678 as password

  • Set variable light2 to 0 from variables auto_fit

Step 2. Show icon “tick” after WiFi connection

  • Snap show icon from basic to On WiFi connected and select icon tick pic_60

Step 3. Check traffic status

  • Snap if statement to block forever

  • If WiFi is connected,

  • Set variable2 to light value from light sensor at P0

  • Snap clear OLED display from OLED to avoid overlap

  • Snap show string and show value of variables light2

  • Pause 6000 (ms) to another checking auto_fit

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… from IoT:bit > channel

  • Set channel to tsuenwan, message trafficjam

  • Set else-condition (traffic jam is not detected)

  • Snap WiFi Sender send channel… message… from IoT:bit > channel

  • Set channel to tsuenwan, message nojam auto_fit

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).

auto_fit

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).

auto_fit

17.1.8. Think


Q1. How can we use distance sensor to detect traffic status?

17.2. Receiver


17.2.1. Goal


Make an auto-traffic LED Module by receiving signal from sender micro:bit.

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.

auto_fit

17.2.3. Part List


auto_fit

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)

auto_fit

Step 2

Assembly completed!

auto_fit

17.2.5. Hardware connect


Connect the Traffic LED Module to P0 port of IoT:bit

auto_fit

17.2.6. Programming (MakeCode)


Step 1. Initialize OLED and IoT:bit and connect to WiFi

  • Snap Initialize OLED with width:128, height: 64 to on start

  • Snap Initialize IoT:bit TX P16 RX P8 from IoT:bit to on start

  • Snap Set Wi-Fi to ssid pwd from IoT:bit

  • Enter your Wi-Fi name and password. Here we set smarthon as SSID and 12345678 as password

  • Set variable oldmsg to “” from variables auto_fit

Step 2. Join Channel “tsuenwan”

  • Snap show icon from basic to On WiFi connected and select icon tick

  • Snap WiFi Receiver join channel from IoT > Chanel

  • Set channel name tsuenwan auto_fit

Step 3. Receive WiFi message

  • Snap On WiFi Receiver received

  • Go to OLED

  • Snap the clear OLED display to On WiFi received to avoid overlap

  • Snap the show string

  • Draw variable receivedMessage to show string placeholder pic_70

Step 4. Set up a new function (TurnRed)

  • Control traffic light at P1 green on from SmartCity > Output

  • Pause for 2000ms from basic

  • Control traffic light at P1 yellow on, pause for 2000ms

  • Control traffic light at P1 red on and pause for 2000ms. auto_fit

Step 5. Set up a new function (TurnGreen)

  • Control traffic light at P1 red on from SmartCity > Output

  • Pause for 2000ms from basic

  • Control traffic light at P1 red and yellow on, pause for 2000ms

  • Control traffic light at P1 green on and pause for 2000ms. auto_fit

Step 6. Set traffic light default status

  • Insert function TurnGreen to on start pic_60

Step 7. Change traffic light status

  • Snap if-statement, set received Messageoldmsg to if-condition

  • Set oldmsg = receivedMessage (renew the latest traffic status)

  • Snap another if-statement

  • Set receivedMessage = trafficjam to if-condition

  • Call function TurnRed

  • Set receivedMessage = nojam to else-if-condition

  • Call function TurnGreen auto_fit

Full Solution

MakeCode: https://makecode.microbit.org/_V5jcy5Pvm7hF

You could also download the program from the following website:

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.

auto_fit

17.2.8. Think


Q1. How can we add sound effect to the traffic LED Module according to the corresponding color?