Gene W.

I read with great interest the excellent beginner’s Arduino tutorial by Dr. Geoff Bunza.   See: https://forum.mrhmag.com/post/scale-model-animation-8-starting-projects-and-virtual-rooms-for-modeling-12196257  I decided to experiment.

First, I found 10 Arduino Pro Minis (5 volt) in China for $20.10 delivered to my door.  For $2.01 each, I’ll let a few take one for the team while I learn.

Next, having no previous Arduino experience, I followed Geoff’s steps and ultimately came up with a street traffic light sequence that uses only six Pro Mini I/O pins and can be used with any combination of street traffic lights, whether in the United States or in the United Kingdom, up to (at least) 66 three-light poles.

The upcoming circuit (Fig. 6) can accommodate at least 16 four-way intersections and works for any scale.

The following street traffic light situations (Fig. 1 to Fig. 5) require:

One Arduino Pro Mini

Six 2N3904 NPN transistors

Six 470 ohm, ¼ watt resistors

The given number of LEDs for each figure

And the given number of calculated resistors for the LEDs

o%20Lane.gif 

Reply 0
Gene W.

More Animation and the Rest of the Story

20T-Bone.gif 

Fig. 2 will need 9 LEDs = 3 Red, 3 Yellow, 3 Green.

Six xxxx Ohm, ¼ Watt resistors

The animation time is 5 seconds per light change.


Sequence.gif 

Fig. 3 will need 12 LEDs = 4 Red, 4 Yellow, 4 Green

Six xxxx Ohm, ¼ Watt resistors

The animation time is 5 seconds per light change.


Sequence.gif 

Fig. 4 also will need 12 LEDs = 4 Red, 4 Yellow, 4 Green

Six xxxx Ohm, ¼ Watt resistors

The animation time is 5 seconds per light change.


Sequence.gif 

Fig. 5 needs:

54 LEDs = 18 Red, 18 Yellow and 18 Green.

A total of 24 LEDs are used for the Red, Yellow, and Green lights on the North-South routes.

A total of 30 LEDs are used for the Red, Yellow, and Green light on the East-West routes.

15 xxxx Ohm, ¼ Watt resistors

The animation time is 10 seconds per light change.

City blocks are not to scale


The Schematic:

Wiring-2.jpg 

The LEDs next to the 12 volts in Fig. 6 are wired as “common anode” (see Fig. 7 for clarification).

The 470-ohm Resistor Value:

Those six 470-ohm resistors in Fig. 6:

Each 470-ohm resistor at the Arduino I/O pins (2,4,6,8,10 and 12) in Fig. 6 are fixed and do not affect how many LEDs the 2N3904 transistor can light.  I chose 470 ohms because I wanted to turn the transistor on hard with about 10ma.  The maximum current of each Arduino output is 40ma.  I cut that to 10ma to be safe and to keep the smoke inside the Arduino.

Assuming the Arduino puts out 5 volts when high (it’s a little less), and ignoring the voltage drop across the base-emitter junction (it’s about 0.7 volts), I used Ohms Law R=E/I to find the resistor value.  So, R = 5 volts/ 0.010 amps = 500 ohms.  I picked a 470-ohm resistor, hooked it up and measured about 8.5ma (0.0085 amps).  Close enough.  The actual voltage across the resistor is 4 volts.  Now I know the Arduino will not need to deliver over 25ma total for any combination of LEDs being energized.

UK operation will have up to three Arduino outputs energized simultaneously (about 25ma total); US operation will only have two outputs energized simultaneously, (about 17ma total).

How the Transistor Works:

And how one sixth of the circuit works (shown are the 10 East-West RED LEDs in figure 5).

nimation.gif 

(LEDs 8, 16, and 20 are wired “common anode”.)

The Arduino can deliver up to 40ma per output.  The 470-ohm resistor between the Arduino I/O and the base of the transistor limits the base current to about 8.5ma.  That transistor base current then allows the transistor to conduct from its collector to its emitter.  When the C-E path of the transistor is conducting, the collector voltage drops to about 0.08 volts.  That’s almost a short circuit.  Current then flows from the 12-volt supply through the LEDs and the resistor, and through the transistor to ground.  All the LEDs will light. The 2N3904 is capable of up to 200ma between the collector and the emitter but shouldn’t be designed for over 100ma.  Still, that’s a lot of LEDs potentially being powered!

The Other Resistor Values:

ED%20box.jpg 

What’s the value of “R-?” 

It depends on the LED color and how many LEDs are wired in series.  It also depends on how bright you want the traffic lights.   LEDs are current devices, meaning the more current, the brighter it gets.  Increasing resistor “R-?” dims all four LEDs equally.

For example:

GREEN LEDs:  Most LEDs have a current rating of 20ma at maximum brightness.  To begin, I decided to cut that 20ma in half.  Going back to Ohm’s Law and assuming the C-E of the 2N3094 is a dead short, and also assuming the LEDs are shorted, I found R = 12volts/10ma = 1,200 ohms.  I selected a 1,000-ohm resistor and connected it in series with the LED and then to 12 volts.  Each Green LED lit up with 2.75 volts across it, and it was too bright.

Next I divided 12 volts by the LED volts and came up with 4.36 LEDs.  Can’t use over four LEDs.  I hooked up the four Green LEDs and the 1,000-ohm resistor in series as shown in Fig. 8.  The current through the string was about 1ma and the resistor had 0.65 volts across it.  The C-E voltage went to 0.35 volts, putting diodes into a different part of their current operating curve.  A small increase or decrease in voltage across any LED results in a tremendous change in brightness, so the current must be controlled, not the voltage.  This time the brightness was just about right.  I can always increase the resistance to make the LED dimmer if I want to.  Right now the value of “R-?” is 1,000-ohm for each string of four Green LEDs.

My RED LEDs need 1.95 volts each with a 1,000-ohm resistor, and the string draws about 3ma.

My YELLOW LEDs need 1.95 volts each with a 1,000-ohm resistor, and the string draws about 3ma.

For your own use, start out with a 1,000-ohm, ¼-watt resistor for one to four LEDs (any color), and increase the resistance until you get the brightness you are comfortable with on your own layout.

How many strings of four can you operate on one 2N3904? 

Four LEDs in series (string) is the maximum because of the Green LED voltage requirement and the 12-volt supply.  100ma is a safe 2N3904 transistor C-E current.  Dividing the 100ma by each string = number of sets of four LEDs.  I can safely run over 50 Green LEDs with one 2N3904 and 13 resistors (12 strings of four LEDs and one string of two LEDs).

Red and Yellow LEDs = 32 total with 8 resistors (8 strings of four LEDs = 96ma)

How Bright should the LEDs be?

The normal safe operating current for a LED is 20ma, but that’s for a flashlight where maximum brightness is wanted.  Remember, if you are three feet away from a HO scale traffic light, it’s the same as if you are 261 feet from a traffic light on a real city street.  It’s your layout and it’s your judgment call.  If you require your HO vehicle drivers to wear Elvis sunglasses at night, so be it.

Number of Single-sided Traffic Light Poles?

If each traffic light pole has three different color LEDs, then the RED and YELLOW LEDs determine the number of poles your layout can have (with the circuit in Fig. 6) because of those LED’s current requirement (3ma) and each 2N3904 safe current limit (100ma).  In this case: 33 poles per N-S roads (99ma per 2N3904) and 33 poles for the E-W.  That gives a total of 198 LEDs (33 per 2N3904 x six 2N3904s), or about 16 four-way intersections with two poles left over for one-way streets.  That’s a big town in a big layout!  Other circuitry can be used to increase that number and still use one Arduino.

Here is the Wired Breadboard:

dboard-1.jpg 

The breadboard is wired for two 4-way traffic lights (24 LEDs total), and the Arduino Pro Mini is in the lower left corner.  The resistors are all ¼ watt, and the LEDs are 3mm.  I have no way to make a video or I would show the actual traffic light sequence.

What the Arduino Sketch does:

The sketch (program) first tells the Arduino what I/Os are to be used and that they are also to be used as outputs.  It then turns all the Red LEDs on for two seconds.  This part of the sketch only does this when the Arduino is first powered up, or when its reset button is pushed.

The sketch then loops through 12 simple steps for (US) coordinated traffic lights:

Loop beginning:

1. Turn all lights Red

2.  Delay for xxxx seconds

2a.  (UK lights only) Turn on North-South Yellow lights

2b.  (UK lights only) Delay xxxx seconds

2c.  (UK lights only) Turn off North-South Yellow lights

3.  Turn off the North-South Red lights and turn on the North-South Green lights

4.  Delay for xxxx seconds

5.  Turn off the North-South Green lights and turn on the North-South Yellow lights

6.  Delay xxxx seconds

7.  Turn off the North-South Yellow lights and turn on the North-South Red lights.  East-West Red lights are already on.

8.  Delay xxxx seconds

8a.  (UK lights only) Turn on East-West Yellow lights

8b.  (UK lights only) Delay xxxx seconds

8c.  (UK lights only) Turn off East-West Yellow lights

9.  Turn off the East-West Red lights and turn on the East-West Green lights

10.  Delay xxxx seconds

11.  Turn off the East-West Green lights and turn on the East-West Yellow lights

12.  Delay xxxx seconds

Repeat loop.

