# I want to control small scale traffic lights with my laptop

#### jordanwb

Joined Jun 22, 2008
30
I have a model railroad and I'm currently working on a new layout. I want to incorporate traffic lights (either with red/green or red/yellow/green), and I want to write a program that I can run on my laptop, and the laptop will control the order of the lights. I know I could use one of these, but they're expensive (and I'm cheap), albeit easier to use.

So since I already have the laptop, and programming knowledge, plus a bit of electronics, I'd rather go for that instead of the BOE-bot. Got any ideas on how to turn the binary signal from the Serial port into something I could use to control the lights?

I'm not very knowledgeable when it comes to electronic components.

#### beenthere

Joined Apr 20, 2004
15,819
First of all, the serial output is RS-232, not binary. Some receiver IC will be necessary.

With no more idea about the control methodology, I'd just be guessing. You could arrange for the serial word to latch into a register whose output determined the state of the lights. But things can get a lot more elaborate if you want to have some demand control on the lights.

If these are supposed to model block control lights, some means of sensing will be necessary. You may also find that using a microcontroller to do the lights makes more sense.

#### jordanwb

Joined Jun 22, 2008
30
Oh I thought it was binary. What I was doing was associating a number with a light in the traffic light. For example:

Traffic light for north: Red = 0, Yellow = 1, Green = 2
Traffic light for south: Red = 3, Yellow = 4, Green = 5

After the first paragraph you totally lost me.

#### beenthere

Joined Apr 20, 2004
15,819
That will work for one light. But you won't be able to sense if the train is in the control block, nor be able to control more than that one light.

What means did you propose to let the number control the lights, and how do they go from one state to the next?

#### jordanwb

Joined Jun 22, 2008
30
Well the train's location does not determine which lights are green or red, my laptop does. I know what to do on the programming end but I don't know what to do regarding translating the signal coming from the Com port into something I can use to turn lights on or off depending on what I send out the Com port

#### SgtWookie

Joined Jul 17, 2007
22,210
Take a look at MicroChip's PIC12Fxxx series. They're 8-legged critters that are cheap enough (about $1/ea) so that you could have one microcontroller for each traffic light. Basically, pin 1 gets +5v, pin 8 gets ground, pin 4 is reset, and the other 5 pins you can use to input and/or output signals. I have a PIC12F675 on a PICkit 1 programmer board sitting in front of me that's using 4 I/O pins to flash 12 LEDs in random patterns at this very moment. You could probably use a PIC12F629-I/P - those are$0.92/each if you buy less than 25 at a time. If you buy the PICkit 1 flash starter kit programmer for $36, it comes with all the hardware and software to get you programming in a hurry. If you go the serial route using an RS-232 port from your PC, you'll have to fool around with MAX232 IC's and interface logic or UARTS just to get the signals out of the computer and into your circuit. Then you'll need logic like latching shift registers to receive the data. Still, you'll need a clock that's accurate enough to synchronize with the RS-232 port. You'll probably spend more in just some logic IC's and making a custom board than you would for some PICs and the PICkit 1. You MIGHT be able to do something with the parallel port LPT1, if you have one, using QBasic. But, they are VERY easily damaged - and if you zap it, the only repair for a laptop is to replace the motherboard. At that point, you throw it in the trash and start over! The Parallax kit and Basic Stamp microcontrollers you were looking at are actually based on a PIC microcontroller. But, they're programmable in Parallax Basic. PICs are programmed in C and assembler; there's a learning curve to go through. But the PICkit comes with some sample code that you can modify for your use right away. There ARE BASIC-like compilers that you can buy for the PICs, but they are not cheap. Besides, the default.c and default.h sample programs that come with the PICkit 1 that the PIC12F675 is programmed with are just about what you need for your traffic light control program - just some code deletions and minor changes are necessary, along with a longer duration timing loop. #### Wendy Joined Mar 24, 2008 21,929 What I would do is before thinking of even the simpliest circuit is define parameters. How many traffic lights do you plan on controlling, are there other lights involved such as RR crossings? House lights? Figure worst case, then design from there. My approach would be to use the printer port, Sgt suggested I get a separate card for my computer to do similar experimets, it only makes sense. If you let the smoke out it will be something replaceable on your computer. From there you can use multiplex circuitry, probably simplier than decoding RS232, but this is an opinion. Thread Starter #### jordanwb Joined Jun 22, 2008 30 Hmm. I see. Well it seems that in terms of simplicity, and chance of messing up my laptop, the BOE-bot is the better way to go, albeit more expensive. Any other ways I might be able to accomplish what I want to do, perhaps without the laptop, like with a timer of some sort? #### SgtWookie Joined Jul 17, 2007 22,210 I just remembered Rob Paisley's site. He's a model railroader as well, and has a number of schematics, PCBs and kits for model RR control, including a 20-step stoplight sequencer. Here's his model RR electronics page: http://home.cogeco.ca/~rpaisley4/CircuitIndex.html Here's his 20-step stoplight controller page: http://home.cogeco.ca/~rpaisley4/20step.html The top half covers the basic circuit design. Halfway down, you'll see how to wire up the circuit for north/south and east/west traffic lights. This circuit is actually less complicated than what would be required if you were to attempt an RS232 interface from your laptop. The PCB is$12; you can likely save a good bit by buying the components from places like Mouser.com or perhaps Jameco or Electronics Goldmine; I've used all three and they don't have minimum orders.

You don't have to order a BOE-Bot to get going with Parallax' Basic Stamps. You could save a bit by ordering a Board of Education (about $100) and a Basic Stamp 1 (about$30). A BS1 would pretty much take care of your traffic light situation (with an inexpensive external driver IC like a ULN2003 or ULN2803), but it's a mighty expensive route to go. The least expensive Basic Stamp is nearly an order of magnitude more expensive than even upper-range PIC offerrings.