Traffic light intersection simulation project

I'm planning to build an accurate simulation of a traffic light controlled intersection near my house.

  • North and south have red/amber/green lights (x3 each direction) + red/white pedestrian lights (x2 each direction).

  • East and west have red/amber/green lights (x2 each direction) + red/white pedestrian lights (x2 each direction).

  • Each corner has a pedestrian button (x1). I'm not sure if the button works for crossing both directions (depending on when it's pressed). Usually, there are two buttons on a pole — one for crossing in each direction. I'll double check but I assume the button works for crossing in both directions, depending on when you press it.

LED specs:

Forward Voltage: R/Y 2.0-2.2V; B/G/W 3.0-3.2V.
Max. Current: 20mA.

What spec power supply should I buy for the LEDs and is this MOSFET a good choice?

You can work it out for yourself.

What is the maximum number of LEDs that could possibly be on at the same time?

How much current is needed to make the LEDs bright enough? The maximum is 20mA but this may actually be too bright.

You may not need MOSFETs at all, but even if you do, those MOSFET modules in the image you posted would be overkill.

Not sure what use the other products shown in your image would be in this project.

First let me ask why have you chosen the R4 minima?

Do you know how to solder?

@zerovolts - welcome to the forum.

That's a great project, fun.

Do you have the software designed and coded yet?

Instead of starting by buying a bunch of hardware, you might consider developing the software to a point of some satisfaction and stability.

Are you following or learning from any of the 1,374 (at last count) Arduino traffic lights projects you can find on the internets?

You might like using a simulator, this one

works well and can be used for free, slightly better if you get a free account. It is also a good way to share progress and let the rest of us play along when necessary.

You might tell us about the last project you did, and what you think your abilities on that side of things is. This entire undertaking might be easy or hard, depending on your skills, here I mean programming.

Srsly, say how hard it would be for you to take a blank piece of paper and write a sketch that would count the number of times you press a button, and turn on the LED after 4 presses and turn it off after 3. No cutting and pasting.

And if you are asking about hardware bits, you might say how you've already made some huge-ish decisions about what you want to end up with.

HTH

a7

1 Like

I just built a similar project for teaching a local high school. It has just the 6 LEDs. I used a XIAO esp32-s3 for now but will switch it to a Pro Micro as we have lots. Power supply is either a wall wart or a 4 AA cell battery pack.
I have no idea how you would make use of a MOSFET.
The software is classic state machine, timing is done with classic millis().
My parts list is almost any MCU, LEDs with dropping resistors, wires and small PSU.

With the 8ma per pin limitation on the R4, you may be thinking you HAVE to have mosfets to drive even minimalist LEDs; however, that 20 mA per LED rating is their maximum current, and you will likely find that cutting that current to 1/3 will still provide reasonable brightness, but you’ll have to test that yourself.

Is there some reason you’re not doing this beginner’s experiment with an Uno or Nano? Higher output per pin, simple, cheap, not so painful when, as a beginner, you fry an output.

YMMV.

Number of illuminated LEDs will vary from 16 steady + 2 flashing, to 18 steady, so I guess the max current would be up to 20mA x 18 LEDs = 360mA.