Note: for UK users the light sequences in the program have hash marks ( // ) that need to be deleted where indicated in the sketch to activate them.  US users don’t need to do anything.

Any “Delay xxxx seconds” can be changed to accommodate local vehicle traffic on your layout, where 1,000 = one second.

There is a fine line between not enough comments in software and too much.  I tend to cross that line because I want to know what I was thinking a year from now if I revisit the software.  Those comments will automatically be stripped when loaded into the Arduino via the USB cable.

Follow Geoff’s instructions to copy and paste the sketch into the Arduino operating software.

Here is the sketch:


/* Arduino Automobile Traffic Lights sketch

This sketch works for both the U.S. and U.K. traffic light sequence.  Although written for a single intersection, it can also be used with a two-lane highway under repair, a T-bone intersection, or any number of coordinated intersections.  Coordinated means traffic on all N-S streets move at the same time, and all E-W street traffic move at the same time.

U.S. automotive traffic signal RED-GREEN-YELLOW color protocol is used. Optionally, the United Kingdom traffic signal RED-YELLOW-GREEN-YELLOW protocol can be used by removing slash marks in the two locations of the program where indicated.

The delay times can be changed to meet local conditions: “delay(1000);” means delay operation for one second.

This program requires a total of only 6 Arduino I/O pins for any number of coordinated intersections.

ODD numbered LEDs (LED-1, LED-3, LED-5, etc.) are used to control North-South traffic.

EVEN numbered LEDs (LED-2, LED-4, LED-6, etc.) are used to control East-West traffic.

For an East-West two-lane road with construction blockage, the southern lane of the road is considered the Even lane and the northern lane is considered the Odd lane (see Fig.1)

Size: 1,390 bytes for U.S.; 1,434 bytes for U.K.

Written by Gene Warren and placed into the public domain on 15 December 2014 */

// ODD numbered LEDS for North-South traffic on Odd Street

int redN = 2; // I/O pin 2, Red-1, Red-3

int yellowN = 4; // I/O pin 4, Yellow-1, Yellow-3

int greenN = 6; // I/O pin 6, Green-1, Green-3

// EVEN numbered LEDs for East-West traffic on Even Street

int redE = 8; // I/O pin 8, Red-2, Red-4

int yellowE = 10; // I/O pin 10, Yellow-2, Yellow-4

int greenE = 12; // I/O pin 12, Green-2, Green-4

void setup() {

// set the appropriate pins as outputs

pinMode(redN, OUTPUT);

pinMode(yellowN, OUTPUT);

pinMode(greenN, OUTPUT);

pinMode(redE, OUTPUT);

pinMode(yellowE, OUTPUT);

pinMode(greenE, OUTPUT);

/* Preset BOTH streets to a stop condition to initialize.  This portion of the program is only run once at the Arduino startup. */

digitalWrite(redN, HIGH); // Turn ON Red-1, Red-3 for North-South traffic on Odd Street

digitalWrite(yellowN, LOW);  // Turn off Yellow-1, Yellow-3 for North-South traffic on Odd Street

digitalWrite(greenN, LOW);  // Turn off Green-1, Green-3 for North-South traffic on Odd Street

digitalWrite(redE, HIGH); // Turn ON Red-2, Red-4 for East-West traffic on Even Street

digitalWrite(yellowE, LOW);  // Turn off Yellow-2, Yellow-4 for East-West traffic on Even Street

digitalWrite(greenE, LOW);  // Turn off Green-2, Green-4 for East-West traffic on Even Street

// Both directions now have a red light to start the Traffic Control Sequence

delay(2000); // Two second delay at startup

}

/*  Point A.  -----------------------------------------------------

Start the Traffic Control Sequence:

A HIGH at the I/O pin will turn the appropriate LED ON; a LOW will turn it OFF.  To begin, start with traffic flowing North-South.  All traffic has been properly halted.

Program looping will jump in here. */

void loop() {

// North-South flowing traffic on Odd Street

// START North-South flowing traffic (Green-1, Green-3)

/* For the United Kingdom and some other countries, a yellow light will also precede the green light. Remove the slash marks for the following two lines for the U.K; keep them for the U.S.*/

// digitalWrite(yellowN, HIGH); /* Turn ON YELLOW lights for traffic flowing N-S (Yellow-1, Yellow-3) */

// delay(5000); // Keep it on for 5 seconds

digitalWrite(yellowN, LOW); // Turn off U.K. yellow lights if used; no effect for U.S. yellow lights

digitalWrite(redN, LOW); // Turn OFF RED lights for traffic flowing N-S

digitalWrite(greenN, HIGH); // Turn ON Green lights for traffic flowing N-S

// Hold green light for 10 seconds

delay(10000);

// Time to caution N-S flowing traffic with Yellow-1, Yellow-3 LEDs

digitalWrite(greenN, LOW); // Turn off N-S Green lights

digitalWrite(yellowN, HIGH); // Turn ON N-S Yellow lights

delay(5000); // North-South flowing traffic has 5 seconds to screech to a halt

// Stop North-South flowing traffic with Red-1, Red-3 LEDs

digitalWrite(yellowN, LOW); // Turn Off  N-S YELLOW lights

digitalWrite(redN, HIGH);  //  Turn ON N-S RED lights

// Stop both directions to clear out the North-South traveling rubberneckers and slowpokes

// North-South flowing Red light is now on

// East-West flowing Red lights (Red-2, Red-4) controlling Even Street are already on

delay(5000);  // Allow 5 additional seconds to clear the intersection

// East-West flowing traffic on Even Street

/* The following two lines are for the United Kingdom traffic lights.  Remove the preceding slash marks to activate the commands. */

// digitalWrite(yellowE, HIGH); // turn ON Yellow-2, Yellow-4 (Red-2, Red-4 are already on)

// delay(5000); // keep it on for 5 seconds

// Allow East-West traffic on Even Street to proceed (Green-2, Green-4)

digitalWrite(yellowE, LOW); // Turn off Yellow Lights for U.K., if used

digitalWrite(redE, LOW); // Turn off Red Lights

digitalWrite(greenE, HIGH); // Turn ON Green lights

delay(10000); //Allow 10 seconds for traffic to flow East-West

// Caution E-W traffic on Even Street (Yellow-2, Yellow-4)

digitalWrite(greenE, LOW); // E-W Green lights goes off

digitalWrite(yellowE, HIGH); // E-W Yellow lights comes ON

delay(5000); // Even Street traffic has 5 seconds to screech to a halt

// Stop E-W traffic on Even Street (Red-2, Red-4)

digitalWrite(yellowE, LOW); // Turn Off  E-W Yellow lights

digitalWrite(redE, HIGH);  //  Turn ON E-W Red lights

// Stop BOTH directions to clear out the rubberneckers and slowpokes in the intersection

// N-S red lights are already on

delay(5000); // Allow 5 seconds to clear the intersection

/* At this point all RED LEDs (Odd Red-1, Red-3, Even Red-2, Red-4) have been on for 5 seconds, and the program will loop under the same conditions as when the loop first started.

Loop the Traffic Control Sequence back to Point A until either the Arduino loses power or its reset button is pushed. */

} // -------------------------------------------------------------


Comments:

Draw out your streets and put traffic pole on each corner as I have done in Figs. 1-5.  Number the N-S lights as odd LEDs and the E-W LEDs as even to make your wiring easier.  Any hanging four-way traffic light is considered four poles, one on each intersection corner. Then determine which Arduino output and 2N3904 to use.

I would keep all the resistors on any built board and not scattered all over the layout.  Troubleshooting will be so much easier.

I may have been a little heavy with the math but felt it was necessary, especially when describing the electronics.  If you understand how I got from A to B, you can get there too.

This is a one-trick-pony, meaning the Arduino can’t be used for much of anything else while the traffic light are operating, unless you want to turn something on when a light changes.  A “walk/don’t walk” pedestrian light can easily be incorporated into the scheme of things.  Yes, more Arduino inputs/outputs are available, but your application still shouldn’t interrupt vehicle traffic flow.

Some may think that the computational power of the Arduino Pro Mini is being wasted.  Perhaps, but I think of the Arduino Pro Mini as a $2.01 digital chip that is simply doing what I want.

Have Fun!

Gene Warren

Reply 0
RossA

Gene, Very nice! This is the

Gene,

Very nice! This is the kind of animation experimentation that Geoff had in mind and thanks for the detailed sharing of your work.

Ross

Reply 0
Prof_Klyzlr

"Train crossing" trigger option?

Dear Gene,

Could this be adapted with a single user push-button trigger/"interupt" input?
I have a hankering to build the 99E/43E intersection in Oregon City, Ore

https://goo.gl/maps/ZnfDe

and would need a "train movement" request to set all traffic lights to red, and illuminate the "train signal" head for a perscribed period of time...

Happy Modelling,
Aim to Improve,
Prof Klyzlr

Reply 0
HVT Dave

Yes

Prof,

The Pro Mini could interrupt the sketch with a push button switch, or you could use IR sensors on both sides of the crossing, and the Pro Mini could also automatically activate the entire crossing process.  See Dr Geoff's thread at:

https://forum.mrhmag.com/post/scale-model-animation-14-grade-or-level-crossing-control-12198577

Plenty of capacity left in this application for such things, utilizing some of the capacity that Gene mentioned.

Regards,

Dave

Dave

Member of the Four Amigos

 

Reply 0
pierre52

Thanks for sharing Gene

What a well written and illustrated post.  I am so glad that readers are starting to share their great ideas and this is certainly one of them

Peter

The Redwood Sub

Reply 0
Graeme Nitz OKGraeme

Excellent

I think this would make a great article for MRH.

I was going to do this myself but you saved me the trouble!

Thanks.

Graeme Nitz

An Aussie living in Owasso OK

K NO W Trains

K NO W Fun

 

There are 10 types of people in this world,

Those that understand Binary and those that Don't!

Reply 0
BruceNscale

Thanks for Sharing

Hi Gene,

Very nice article.  Traffic signals are a good item to make your railroad look alive.

ignature.jpg 

Happy Modeling, Bruce

Reply 0
Geoff Bunza geoffb

Neat Project & Computational Power

Hi Gene,

This is a neat project to tackle for layout animation with an Arduino-- and very well suited to the task. Nicely done!

Two comments: The 2N3907 you specify in Figure 6. can be replaced by the 2N3904 you refer to further down in Figure 7. The 2N3907 is hard to find these days, and the 2N3904 is quite common, available, and cheap.

Your last comment about cost and computational power is well taken. The Pro Mini is so cheap that I use it even to blink a single crossing signal, because even a home built 555 timer circuit will cost me more! I don't think I've completely adjusted to this reality yet. 

Best Regards,

Geoff Bunza

Geoff Bunza's Blog Index: https://mrhmag.com/blog/geoff-bunza
More Scale Model Animation videos at: https://www.youtube.com/user/DrGeoffB
Home page: http://www.scalemodelanimation.com

Reply 0
Gene W.

2N3904 vs 2N3907 in Fig. 6

Hi Geoff,

Those should all be 2N3904 in Fig. 6.

The Hand/eye coordination, copying/pasting, and internal multitasking in my head (along with too much coffee) let errors creep in.  I should have caught that one.  Thanks for pointing it out.

You're right, to duplicate this project with 555 timers would not only cost a lot more that my Arduino, trying to set individual timing sequences for each light color would be a nightmare.  I wouldn't even attempt it.  I just wish the Arduino would have been available in 1978 when I bought my first computer - a TRS-80.  "Trash 80" for those old enough to remember.

Gene

P.S.  I just figured out I could edit an old post, so I fixed the 2N3907-2N3904 error.  This is some great forum software!

Reply 0
Alexedwin

Ahh! The Trash 80 This thing

Ahh! The Trash 80

This thing completely changed my life.

I quit my trade as a Fitter & Turner (Engineer) & grew with the PC market because of this beast.

Alex

One day I might be modeling the Puffing Billy Railway, Victoria, Australia.

My location - Queensland, Australia.

Reply 0
Gene W.

For the Prof in Oregon City…

Quote:

Could this be adapted with a single user push-button trigger/"interrupt" input?

Yes, it can.

The following animation depicts the train movement I think you have in mind:

nimation.gif 

The flippin’ pages are every seven seconds each.  Look for the “Loop Start” in the upper right hand corner.  The actual light changes are not seven seconds in the Arduino sketch, but are individually settable.

The sequence is:

1. All Red, Start Loop

2. N-S Green

3. N-S Yellow

4. All Red

5. E-W Green

6. E-W Yellow

7. All Red

8. Train has the right of way (if pushbutton was held)

9. E-W green

10. E-W yellow

11. All red

12. Train has the right of way (if pushbutton was held)

Loop to 1

Even though the notation in the bottom left corner of some of the pages says, “Train has R-O-W  (Button was held on X-X Yellow until lights turned Red)”, interpret that to read: “until Train Green permission is given”.

I constructed the light sequences as shown because you don’t want to interrupt normal vehicle flow, such as making commuters on the four-lane McCoughlin Blvd. screech to a halt when they had a Green light and didn’t get a yellow caution light.

If the above animation sequence is what you had in mind, you will need three additional Arduino I/Os; one for the pushbutton and two for the train signal LEDs.

You will also need to make some Sketch additions, given later.

First the input pushbutton:

n%20City.jpg 

The 10,000-ohm resistor is used as a “pull-up” resistor for the Arduino to make sure the Arduino input detects a “high” input signal when the pushbutton is left alone (not pushed).  The pushbutton is the normally open variety (N/O). You can also use a 22,000-ohm resistor for a 12-volt system.  As Dave mentioned earlier, you could incorporate Dr. Geoff’s IR Detector instead of a pushbutton to completely automate that intersection.

The Train Permission Signal LEDs:

Those train LEDs cannot be tied in with any of the street LEDs because the train Red needs to be on until all the street traffic lights go Red, but then the train Red light goes off and its Green light turned on.

The Google image shows the train signal for a northbound train, but I can’t tell if the train signal is two-sided or not.  I would assume that train also would travel from north to south since it dead-ends on Main Street across from the Happy Lounge?  If that train signal is two-sided, you will need an additional two Red LEDs and two Green LEDs.  Those LEDs would not need 2N3904 transistors if you wire them like this:

n%20LEDs.jpg 

Select the resistors for the brightness you want, starting with the indicated 2.2k-ohm, but don’t exceed the Arduino output capability of 40ma for each output or you will activate its smoke release valve.

Five Sketch (software) additions:

First, paste the following five lines at the beginning of the sketch:


// I/Os for the train signals

int buttonT = 3; // I/O pin 3, Pushbutton

int redT = 5; // I/O pin 5,  Train Red LEDs

int greenT = 7; // I/O pin 6, Train Green LEDs

int buttonTValue = 0;  // LOW = pushbutton is held down


Second, tell the Arduino what to do with those extra inputs by pasting the following in the void setup() section:


pinMode(buttonT, INPUT);  // Input pushbutton

pinMode(redT, OUTPUT); // Train red LEDs

pinMode(greenT, OUTPUT);  // Train green LEDs


Third, add the following to the Presets section at the beginning of the program:


// Preset the train lights to Red = ON and Green = OFF at startup, before the loop section

digitalWrite(greenT, LOW); // Turn off the green train light

digitalWrite(redT, HIGH); // Turn ON the train Red light


The only time the train should be given the green light is when all vehicle traffic has stopped, so the code following must be inserted into the sketch after all the traffic lights go red.  This is to insure that idiots running the red light get out of the way. The train delay should give the train enough time to clear the intersection. 

The code, given below, should be pasted in two locations:


/* Read the input for the pushbutton.  If the pushbutton is not being held down, skip this section and go to normal street traffic light operations */

buttonTValue = digitalRead(buttonT);

// If the pushbutton is LOW, i.e., pushed and held, do the following

if (buttonTValue == LOW){

digitalWrite(redT, LOW); // Turn off the train red light

digitalWrite(greenT, HIGH); // Turn on the train green light

delay(20000); /* 20 seconds, or change to appropriate timing for the train movement through the intersection */

digitalWrite(greenT, LOW); // Turn off the train green light

digitalWrite(redT, HIGH);  // Turn on the train red light

/* The train now should have cleared the intersection and the N-S or E-W directional green traffic lights will activate normally. */

}


There are only two places to paste this extra code:

One for the North-South traffic:

Fourth addition:


// Stop North-South flowing traffic with Red-1, Red-3 LEDs

digitalWrite(yellowN, LOW); // Turn Off  N-S YELLOW lights

digitalWrite(redN, HIGH);  //  Turn ON N-S RED lights

// Stop both directions to clear out the North-South traveling rubberneckers and slowpokes

// North-South flowing Red light is now on

// East-West flowing Red lights (Red-2, Red-4) controlling Even Street are already on

delay(5000);  // Allow 5 additional seconds to clear the intersection

Paste Code Here:


And for the East-West traffic

Fifth and last sketch addition:


// Stop E-W traffic on Even Street (Red-2, Red-4)

digitalWrite(yellowE, LOW); // Turn Off  E-W Yellow lights

digitalWrite(redE, HIGH);  //  Turn ON E-W Red lights

// Stop BOTH directions to clear out the rubberneckers and slowpokes in the intersection

// N-S red lights are already on

delay(5000); // Allow 5 seconds to clear the intersection

Paste Code Here:


Comments:

This additional Arduino code will also work for U.K operations.  Bytes used are now 1,756 for U.S. operation.  My coding may not be the most elegant or efficient, but it does the job.

“Walk/Don’t Walk” lights can be used for pedestrians instead of the train lights, or can be added for N-S pedestrians in Oregon City.

The train engineer won’t know (when he sees the N-S traffic red light) if all traffic lights are red or if just the N-S lights are red.  He may have to hold the pushbutton through the E-W green, E-W yellow and All Red lights to get permission to enter the intersection.  Once the green permission signal comes on he can release the pushbutton.  Local train procedures and practices, given loco speed and the number of trailing cars, should determine what the proper green permission signal length should be.

I don’t have one of Geoff’s IR detectors for experimentation, but I think using one for complete automation of this intersection is an excellent idea.

The combined circuitry on my breadboard (Arduino and 28 LEDs) draws about 40ma from my 12-volt DC supply.

Is that track in the middle of Oregon City Main Street for a streetcar instead of a train?  Servicing the “Happy Lounge”?

Gene

P.S.  I’m a fan of your “Brooklyn: 3 AM” layout.  http://www.carendt.com/small-layout-scrapbook/page-87-july-2009/  I’m going to try to duplicate the All-Foamcore “eggcrate” design for my own 2’ x 4’ module out of 508mm x 762mm x 5mm thick (20” x 30” x 3/16”) Elmer’s Foam Board, available at WalMart for $1.77 each.

Reply 0
Prof_Klyzlr

Ard Pro Mini VS 555 : just had to check

Dear Dr Geoff,

I saw your comments

Quote:

The Pro Mini is so cheap that I use it even to blink a single crossing signal, because even a home built 555 timer circuit will cost me more! I don't think I've completely adjusted to this reality yet.  

and just had to do some checking...
(not because I doubt the wisdom which is Dr Geoff,
but some modellers just find it easier to code "in hardware" than "in software"...)

from ebay.com.au, with the stated aim of "...a single LED flasher unit..."

Start with:

- Arduino Pro Mini @ AUD$2.72 + free shipping
http://www.ebay.com.au/itm/Pro-Mini-atmega328-5V-16M-Replace-ATmega128-Arduino-Compatible-Nano-module-CM-/271659193507?pt=AU_Gadgets&hash=item3f402654a3

Compare to:

- 555 based timer circuit @ AUD$1.77 + 0.50c shipping
(complete, 12VDC supply capability, and relay output)
http://www.ebay.com.au/itm/NE555-Delay-Timer-Relay-Switch-Module-Adjustable-0-to-10-Second-Inpute-DC-12V-/131044321757?pt=AU_B_I_Electrical_Test_Equipment&hash=item1e82d9a9dd

​or in component form (needs 1x timer + 2x resistor + 1x capacitor)

- NE555P timer (qty 10) = AUD$1.00 + free shipping
http://www.ebay.com.au/itm/10Pcs-NE555P-NE555-DIP-8-Single-Bipolar-Precision-Timers-IC-High-Quality-H-/271494067812?pt=AU_B_I_Electrical_Test_Equipment&hash=item3f364eb664

​IE AUD$0.10 per 555-timer

​- 1K ohm 0.25W carbon resistor (qty 10) = AUD$1.44 + free shipping
http://www.ebay.com.au/itm/10-pcs-Carbon-Film-Resistors-1-4W-0-25W-0-25-Watt-1000-Ohm-1KOhm-1K-Ohm-5-/180745413025?pt=AU_B_I_Electrical_Test_Equipment&hash=item2a154421a1

IE AUD$0.14 per resistor, each flasher will need 2x resistors

- 480uF 16V electrolytic capacitor (qty 5) = AUD$4.00 + free shipping
http://www.ebay.com.au/itm/470uF-16V-Electrolytic-Capacitor-5-Pack-AU-STOCK-/261460352487?pt=AU_B_I_Electrical_Test_Equipment&hash=item3ce0405de7

​IE AUD$0.80 per capacitor

= total cost for a 1sec on/off flasher (without the LED) of

555 timer @ $0.10
2x resistors @ $0.28
1x capacitor @ $0.80

= AUD$1.18

Conclusion, while the Arduino certainly gives far more flexibility in programming and end-result without having to change hardware (NB 5VDC is not a common "model RR layout supply voltage"!),

there's still plenty of "budgetary life" in the "old girl" 555-timer solution yet....
(and sometimes, it's useful to remember exactly how to design and build hardware circuitry... ).

Happy Modelling,
Aim to Improve,
Prof Klyzlr

Reply 0
Prof_Klyzlr

Prof's got no excuse now...

Dear Gene,

WOW! Thankyou soo much for this! I've already stockpiled enough Walthers "traffic light signal head" units for this project, so mow I have no excuse...

Many many thanks, I can "code in hardware" reasonably comfortably, and have had a measure of success professionally coding in mid-level "macro scripted" systems, but I must confess direct Ard/Droid coding holds a significant fear-factor for me.
(I have a current throttle/control project which would be oh-so-easy to accomplish in hardware,
only slightly harder ro accomplish with an NCE MiniPanel, 
but has me stumped at the moment as I want to try and get it working via Droid/JMRI...)

With your post, guess I've got no choice to "get thee to the workbench" and "have a go"...
(will definitely post if/when results are available...)

Happy Modelling and Thankyou soo much for the "get on with it" kick in the pants...
Aiming to Improve,
Prof Klyzlr

PS the trackage in the street served (past tense) the Blue Herron Paper Mill. Originally the track went beyond "Happy" and was run by Portland Traction Co. Later, SP/UP gained access to Blue Herron from the south, ran _thru_ the Mill, and used the now-truncated "track in Main St" as a switchback to access the Blue Herron "Long" and "Short" spur tracks. In the era we're considering for the traffic lights, that train signal is only visible from the sth side of the intersection, IE when the train (2x UPY SW1500s + max 4x 50' Railbox boxcars) is sitting in Blue Herron, awaiting a turn to do a quick "up and back" switchback move which crosses the intersection. For more info:

http://www.fogchart.com/Down/Yard/OC_Switcher.pdf

http://www.trainorders.com/discussion/read.php?1,2214803,nodelay=1

http://www.railpictures.net/photo/337927/

http://lothes.blogspot.com.au/2011/02/last-switch.html

Reply 0
LKandO

Long live the 555

And if you buy 100 and are willing to wait 30 days for delivery they can be had for about 2 cents each on eBay from China. Just gotta love a strong dollar!

Watch the clearance section of onlinecomponents.com. I recently bought 47uF 35V electos for a penny a piece in quantity of 500.

If you were pressed I bet the 555 circuit could be assembled for less than 25 cents.

Alan

All the details:  http://www.LKOrailroad.com        Just the highlights:  MRH blog

When I was a kid... no wait, I still do that. HO, 28x32, double deck, 1969, RailPro
nsparent.png 

Reply 0
Dave K skiloff

Love this stuff

Thanks for posting this, I will definitely be putting an Arduino towards this project (just need the traffic lights).  A bit of a funny story.  My sister and I were sitting around chatting after we had hammered out a few details of my Dad's funeral, and she mentioned she got a soldering iron starter set for Christmas.  I asked what the heck she was going to do with that (I've never known her to have any electronics interest whatsoever), and she said in her new job, she's been tasked with doing introductory classes to Arduino programming with kids!

I had no idea she was doing this now, but I showed her this thread and a couple of Dr. Geoff's threads, and now she is really interested in this and has more projects to do with kids.  We might work up a diorama for her to use as examples for her classes.  Maybe even get a kid or two interested in model railroading...

Dave
Playing around in HO and N scale since 1976

Reply 0
Geoff Bunza geoffb

No Wisdom, Just reality, just nits!

Normally, I would just let Prof's educated comments roll by, but there is a small matter of circuit board, voltage regulator, filter caps, power LED, solder, assembly time and the like. And my assembly time for a small, complete,working device that I can replicate at will very fast is worth at least 0.82 AUD,  maybe even 0.83 AUD !!

Also, gentlemen,  a crossing signal is a dual light flasher.

I think the real point is, you can do allot with the Pro Mini that rivals and beats piece part builds.

Now with that said, I am completely in favor of people building their own anyway they want if that's what they like to do!! For me-- it's not worth my time. I'd rather be modeling or doing other things.

Best Regards,

Geoff Bunza

 

Geoff Bunza's Blog Index: https://mrhmag.com/blog/geoff-bunza
More Scale Model Animation videos at: https://www.youtube.com/user/DrGeoffB
Home page: http://www.scalemodelanimation.com

Reply 0
Logger01

Discrete Component to Component Cost Comparison

