4 Way Traffic Light System Using 5 Arduinos and 5 NRF24L01 Wireless Modules
by DonX-Developer in Circuits > Arduino
5662 Views, 23 Favorites, 0 Comments
4 Way Traffic Light System Using 5 Arduinos and 5 NRF24L01 Wireless Modules
A little while ago I created an Instructable detailing a single pair of traffic lights on a breadboard.
I also created another Instructable showing the basic framework for using an NRF24L01 wireless module.
This got me thinking!
There are quite lot of hobbyists around the globe who build model towns and railways, and almost always have traffic lights of some description.
Some are working models, and others are for aesthetic purposes only.
Could I create a working model of a four way traffic light system and connect them wirelessly?
I sat down and thought about my list of possible requirements. Which went a little bit like this.
Control 4 directions of traffic, like a crossroads junction.
Each direction having two lights; and each pair getting their instructions wirelessly from some sort of control unit.
Be able to define and modify the operating sequence of the lights,
- 1, 2, 3, 4 - clockwise
- 1, 3, 4, 2
- 1, 4, 2, 3
- 1, 4, 3, 2 – anti-clockwise
- 1, 2, 4, 3
- 1, 3, 2, 4
- 1 + 3, 2 + 4 – 2 on 2 off
- 1 + 3, 2, 4
- 1, 3, 2 + 4
All sequencing to be controlled by a single control unit, and the receiving units solely turning the lights on and off.
When I said make a model, I meant, make a real model, nothing too fancy, but something that would actually look something like the real thing, possibly, maybe ish.
Ambitious? Maybe!
Main parts requirements:
A control unit and four sets of lights = five Arduinos and five wireless modules. AliExpress to the rescue (again).
Eight traffic light stands. I have a poor imitation of a 3D printer, which tends to supply more bin fodder than usable products, but I thought that I would give it a go anyway. I found some on Thingiverse,
https://www.thingiverse.com/thing:2157324
This model looked like the least complex for my printer. I wanted eight, so I was still pushing my luck. As it turned out, I found that after a couple of failed attempts, if I orientated the model in a certain direction (from front to back), I got reasonable results. In total I printed thirteen, and got eight useable ones.
That was the major parts list sorted. The remaining parts, I already had.
The complete parts list are
- 5 x Arduino UNOs
- 5 x NRF24L01 wireless boards
- 5 x YL-105 (or similar) breakout boards for the NRF24L0s
- 8 x Red LEDs
- 8 x Yellow LEDs (I do not have any orange LEDs)
- 8 x Green LEDs
- 4 x RGB LEDs
- 28 x 220 Ohm resistors
- Breadboards / PCBs??
- 8 x Model traffic Lights
- 6 x 8 long pin headers (the sixth one was for spacing on the control board, see video)
- Shrink tube
- Jumper wires
- Piece of hardboard or something flat
- Other pieces of wood ??
- Paint ??
- Hot glue
- Time, Patience and alcohol of choice
Writing the Code for the Control Unit
This is the bit that I had to do first, just in case I couldn’t actually manage it, which would have been a showstopper.
This was by far the most complex part or the project, but also the most interesting for me.
I had to sit down and define all of the possible combinations of light changes and how they would work together synchronously.
Like all good design, it started, on paper, with a very long list of numbers, and because I wanted to be able to have multiple possible operating sequences, the list got even longer.
But, once I was happy that I had everything that I thought was required and, after staring at the pages of numbers for a while, my OCD kicked in and I started to see patterns.
Organising the patterns, I managed to collate all of the sequencing down into a single 3-dimensional array and two 2-dimensional arrays.
All I had to do now is find a way to manipulate those arrays into creating the correct sequencing and light steps.
It took a while, but I managed to achieve it in less than fifty lines of code, including comments etc.
The code for this is not for the faint hearted, but if you understand multi-dimensional arrays, it should not be too difficult to follow. Or a learning curve for the rest.
The point is, that I believe it works, and should not require changing anyway. But…………
NRF24L01 Breakout Board Mod
The NRF24L01 module and the YL-105 breakout board, unfortunately, are not very breadboard friendly.
The breakout board goes partway to fixing the problem and more importantly, making it 5v tolerant, but, it is still not breadboard friendly.
So I got a little inventive.
In my collection of ‘stuff’, I have a number of 6 pin headers with long pins. The sort that are required for making Arduino Shields.
I took one of these and bent the pins at 90 degrees.
I removed one of the power rails from a breadboard, and plugged the header into the edge of the breadboard.
That left the power pins on the breakout board. They are now in the way.
So I removed them and placed them on the other side of the breakout board so that they are now protruding from the rear of the board.
For the purposes of this Instructable, I require five NRF24L01 modules, so I mounted them all along the breadboard and then fixed the power rail along all of the power pins on the breakout board.
It looked quite tidy until I connected the Arduinos and it got a bit crowded.
Plus, which is the important bit, once the power rail was connected, all Arduinos would be connected to the same source and that was what I was trying to avoid, so I took most of it apart again.
I will keep the board with a couple of NRF24L01 modules on it for prototyping in the future, so not a complete waste of time.
Traffic Light Units
I found some small 170 tie point breadboards. These do not have a power rail so my modified breakout board would still fit. Albeit on a slight angle because of the height of the breakout board.
I built the four traffic light controls the same, same colour wires, positioning etc. They are now truly standalone.
For the control unit, I put the NRF24L01 module on a PCB with the RGB LEDs. I used RGB because, although I didn’t need to see all lights, just the red and green, they take up less space.
Connected the LEDs to the Arduino in the normal way and added a bit of code to display the red or green status of each set of traffic lights.
I tried to be consistent with my wiring colours so that I could easily see if I had done something different on one of the boards.
I have some short Dupont lead sets, and as the leads are stuck together, it made this part quite easy.
NRF24L01:
- CE Orange To Arduino pin 10 (defined in the code)
- CSN Yellow To Arduino pin 9 (defined in the code)
- SCK Green To Arduino pin 13 (mandatory)
- MOSI Blue To Arduino pin 11 (mandatory)
- MISO Purple To Arduino pin 12 (mandatory)
- Vcc Red To 5v. If you are not using the breakout boards this must be 3.3v.
- GND Brown To Arduino GND
Light units and Arduino pins to LEDs:
- Red for red LED
- Orange for the Yellow LED (I do not have orange LEDs)
- Green for the green LED
- Black for GND
My only deviation from this was when I connected the Control Arduino to the RGB LEDs. I used white and grey wires because I had run out of red ones.
Traffic Lights and Testing
That’s the code completed and each standalone control also completed. All I need now is the traffic lights themselves.
As I said before, I found an uncomplicated model on Thingiverse and managed to print eight that didn’t look too bad.
I fitted the LEDS with their required 200 Ohm resistor and a link and ground wire.
Shrink tubed the leads, and hot glued it all in place.
I decided to paint them black after all of the LEDs were fitted. Bad idea, I should have done that first.
I wired everything up for a test before progressing any further.
The Crossroads
I decided to mount them all on a board, so now I had to create some kind of crossroads lookalike.
I live in the UK so we drive on the wrong side of the road here, and therefore I made my crossroads as UK friendly as my poor artistic skills would allow.
This was quite straightforward, just time consuming; and I’m sure that there are no crossroads that actually look like that, but mine have no potholes.
I didn’t want to permanently sacrifice my Arduinos to this project, so I compromised by populating each one with 10mm standoffs and hot glued the standoffs to the base of the board.
What I did do though is hot glue the mini breadboard to the side of the Arduino.
Firstly, it held the NRF24L01 and breakout board off the base of the crossroads,
and secondly, I rarely use an Arduino without a breadboard of some sort anyway, so they will still be useful like that.
All Done
All code files have been included.
I did not go through the code here as this Instructable is long enough without it.
I hope that this has been a useful Instructable, even if it only shows how to control a number of other Arduino boards wirelessly with the very reasonably priced NRF24L01.
If you have any queries, please do not hesitate to make a comment and I’ll do my best to help.