Results 1 to 16 of 16

Thread: 4-Way Traffic Signal with Arduino

  1. #1
    Join Date
    Jan 2010
    Location
    Fort Worth, Texas, USA
    Posts
    174
    Thanks
    20
    Thanked 797 Times in 134 Posts
    Mentioned
    19 Post(s)
    Tagged
    0 Thread(s)

    Default 4-Way Traffic Signal with Arduino

    Over the past two weeks, I’ve purchased an Arduino Uno starter kit and learned how to write code for it. After doing some of the basic tutorials, I moved on to things that will be beneficial to me and other model railroaders. My first big project was to test & mock up a 4-way intersection with working traffic signals. I have my lights sequenced as Green for 20 seconds, Yellow for 5 seconds, all directions Red for 5 seconds and then set up to repeat in the opposite direction. Attached are some photos, of my breadboard tests and mock up. Once the breadboard testing was complete, I transferred the code to an Arduino Nano for permanent use on the layout. If anyone has interest in my code and components used, just let me know and I’ll post them. Happy Railroading, Jim

    4-Way Tinker.JPG4-Way Bread.jpg4-Way Nano.jpg4-Way Mock1.jpg4-Way Mock2.jpg

  2. The Following 14 Users Say Thank You to jimbobones For This Useful Post:


  3. #2
    Join Date
    Jan 2013
    Location
    Zanesville, OH
    Posts
    1,206
    Thanks
    6,499
    Thanked 3,962 Times in 764 Posts
    Mentioned
    58 Post(s)
    Tagged
    0 Thread(s)

    Default

    Neat project! Please post the code and info about the street lights. I am sure that I am not the only one interested.
    Bo D.
    B&O Keyridge Subdivision
    I'm not allowed to run the train, the whistle I can't blow. I'm not allowed to say how fast the Railroad Train can go.
    I'm not allowed to shoot off steam, nor even clang the bell. But let the damned Train jump the track, and see who catches hell!


  4. #3
    Join Date
    Jan 2010
    Location
    Fort Worth, Texas, USA
    Posts
    174
    Thanks
    20
    Thanked 797 Times in 134 Posts
    Mentioned
    19 Post(s)
    Tagged
    0 Thread(s)

    Default

    Per @RailKing50 's Request;

    1. The traffic lights I found on eBay. Currently there aren't any more available but I'm sure there will be more soon. It's from a seller who deals a lot in HO / N scale LED street lamps, signals, etc. I shot him a private message to see when he might have more in and will let everyone know. I bought the last 8 he had but would like to buy more now that I know the work well.

    2. The Green lights are about 50% brighter than the Yellows and the Reds so I'll be adding resistors to the Green wires to "dumb" them down a bit. I'll post which resistor value works best to even them out once I've tested them.

    3. Arduino Code Below - NOTE: This is for common Anode wiring (Black Wire with resistor to Anode / Red, Yellow & Green Wires to separate Cathodes so a digitalWrite LOW lights the LED's not digitalWrite HIGH.


    *** SEE REVISED CODE IN THE POST BELOW ***
    Last edited by jimbobones; 30th Jan 2019 at 10:17 AM. Reason: Revised the Arduino Code

  5. The Following 6 Users Say Thank You to jimbobones For This Useful Post:


  6. #4
    Join Date
    Dec 2011
    Location
    Huntley,Ill
    Posts
    2,314
    Thanks
    4,963
    Thanked 2,395 Times in 987 Posts
    Mentioned
    32 Post(s)
    Tagged
    1 Thread(s)

    Default

    Very cool traffic lights, I’ll keep an eye out for more,,,,,,
    Arduino,,yet another thing I have that’s still in the queue to work with along with my 3d printer kit.

  7. #5
    Join Date
    Apr 2014
    Location
    southeast michigan
    Posts
    2,030
    Thanks
    1,661
    Thanked 3,987 Times in 1,194 Posts
    Mentioned
    101 Post(s)
    Tagged
    0 Thread(s)

    Default

    Can you label the pins something like Nred, Ngreen, Nyellow etc. Then it could look something like
    digitalWrite(Wyellow, LOW);
    Otherwise I'm having a hard time figuring out whats what in the code, although I'm sure I could map it out from the photo.
    Moving coal the old way: https://youtu.be/RWJVt4r_pgc
    Moving coal the new way: https://youtu.be/sN25ncLMI8k

  8. #6
    Join Date
    Dec 2003
    Location
    Columbus,OH, USA
    Posts
    3,333
    Thanks
    91
    Thanked 1,707 Times in 964 Posts
    Mentioned
    53 Post(s)
    Tagged
    0 Thread(s)

    Default

    Quote Originally Posted by NtheBasement View Post
    Can you label the pins something like Nred, Ngreen, Nyellow etc. Then it could look something like
    digitalWrite(Wyellow, LOW);
    Otherwise I'm having a hard time figuring out whats what in the code, although I'm sure I could map it out from the photo.

    You certainly can.

    You add a line like the following for each pin you want to name:

    Code:
    #define Wyellow pin
    Where pin is the number of the pin you are naming

    One other simplification here, you only need 6 outputs for the signals you have. Think about this, the east and west signals always show the same thing, as do the north and south signals.

    You can easily connect two LEDs either in parallel or in series to the Arduino outputs.

    Paul
    For decoder installation and JMRI services, please visit http://www.bentraildigital.com
    For n-scale intermodal information, please visit http://nscaleintermodal.com

  9. The Following 3 Users Say Thank You to pbender For This Useful Post:


  10. #7
    Join Date
    Jan 2010
    Location
    Fort Worth, Texas, USA
    Posts
    174
    Thanks
    20
    Thanked 797 Times in 134 Posts
    Mentioned
    19 Post(s)
    Tagged
    0 Thread(s)

    Default

    Hey pbender,

    I did exactly as you suggested when I did my mock up last night and only used 6 pins instead of all twelve like my original breadboard setup. I bought two sets of lights (8 total traffic signals) planning to use one nano to control two complete intersections (or maybe even more) so, that's why I wrote the code to use all 12 digital pins. But, with the Nano's only costing about $4.75 each, they're affordable enough to have several on the layout controlling just a few lights each.

    NtheBasement,

    I'll re-write my code tomorrow and post it defining the pins as names to make it easier to understand.

  11. The Following 3 Users Say Thank You to jimbobones For This Useful Post:


  12. #8
    Join Date
    Jul 2012
    Location
    TX
    Posts
    1,945
    Thanks
    1,650
    Thanked 6,618 Times in 1,241 Posts
    Mentioned
    47 Post(s)
    Tagged
    0 Thread(s)

    Default

    That's cool - I did a 3-way for my last layout controlled by arduino, but on this one I haven't re-installed the controller, but I did mount the three-way traffic lights wired as a static setup. Perhaps in the future I will get it all working again.

  13. #9
    Join Date
    Jan 2010
    Location
    Fort Worth, Texas, USA
    Posts
    174
    Thanks
    20
    Thanked 797 Times in 134 Posts
    Mentioned
    19 Post(s)
    Tagged
    0 Thread(s)

    Default

    @NtheBasement

    1. Below is the revised code you've requested. It's trimmed down so only six pins are used as @pbender suggested. In this setup, two Traffics Lights are connected to Pins 2,3 & 4 and the other two Traffic Lights are connected to pins 5,6 & 7. Sorry for the delay, I was watching the Rolex 24 at Daytona.

    To all,

    The traffic lights will be available on eBay again after Chinese New Year on Feb. 11th, 2019. The seller's name is wehonest_us - he's US based selling goods from China with over 10,000 sales under his belt and has 99.7% positive feedback.


    /* American Traffic Signal for North/South - East/West operation.
    * Length of time for lights can be adjusted by changing the delay times.
    * Have fun with it. Jim Barclay Version 2 012719
    */

    int trafficDirection = 0; //Set Traffic Direction to North/South

    #define NS_Green 2
    #define NS_Yellow 3
    #define NS_Red 4

    #define EW_Green 5
    #define EW_Yellow 6
    #define EW_Red 7


    void setup()
    {
    pinMode(2, OUTPUT);
    pinMode(3, OUTPUT);
    pinMode(4, OUTPUT);
    pinMode(5, OUTPUT);
    pinMode(6, OUTPUT);
    pinMode(7, OUTPUT);
    Serial.begin(9600);
    }

    void loop()
    {
    digitalWrite(NS_Green, LOW); // North/South - GREEN - East/West - RED
    digitalWrite(NS_Yellow, HIGH);
    digitalWrite(NS_Red, HIGH);
    digitalWrite(EW_Green, HIGH);
    digitalWrite(EW_Yellow, HIGH);
    digitalWrite(EW_Red, LOW);
    delay(20000); // Wait 20 Seconds


    if (trafficDirection == 0) {
    digitalWrite(NS_Green, HIGH); // North/South - Turns YELLOW - East/West - RED
    digitalWrite(NS_Yellow, LOW);
    digitalWrite(NS_Red, HIGH);
    digitalWrite(EW_Green, HIGH);
    digitalWrite(EW_Yellow, HIGH);
    digitalWrite(EW_Red, LOW);
    delay(3000); // Wait 3 Seconds for YELLOW light

    digitalWrite(NS_Yellow, HIGH);
    digitalWrite(EW_Red, LOW);
    digitalWrite(NS_Red, LOW);
    delay(5000); // Wait 5 Seconds where both direction are RED

    trafficDirection = 1; //Change Traffic Direction to East/West
    Serial.println(trafficDirection);
    }

    digitalWrite(NS_Green, HIGH); // North/South - RED - East/West - GREEN
    digitalWrite(NS_Yellow, HIGH);
    digitalWrite(NS_Red, LOW);
    digitalWrite(EW_Green, LOW);
    digitalWrite(EW_Yellow, HIGH);
    digitalWrite(EW_Red, HIGH);
    delay(20000); // Wait 20 Seconds

    if (trafficDirection == 1) {

    digitalWrite(NS_Green, HIGH); // North/South - RED - East/West - Turns YELLOW
    digitalWrite(NS_Yellow, HIGH);
    digitalWrite(NS_Red, LOW);
    digitalWrite(EW_Green, HIGH);
    digitalWrite(EW_Yellow, LOW);
    digitalWrite(EW_Red, HIGH);
    delay(3000); // Wait 3 Seconds for YELLOW light

    digitalWrite(EW_Yellow, HIGH);
    digitalWrite(EW_Red, LOW);
    digitalWrite(NS_Red, LOW);
    delay(5000); // Wait 5 Seconds where both direction are RED

    trafficDirection = 0;
    Serial.println(trafficDirection);
    }
    }

  14. The Following 4 Users Say Thank You to jimbobones For This Useful Post:


  15. #10
    Join Date
    Apr 2014
    Location
    southeast michigan
    Posts
    2,030
    Thanks
    1,661
    Thanked 3,987 Times in 1,194 Posts
    Mentioned
    101 Post(s)
    Tagged
    0 Thread(s)

    Default

    @jimbobones thanks for the update. No problem with the timing; I'm interested in learning how to program arduinos for my own nefarious purposes. The code makes sense standing by itself and is easy to follow.
    Moving coal the old way: https://youtu.be/RWJVt4r_pgc
    Moving coal the new way: https://youtu.be/sN25ncLMI8k

  16. #11
    Join Date
    Jan 2010
    Location
    Fort Worth, Texas, USA
    Posts
    174
    Thanks
    20
    Thanked 797 Times in 134 Posts
    Mentioned
    19 Post(s)
    Tagged
    0 Thread(s)

    Default

    @NtheBasement

    I have a background in I.T. and have learned a few programming languages along the way. For example, Visual Basic, so after buying a $35 kit on Amazon (which includes an Uno clone) and watching a few Youtube videos on the Arduino, I found it the easiest language to learn, especially for lighting LED's or activating servo's for example. Two concepts that can help automate a model railroad and my main reason for learning with the Arduino. Here's where I got started.

    Learning Kit by Elegoo

    https://www.amazon.com/gp/product/B01D8KOZF4/ref=ppx_yo_dt_b_asin_title_o04__o00_s00?ie=UTF8&ps c=1



    Great Youtube Video Series on the Arduino


    https://www.youtube.com/watch?v=09zf...eapCS_UrsgfuJA




    A Drag and Drop way to write code and make a graphical working prototype


    https://www.tinkercad.com/circuits


    Have fun, Jim
    Last edited by jimbobones; 30th Jan 2019 at 06:33 PM.

  17. The Following 4 Users Say Thank You to jimbobones For This Useful Post:


  18. #12
    Join Date
    Jan 2013
    Location
    Zanesville, OH
    Posts
    1,206
    Thanks
    6,499
    Thanked 3,962 Times in 764 Posts
    Mentioned
    58 Post(s)
    Tagged
    0 Thread(s)

    Default

    @jimbobones - Thanks for uploading the code. Little things like this really bring a scene to life!

    I am working on wiring up the upper floors in my buildings to random cycle LEDs on and off to look as if someone is moving between rooms using an Arduino Mega. I have the basic code and lights, just need to finish all of the interiors......

    Quote Originally Posted by jimbobones View Post
    The traffic lights will be available on eBay again after Chinese New Year on Feb. 11th, 2019. The seller's name is wehonest_us - he's US based selling goods from China with over 10,000 sales under his belt and has 99.7% positive feedback.
    I have bought from this seller before and would do it again! Thanks for digging into the restock time line.
    Bo D.
    B&O Keyridge Subdivision
    I'm not allowed to run the train, the whistle I can't blow. I'm not allowed to say how fast the Railroad Train can go.
    I'm not allowed to shoot off steam, nor even clang the bell. But let the damned Train jump the track, and see who catches hell!


  19. #13
    Join Date
    Jan 2010
    Location
    Fort Worth, Texas, USA
    Posts
    174
    Thanks
    20
    Thanked 797 Times in 134 Posts
    Mentioned
    19 Post(s)
    Tagged
    0 Thread(s)

    Default

    @RailKing50

    Are you going to randomly light the rooms or follow a sequence? Also, please post photos or your interiors when you have one or more done so we can see how you block off individual rooms.

    My next Arduino project is to put Red "blinking" lights on top of tall structures like my Grain Elevator. Instead of just blinking on and off, I'm going to have them fade in & fade out mimicking incandescent bulbs which I think will look better.



    Jim

  20. The Following 2 Users Say Thank You to jimbobones For This Useful Post:


  21. #14
    Join Date
    Jan 2013
    Location
    Zanesville, OH
    Posts
    1,206
    Thanks
    6,499
    Thanked 3,962 Times in 764 Posts
    Mentioned
    58 Post(s)
    Tagged
    0 Thread(s)

    Default

    @jimbobones

    In the code I will name each LED and then turn them on and then off later in the code. It will just be on a continuous loop.

    I am sure there are better ways of doing this, but for me I know how to do it that way so I am going to stick with it! K.I.S.S
    Bo D.
    B&O Keyridge Subdivision
    I'm not allowed to run the train, the whistle I can't blow. I'm not allowed to say how fast the Railroad Train can go.
    I'm not allowed to shoot off steam, nor even clang the bell. But let the damned Train jump the track, and see who catches hell!


  22. The Following User Says Thank You to RailKing50 For This Useful Post:


  23. #15
    Join Date
    Jan 2010
    Location
    Fort Worth, Texas, USA
    Posts
    174
    Thanks
    20
    Thanked 797 Times in 134 Posts
    Mentioned
    19 Post(s)
    Tagged
    0 Thread(s)

  24. The Following 3 Users Say Thank You to jimbobones For This Useful Post:


  25. #16
    Join Date
    Apr 2014
    Location
    southeast michigan
    Posts
    2,030
    Thanks
    1,661
    Thanked 3,987 Times in 1,194 Posts
    Mentioned
    101 Post(s)
    Tagged
    0 Thread(s)

    Default

    Quote Originally Posted by RailKing50 View Post
    @jimbobones

    In the code I will name each LED and then turn them on and then off later in the code. It will just be on a continuous loop.

    I am sure there are better ways of doing this, but for me I know how to do it that way so I am going to stick with it! K.I.S.S
    Would this work for a loop? Use random(2,13) to generate an output pin, turn it on, then random(5000 or so) to wait, then random(2,13) again to turn one off, then another wait? Half the time the light will already be in that state but it shouldn't matter (I think).
    Moving coal the old way: https://youtu.be/RWJVt4r_pgc
    Moving coal the new way: https://youtu.be/sN25ncLMI8k

Similar Threads

  1. Fun with Arduino!
    By TwinDad in forum DIY Electronics Projects
    Replies: 20
    Last Post: 18th Mar 2019, 05:46 PM
  2. Arduino Pico!
    By TwinDad in forum Turnout Control Devices, Signaling, & Detection.
    Replies: 19
    Last Post: 4th Nov 2018, 05:58 PM
  3. Arduino Signal Control w/ Ecos and Traincontroller.
    By Chicago Rail in forum Turnout Control Devices, Signaling, & Detection.
    Replies: 25
    Last Post: 8th Feb 2018, 10:47 PM
  4. Arduino-based turnout/signal controller
    By TwinDad in forum Turnout Control Devices, Signaling, & Detection.
    Replies: 11
    Last Post: 4th Apr 2017, 03:59 PM
  5. Traffic signals
    By Ngineer in forum Product & Service Announcements
    Replies: 10
    Last Post: 17th Apr 2015, 03:50 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •