Introduction: Arduino Traffic Light Simulator

A child friendly language to teach children programming is Scratch. Scratch is a visual programming language developed by the MIT Media Lab Lifelong Kindergarten Group. It can be used to program games and animations by dragging and dropping instructions. Based on Scratch 2.0, there are modifications available like s4a and mBlock which interface with hardware boards such as the Arduino. Also for these extensions there are numerous button and led examples which show how to read the status of a button and make a led blink. This is a nice start for children to teach code. But after having pressed the button a few times to see the led react, it is hard to find a new challenge in this setup and keep the interest of children.

So i tried to build an module based on the button and led example which is more interactive and give the junior programmer more challenges. A real live version of the button and led example can be found in a traffic light system. So i tried to build a complete programming kit which simulates a traffic light system. The traffic light simulator can be used to with existing toy cars after adding a very small magnet glued to the bottom of the car. For the design transparent acrylic sheets are used to make all parts of the simulator visible, providing children a better understanding about the whole. Interested in building a traffic light simulator yourself ? Here are the steps how to do it.

Step 1: Parts and Tools

Parts List

First you have to gather the parts. Most of the parts can be easily acquired, i usually buy my parts at AliExpress or eBay.

2 Green stray hat 5mm leds (AliExpress)
2 Green Led 10mm (Opitec)
2 Yellow Led 10mm (Opitec)
2 Red Led 10mm (Opitec)

6 resistor 100 Ohm (Opitec)
2 resistor 10k (AliExpress)
2 resistor 2k2 (AliExpress)

2 14mm reed switches (AliExpress)
8 2.54mm Dupont male pins (eBay)
8 2.54mm Dupont female pins (eBay)
4 2.54mm 4p Pitch Dupont Cable Housing (eBay)
14 2.54mm 1P Dupont Jumper Cable Housing & male pins (eBay)

Brawa 0,08qmm wire black (3168)/green (3163)/yellow (3161)/red (3162)/purple (3160)
1 mini Breadboard 35*47*8.5mm (AliExpress)
9 M3 x 12mm nylon screws and (AliExpress)
9 M3 x 10mm Hexagon nylon columns

1 arduino nano (AliExpress)
2 10*2mm n35 neodymium magnets (AliExpress)


1 Sheet Acrylic (1200mmx600mmx3mm) fluorescent orange.

Tools
Solder station
Dupont plier (eBay)
Lasercutting service (snijlab.nl)

Step 2: Laser Cutting the Acrylic Sheets

The road design is made of a three layered set of laser cut 3mm acrylic sheets. From each sheet the recesses for mounting the traffic lights and the screw holes have been cut. Out of the middle sheet recesses have been made for the wiring and components. On the top sheet street signs and markings have been engraved. The traffic lights are made of two laser cut parts which can combined using a half-lap joint.

The design files can be download from the reddipped github repository.

Step 3: Constructing the Traffic Lights

Mount the leds and bend the wires using a small plier in such a way the anode of the led can be soldered to the resistors without making a short circuit. Solder the cathode of the three leds together.

Place the resistors on the front side and lead the wires to the back.

Solder a small black wire on the soldered kathode led wires and crimp the Dupont male pins to the black wire and the free ends of the resistors.

Crimping the Dupont pins may be a little difficult, so i suggest to try it out on piece of loose wire first. The following video was a great help for me to learn making a good crimp connection using a Dupont plier.

Step 4: Wiring Up (1 of 2)

The wiring of arduino is fairly simple and shown in the schema below. The components for the traffic light and sensor at S First St are wired to D2-D5, the pins D5-D9 are wired to the traffic light and the sensor at E Second St.

To start wiring, put the top acrylic sheet upside down and place the middle sheet with the wiring recesses on top of it. Place some of the screws loose and tighten them loosely to keep the sheets in place while wiring up. Continue from here to place the wires.


A reed switch placed in the small recess in the middle of the right lane is used as a detector. A led on the right side of the road which is connected in series with the reed switch will act as an indicator. When a car is waiting at the detectors position the led will light up and a high signal will be sent to D5/D9. Although the Arduino has an internal pull-down resistor, an external 10K resistor is used as a pull-down resistor. This was necessary because in mBlock, which will be used to program the Arduino, it is not possible to activate the Arduino's internal pull-down resistor.

Now solder the reed switch, two resistors and the led together and attach the three wires connecting it to ground, D5 or D9 and 5v of the Arduino. It is a snug fit an will take some time to get it right. On the image below the position of the components are shown when seen from the top. Pull the end of the wires through the recess at the Arduino's position.

Step 5: Wiring Up (2 of 2)

When the two sensors and indicator led are in position, place for each traffic light 4 wires from the rectangular recess at the street corners to the recess at the Arduino's position. Remove the columns from the screws and place the third sheets carefully on top. I had to to this several times before i got all wires in the recess and not having them stuck somewhere between two sheets. When done place and tighten the remaining columns.

Now finish the wiring by crimping female Dupont connectors on the wires at the end of the traffic light and male Dupont wires on the side of the Arduino. Place the Dupont cable housings and glue the mini breadboard to the top sheet and place the Arduino. Make sure you place the Arduino asymmetrical in the breadboard. You must be have four free rows on the side of the 5v and gnd pins.

To be able to position the traffic lights correctly the Dupont 4p connectors connecting the traffic light with the Arduino must be guided into to rectangular recess. Do this by slightly pulling the wires on the side of the Arduino and slightly pushing the Dupont connector into the recess.

Step 6: Assembly

Place the traffic lights carefully into the mounting hole and connector.

And finish up by placing the connectors into the breadboard.

Step 7: Preparing the Cars

To trigger the reed switches which are recessed in the road a small neodium magnet has to be glued to the bottom of the cars. To position the magnets, put the car on the opposite direction of the road evenly with the stop line. Now move the magnet approximately 1cm above the road in the direction of the traffic light until the green led switches on. Now the magnet is at the position of the car where you should glue it on the bottom of the car.

On my first attempt i tried to melt the magnet into the plastic bottom of the car by placing the magnet on the correct position and heating it with a soldering iron. This seemed to be a good idea, but totally destroyed the magnetic field of the magnet.

Step 8: Installing Software and Drivers

mBlock

I chose mBlock to program the Arduino with Scratch. There are more tools like S4A, to program the Arduino. But i preferred mBlock because the development program seems to be more active than S4A. It also can generate Arduino code and upload it to the Arduino so it can run independently. When using S4A you always need to upload the S4A firmware to the Arduino and run the program from your computer. You can download mBlock here for mac or windows.

Arduino CH340/341 interface

If you have an Arduino with an CH340 or CH341 interface, you need the correct driver to be able to connect the Arduino with your Mac or pc. On OS X drivers nowadays must be signed, yes you can disable kext signing, but makes you Mac less secure. Although there are free signed drivers available like here at Björn's Techblog, it did not work for me and therefore spent some dollars on a commercial CH340/CH341 USB-Serial driver from Repleo. When connecting from mBlock to the USB Serial port, there is a chance that you are able to select the port, but fails to connect. You can detect if a check mark is not shown after selecting the serial port. This is caused by mBlock which seems to have an issue with handling specific port names, it could be length of the name or specific characters in it. This can be easily resolved by making a new link with a short name to the longer driver name.

sudo -s ln /dev/tty.Repleo-CH341-0000124 /dev/tty.ch341

Now you are able to connect to the driver using the link.

When connected change make sure the correct board is selected and extensions are enabled

Click on 'Extensions' option on the menu bar, then select the Arduino.Click on 'Boards' option on the menu bar, then select the Arduino Nano ( mega328 )

Now upload the mBlock firmware by selecting the 'Connect' option on the menu bar, then select Upgrade Firmware. Now you are ready to go !

Step 9: The Cookbook

To keep the cookbook open for modifications, extensions or translation i wrote it in Markdown format. I wrote it in Dutch because i will this kit at Dutch schools and Coding Dojo's. Feel free to make a pull request for translations! For non github users, the Cookbook can be accessed in html and md format usink the links below.

Traffic light cookbook html

Traffic light cookbook markdown

The accompanying Scratch files are available here or download all files from my github repository as a zip.