Quote:

= total cost for a 1sec on/off flasher (without the LED) of

555 timer @ $0.10
2x resistors @ $0.28
1x capacitor @ $0.80

= AUD$1.18

Per component prices based on quantities of ten from known commercial distributor (lower prices for some components can be found on ebay):

NE555 $0.33

595-NE555P

AtTiny4 $0.69 //More than enough for a flasher//

556-ATTINY4-TSHR

AtTiny85 $1.198 //Evilbay for comparison. A lot more capabilities than the Tiny4//

5pcs NEW ATTiny85-20PU 8-bit ATMEL AVR Microcontroller Dip8 MCU uC - FROM USA

ATmega328 $1.80 //Evilbay for comparison. A lot more capabilities than the Tiny85//

NEW ATmega328P-PU with Arduino UNO bootloader ATMEL 2014 //Fully Arduino Pro Mini Compatible//

Caps $0.263

667-EEU-FR1C471

Caps $0.10

581-SA105E104M

Resistors $0.059

299-1K-RC

 

555 timer @ $0.33             AtTiny4 @ $0.69                AtTiny85 $1.198 ebay       ATmega328 $1.80 ebay
2x resistors @ $0.059        2x resistors @ $0.059       2x resistors @ $0.059       2x resistors @ $0.059
1x capacitor @ $0.263       2x capacitor @ $0.10        2x capacitor @ $0.10        2x capacitor @ $0.10