Each traffic light always has only one bulb illuminated (never two at once. I'm in Canada, so they skip from red straight to green without the amber+red advance warning some countries like the UK have), while the four pedestrian signs always either have one bulb illuminated or flashing.

10 traffic lights = 10 bulbs always on at any given time.
8 pedestrian lights = 8 bulbs always on at any given time (or sometimes only 4 steady + 4 flashing).

I guess I could use a larger resistor to dim any that are too bright at 20mA. It's interesting that the red and yellow LEDs are rated for 2.0-2.2V, while green and white are rated for slightly more at 3.0-3.2V.

I'll try to find some smaller ones (once I know for sure I need MOSFETs for this project).

I don't have anything else yet, so I need a breadboard/jumper wires etc.

I thought the right tool for the job might be either an Arduino R3, R4 Minima or R4 WiFi, but maybe all of them would be overkill. I liked the idea of an R4 because I heard they can handle more complex programs than the R3, so perhaps I could use it for something else in future. On the other hand, I also read the R3 can handle more current, so maybe going that route would mean not needing MOSFETs at all. (Total current for 18 LEDs should only be 360mA.)

What do you think would make the most sense? I suppose I could spread the load across multiple output pins, so instead of 6 red/6 yellow/6 green (which share the exact same timings) pulling 120mA from one pin, it could be spread across several (potentially avoiding the need for a MOSFET and additional power supply).

I recently bought my first soldering iron, flux and solder for another project but haven't used it yet. This will be my first time soldering anything.

Thanks. I think it will be interesting. I'll record a short video of them cycling through a couple of phases, which I can use to get the timing accurate. I'll eventually have to figure out how the pedestrian button affects the duration of the red light shown to traffic but that'll come later.

Not even started yet. I wanted to figure out what hardware I was going to be using first (and order it), so I'll know what's going to be actually running the code and controlling the LEDs. It'll be more fun if I can see the LEDs running different version of my code as I refine it.

I'll give that a try tonight. Sounds great! I really like the idea of bringing something to life by making it behave like the real-world system.

My final version will have one switch to turn on and off the traffic sign control system. and another for the streetlights. I envisage eventually making a mock-up of the intersection, including little buildings out of cardboard or something. Getting details like the speed of the flashing red light on the pedestrian signs right will give it a sense of realism.

So far I've just watched this one to get a general feel for it:

I don't have a programming background but was thinking of repurposing someone's existing code (tweaking the timings), until I gradually get closer and closer to what I want — at least to start with.

I don't know how to do that.

I was just throwing my initial ideas out there as a starting point. If the consensus is an Arduino is overkill for this project then I'm open to going in a different direction. I just thought it would be a good tool for the job (and admittedly I don't really have any ideas other than Raspberry Pi) and thought it would be a good way for me to get learning how to program Arduinos.

Soldering FYI

url=[https://nam04.safelinks.protection.outlook.com/?url=https%3A%2F%2Fm.youtube.com%2Fwatch%3Fv%3DeBIjsZ1dcT0&data=02%7C01%7C%7C062fd6a2b0b34c3683cd08d80e2efbcb%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637274942249679699&sdata=gOIakt8AnEyB6Hj8GEfZUkOKIOtcP552yMADEtvAKp4%3D&reserved=0]https://nam04.safelinks.protection.outlook.com/?url=https%3A%2F%2Fm.youtube.com%2Fwatch%3Fv%3DeBIjsZ1dcT0&data=02%7C01%7C%7C062fd6a2b0b34c3683cd08d80e2efbcb%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637274942249689695&sdata=O3hKuxShtiw9Yye%2BngOzbaMLhN0kMAohQdDKyc1%2Fp6U%3D&reserved=0 [/url]

url=[https://nam04.safelinks.protection.outlook.com/?url=https%3A%2F%2Fm.youtube.com%2Fwatch%3Fv%3D6PB0u8irn-4&data=02%7C01%7C%7C062fd6a2b0b34c3683cd08d80e2efbcb%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637274942249689695&sdata=Bt1BOrZNrdYrg19zvJgaEPcrpAQQq%2Bu7WWzkazSP0BU%3D&reserved=0]https://nam04.safelinks.protection.outlook.com/?url=https%3A%2F%2Fm.youtube.com%2Fwatch%3Fv%3D6PB0u8irn-4&data=02%7C01%7C%7C062fd6a2b0b34c3683cd08d80e2efbcb%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637274942249689695&sdata=Bt1BOrZNrdYrg19zvJgaEPcrpAQQq%2Bu7WWzkazSP0BU%3D&reserved=0 [/url]

url=[https://nam04.safelinks.protection.outlook.com/?url=https%3A%2F%2Fm.youtube.com%2Fwatch%3Fv%3D5uiroWBkdFY&data=02%7C01%7C%7C062fd6a2b0b34c3683cd08d80e2efbcb%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637274942249689695&sdata=5oCraRKFNL9Ycigz2OdgPHfyzE7xCtmkU7dHW%2FwcCds%3D&reserved=0]https://nam04.safelinks.protection.outlook.com/?url=https%3A%2F%2Fm.youtube.com%2Fwatch%3Fv%3D5uiroWBkdFY&data=02%7C01%7C%7C062fd6a2b0b34c3683cd08d80e2efbcb%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637274942249689695&sdata=5oCraRKFNL9Ycigz2OdgPHfyzE7xCtmkU7dHW%2FwcCds%3D&reserved=0 [/url]

url=[https://nam04.safelinks.protection.outlook.com/?url=https%3A%2F%2Fm.youtube.com%2Fwatch%3Fv%3D_6tpQE7ptqo&data=02%7C01%7C%7C062fd6a2b0b34c3683cd08d80e2efbcb%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637274942249689695&sdata=Bh6Fn%2F%2F1mGa6u85BU14S2LF%2FnFdQP%2FwDxwE7UJPUx%2F8%3D&reserved=0]https://nam04.safelinks.protection.outlook.com/?url=https%3A%2F%2Fm.youtube.com%2Fwatch%3Fv%3D_6tpQE7ptqo&data=02%7C01%7C%7C062fd6a2b0b34c3683cd08d80e2efbcb%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637274942249699688&sdata=O3lBAw%2F9UsA2NYR7dqE9tDYqnp5wBQIHmdVnNayCZNk%3D&reserved=0[/url]

url=[https://nam04.safelinks.protection.outlook.com/?url=https%3A%2F%2Fm.youtube.com%2Fwatch%3Fv%3DL_DIpklxXcI&data=02%7C01%7C%7C062fd6a2b0b34c3683cd08d80e2efbcb%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637274942249699688&sdata=dlFgJ%2BNpWrw30pcDW7dZFFEaPYdo0Ke5AIRD%2FPRbDmM%3D&reserved=0]https://nam04.safelinks.protection.outlook.com/?url=https%3A%2F%2Fm.youtube.com%2Fwatch%3Fv%3DL_DIpklxXcI&data=02%7C01%7C%7C062fd6a2b0b34c3683cd08d80e2efbcb%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637274942249699688&sdata=dlFgJ%2BNpWrw30pcDW7dZFFEaPYdo0Ke5AIRD%2FPRbDmM%3D&reserved=0 [/url]

https://nam04.safelinks.protection.outlook.com/?url=https%3A%2F%2Fm.youtube.com%2Fwatch%3Fv%3Db15MMzb_GWw&data=02|01||062fd6a2b0b34c3683cd08d80e2efbcb|84df9e7fe9f640afb435aaaaaaaaaaaa|1|0|637274942249699688&sdata=X5ClWKU9FTmBd6xLTXo4Dr%2Fv3gnV96nerv4dGZiGVbk%3D&reserved=0

I always say why kill something if you aren't going to overkill it? :expressionless:

The UNO or a more directly breadboard friendly similar powered Arduino like the original Nano would leave resources leftover, but that just means less to worry about. The UNO level of power is actually a very good march to this project.

Whether you write this yourself or find and tweak other solutions, you would def benefit from getting to a certain level of competence, which can be done with a very few real parts, or using the simulator.

The IDE has examples starting from near zero, which are usually linked to competent articles. Work them.

And you might just start read read reading on these fora. And read the code… ppl tend to see it as impenetrable and sling it around w/o looking close. Read threads that look interesting until they are clearly irrelevant or over your head or, soon enough, stuff you've already figured out.

a7

1 Like

Good to hear! That's kind of what I assumed although I hadn't considered using a Nano instead.

I'm a minimalist and like using the smallest/least complex solution to a problem, so maybe a Nano is the way to go. Any possible downsides to the Nano? Would it run just as smoothly? Could it potentially struggle with more complex traffic light simulations?

For the final project, what would be the best way to interface between my microcontroller and the lighting circuits? Should I use a breadboard only for testing then solder for the final product?

  • An inexpensive Arduino Mini should work too.
    You will need one FTDI serial adaptor, can be used with other Pro Minis later.
  • First a solderless breadboard, then soldered prototyping breadboard,
    then a PCB.
1 Like

I use the UNO almost exclusively, with good quality Dupont wires. For development.

With breadboards and modules that use standard connectors.

The UNO, Nano and a few others have almost if not exzctly the same resources from a software point of view, so it comes down to personal preference.

I do like the Mini @LarryD mentions, and having an FTDI adapter is handy in a number of other circumstances, some might consider it an essential tool.

The UNO wins here because you just plug it into the USB port and until you are needing significant extra current, you are onto the fun stuff.

a7

@LarryD - Your links...

https://youtu.be/_6tpQE7ptqo

https://youtu.be/L_DIpklxXcI

https://youtu.be/b15MMzb_GWw

https://youtu.be/eBIjsZ1dcT0

https://youtu.be/6PB0u8irn-4

https://youtu.be/5uiroWBkdFY

2 Likes

Thank you

1 Like

I'm going to look into the XIAO ESP32-S3 and the Pro Micro. Basically, I'm looking for the smallest/cheapest/simplest microcontroller than can handle the traffic light logic for any conceivable kind of intersection. I may end up making more of these in future, so will want the option of using a battery or wall wart, depending on the situation.

I'll only look into a MOSFET if the lights are too dim at the max current of the microcontroller pins. I'm trying to avoid unnecessary complexity but if MOSFETs are what it takes to achieve the desired brightness, I'll add them for sure.

I'm thinking of running a single ground wire to each traffic light pole for the red/yellow/green AND pedestrian red/white LEDs — so all 5 LEDs will share a ground but will each have a dedicated connection to the digital output from the MCU for independent control. Sound good? (Only two LEDs max out of the five on a pole will be on any any given time.) For your high school project, did you have groups of 3 LEDs share a ground or did you run a pair of wires from the breadboard to each LED?

I guess it depends how bright they end up looking in real life. Sounds like I'll only be able to have one LED per output pin as that's already going to be pushing it. As long as the MCU I end up going with has at least 18 PINs and can deliver sufficient current at each one, I might not need to use MOSFETs.

The good thing about the MOSFET scenario is there will be so much power on each MOSFET pin that I'll be able to group the anodes for LEDs that always come on simultaneously together, which will reduce the number of MCU output PINs I'll need to use (i.e. from 18 down to 10).

Still brainstorming at this stage. I'm not sure yet which of these is the smallest/simplest MCU than will be able to handle the logic for pretty much any controlled intersection:

  • Uno
  • Nano
  • Mini
  • Pro Mini
  • Pro Micro
  • XIAP ESP32-S3

8mA per output pin would probably be too low but it's hard to say without trying it in real life.

I'm pretty sure I won't fry anything — I'm generally pretty careful. I guess the main risk isn't from outright shorting an output pin to ground but from accidentally going over the 20mA with too many LEDs/resistors.

The Pro Mini has 14 outputs, which isn't enough for all 18 traffic light LEDs to each have their own output pin but is enough if they can double up two LED anodes per pin.

What happens if I try to pull 50mA from a 40mA pin? Would it damage the circuit feeding it or would it just refuse to provide more than 40mA?


I'm thinking Pro Micro for prototyping (because of the USB port), then Pro Mini for the finished product (cheaper + smaller footprint).

So even a 1A 5V power supply should be enough for your LEDs and Arduino. Even a 0.5A 5V power supply might be enough.

Exactly. And if 10mA per led is bright enough, that reduces to 180mA maximum.

Bear in mind that LEDs don't last forever. Good quality ones are less likely to suddenly fail, but all leds will gradually fade over time. The closer to that maximum current, the faster they fade. At 20mA, they might only be half as bright after a few years. But at 10mA, it might take over a decade for them to fade as much.

It's normal and because of the chemistry required to make each colour. But similar current will give similar brightness for any colour. So to achieve a consistent brightness across all the LEDs, you will need slightly different resistor values for the R & Y vs. the G, B & W.

1 Like

I suspect that groups of your LEDs will always be on or off together at the same time. For example the 6 red leds on the east & west sides of the intersection?

To avoid these groups needing one Arduino pin per led, you will want to use transistors of some kind to drive each group, because Arduino pins alone can't provide enough current for more than one led each.

Which groups will have the largest number of LEDs? Let's assume the largest group(s) will have 8 LEDs. That's a maximum of 160mA. Even many small transistors can handle that. For example bc337.

With modern leds ( from a good source ..) a much lower current can be used - say 2 mA and still give good brightness .

  • something like a 2k7 series resistor will get your project going and not cause over load on the pins .

Get yourself a 5v usb power supply , a genuine UNO and power via the 5v pin.
Some good leads , pack of resistors and whatever buttons/switches you want .

Be careful of shorts and wiring the power the wrong way around - colour code your wires and make it neat . It can be good to mount everything on a base board

Have fun !

  • have a look at model railway stuff , you might be able to get some traffic lights ready made .( or print some ….?)