= US $0.711                       = US $1.008                        = US $1.516                    = US $2.118

For the average buyer the NE555 may still be ahead, but not by much. However, if we go the ebay route and given the capabilities of the Atmel microcomputers uPCs give you a lot more options for not much more cost.

And as Geoff noted, you still have to build and debug the thing.

Ken K

gSkidder.GIF 

Reply 0
Gene W.

Prof: Software Animation and more comments…

I know there are a lot of people with that “significant fear-factor” you mentioned when approaching the Pro Mini.

When I read Geoff’s excellent tutorial https://forum.mrhmag.com/post/scale-model-animation-8-starting-projects-and-virtual-rooms-for-modeling-12196257 I decided “Hey! I can do that!”

I immediately ordered 10 Pro Minis and the USB to TTL cable out of China in October of 2014.  Then I downloaded the free Arduino IDE (Integrated Development Environment) software and installed in on an old Windows XP laptop that had been damaged by a lightening pulse through the phone landline (it took out the network port).

Played around with the software.  My Pro Minis arrived the first part of November (about 21 days).  I soldered the headers on, poked it into my breadboard (Radio Shack Experimenter Socket 276-174) and started to brainstorm (that kinda hurt a bit).

I chopped up a string of cheap Christmas LED lights, soldered a 2.2k-ohm resistor to the cathode leg of each Led (the minus side).  The LEDs were all white – didn’t matter.  Then I made a crude drawing on a 3x5 index card of a two-lane street under repair, poked six appropriate holes in the index card with a pencil and shoved a LED into each hole, creating the scene as shown in Fig. 1 of my first posting.  It wouldn’t win any craftsmanship awards.  Following the example of the famous “Blink” sketch, I had my Fig. 1 scene working two weeks before Thanksgiving.  What a ride and what a rush!  Almost better than coffee.

What I’m a-saying is you don’t necessarily need to have any traffic lights mounted on your layout to test this Pro Mini sketch.

Lack of knowledge about programming seem to be the biggest hurdle for many people, but that programming knowledge won’t be increased if it isn’t done.  To help visualize what my sketch does, I have created the following animation.  If you print out my sketch (with the Oregon Street additions) and follow along with the animation, you can see how the sketch works.

nimation.gif 

The flippin’ pages are every seven seconds each.

The “Loss of power or Reset pushed?” is not part of the programming, but I wanted to show what happens if either event occurs.  The “Loss of power or Reset pushed?” can happen at any time and the Pro Mini actions will immediately revert to “Power Up” at the top of the picture.

Again, that may not be the most efficient or elegant coding, but I don’t care.  It works.

Comments:

555 Timer:

I remember the first time I constructed a 555 timer circuit.  The thing actually worked and I felt like a genius.  I hope you get that same feeling when you fire up your Pro Mini for the first time.

A single 555 timer and its associated circuitry may be cheaper than one Pro Mini, but it doesn’t have the Pro Mini’s versatility.  Changing the delay times on six or more 555’s to duplicate the Oregon City light sequence is certainly do-able, but I can change all the delays in the Pro Mini in less than 5 minutes.

Pro Mini:

Quote: “NB 5VDC is not a common "model RR layout supply voltage"!

The Pro Mini has two possible power options; a 5-volt input (5-volt model) on Vcc, or 12-volts on the RAW input.  A 7805 three-legged voltage regulator chip wired to Vcc should also do the trick to power the Pro Mini from 12 volts.  The specs say “up to 12 volts” on RAW, so I’m not sure what leeway there is.

My sketch, with the Oregon City additions, uses 1,756 bytes (of a 30,720 byte maximum), so there is plenty of room for more additions.  A “byte”, by the way, is one character in your sketch.  “Three_A”, without the quotes, is seven bytes.  Comments do not count because they are not loaded into the Pro Mini.

These are the ones I bought:

http://www.aliexpress.com/item/10Pcs-Lot-Pro-Mini-Module-Atmega328-5V-16M-For-Arduino-Compatible-With-Nano/1432639609.html

I selected these out of the many offered on that Aliexpress website because they came with those pre-cut headers. I found if I put the 90-degree header on top and the 10-pin headers on the bottom they fit my breadboard just fine, as you can see in the photo of my first posting.

By the way, that Aliexpress website takes a credit card, holds the money in escrow, and doesn’t release it to the vendor until you say so.  I’ve been using it for over three years without any problems.

Personal:

Prof, this rambling is not necessarily aimed at you, but its aim is to encourage everyone.

I am not an expert at anything except getting’ up in the morning, and certainly not an expert with the Pro Mini.  I’ve only played with one for about two months, and just wanted to share my feeble knowledge about it with anyone who may be interested.

Gene

P.S. I’ve been called a lot of things in my life, but I’ve never been called brief.

Reply 0
Graeme Nitz OKGraeme

Price Drop

Gene,

The price for the Item you listed has dropped and $18.49 for 10 ($1.849 each)!! How do they do it? Back when I used to have PCBs made for my job I would have been hard pressed to get just the PCB for less than $2.00 for that sort of double sided board even in quantities of 1000s.

Going to have to order some even though I don't presently have a layout (next year maybe) to put them on.

Graeme Nitz

An Aussie living in Owasso OK

K NO W Trains

K NO W Fun

 

There are 10 types of people in this world,

Those that understand Binary and those that Don't!

Reply 0
pierre52

12v Input

I too am a recent convert to Arduino also thanks to Dr Geoff's inspiration and assistance.

I too thought that 12v input could be achieved using the Raw pin .. But a word of caution the Pro Mini voltage regulator can only handle a very small current draw as I discovered when trying to run servos. I have used a 12v to 5v converter from EBay (China) that costs about $4 with free postage. It can handle up to 5 amps.

Peter

The Redwood Sub

Reply 0
Gene W.

12-volt RAW input…

Hi Peter,

You’re right.  The Pro Mini I specified has a small, 3-pin, surface mounted device marked “L05” on it.  I interpret that to be a 78L05 5-volt regulator.

That “L05” goes between the RAW input and the AMEGA328 processor.  The Vcc goes straight to the processor.  Even though most 78L05’s can take between 140-150ma, I don’t want to make-it-take-it because I have doubts that particular “L05” will.

For the “Oregon City” LED traffic lights I have offloaded most current from the Pro Mini with the 2N3904 transistors.  Each 2N3904 traffic light transistor will draw about 8.5ma from the Pro Mini, and the paired red or green train LEDs (no transistors) will take a total of about 20ma depending on how bright those LEDs are. 

Only one traffic light color will be on at any one time (8.5ma), and only one train color (20ma).  When the train has permission, the red traffic lights and one train light color will be on for a total of about 28.5ma maximum needed from the Pro Mini.  I felt comfortable, in this instance, using the RAW input.

The Pro Mini Vcc can take up to 200ma before it goes out in a blaze of glory.  http://arduino.cc/en/Main/ArduinoBoardProMini 

I would design for a maximum of 100ma at the Vcc pin.  Each I/O pin should be designed for 20ma or less, even though the specs indicate they can be pushed to 40ma each.

Thank you for pointing out the limitation of the RAW input for anyone designing his or her own circuit.  Offloading the current from the Pro Mini will result in its longer life.

Gene

Reply 0
atanisoft

another great deal for boards

I ordered via the link you gave above and about 20 mins later of hunting I found this on there as well:

http://www.aliexpress.com/item/Free-Shipping-10PCS-LOT-Pro-Mini-Atmega328-5v-16MHz-For-Arduino-Compatible-nano-uno/1735348583.html

I was also thinking of trying out one of these as another interesting item:

http://www.aliexpress.com/item/Geeetech-New-4-Channel-Relay-module-a-control-board-with-optocoupler-Relay-Output-4-way-relay/2026000566.html

I don't like the price on it as much but something like it might be useful for powering frogs in an automated fashion...

Reply 0
DBodnar

Two additional links for good prices

The 4 channel relays are also available at a better price here:

http://www.ebay.com/itm/181555597294?_trksid=p2059210.m2749.l2649&ssPageName=STRK%3AMEBIDX%3AIT

and the Arduinos here:

http://www.ebay.com/itm/261711842457?_trksid=p2059210.m2749.l2649&ssPageName=STRK%3AMEBIDX%3AIT

dave

Reply 0
Kevin Rowbotham

8-Channel for price of 4

8 channel Relay for about the cost of the 4 channel.

 

http://www.ebay.ca/itm/131354132789?ssPageName=STRK:MEWAX:IT&_trksid=p3984.m1423.l2649

~Kevin

Appreciating Modeling In All Scales but majoring in HO!

Not everybody likes me, luckily not everybody matters.

Reply 0
Reply