Jul 10 2008
Traffic Light Simulation using the Basic Stamp 2
So, it took me long enough right? I know in my last post I left off by saying that I would try to have at least 1 project that is build with the Basic Stamp 2 and include it on my next post. I came up with a Traffic Light simulation which I thought was a very practical application and I am very confident that everyone reading this blog is familiar on how a traffic signal works.
Initially, I thought about posting screenshots of the Homework Board circuit; but I felt that these types of projects would be best to demonstrate with a video.
Here is the circuit schematic that I have constructed using ExpressPCB which is absolutely FREE!
As you can see, the connections on the left represents the I/O pins of the Basic Stamp. Then there is a series connection of a 220 ohm resistor and the LED for the traffic signals and cathode or the minus (-) or the flat end of the LED is connected to GRND. I think it’s worth mentioning that when an I/O pin goes HIGH each circuit would have a source voltage of +5 volts and when the I/O pin goes LOW then the source voltage goes down to 0 volts. In this circuit configuration, the LED is configured as Active High which means that when I send a HIGH signal to an I/O pin, the LED is turned on and a LOW turns the LED off. According to the documentation for the Basic Stamp, each I/O pin can only source up to 20 mA of current if only 1 pin is connected to a circuit. However, if we are using a group of pins, we can only source up to 40 mA. So, we have to be careful not to supply our circuit with too much current or we can risk damaging our Basic Stamp that is the reason I included a 220 ohm resister to limit the current flowing through the circuit. Also, take into the account that the Basic Stamp Homework Board has built-in 220 ohm resistors as a safety feature but I didn’t want to take any changes so I added my own. But, if you don’t decide to use the resistor, then you LED would light up more brightly than in the video. Good. That is out of the way.
Ok. Now here is the code that I used to get all of this done. Remember, the Basic Stamp is programmed using PBASIC so the code should be very easy to read.
' ========================================================================= ' {$STAMP BS2} ' {$PBASIC 2.5} ' ========================================================================= ' -----[ Program Description ]--------------------------------------------- ' SIMPLE TRAFFIC LIGHT CIRCUIT SIMULATION ' Traffic Light Transition: ' RED ' GREEN ' YELLOW ' -----[ I/O Definitions ]------------------------------------------------- RedLED PIN 15 YellowLED PIN 10 GreenLED PIN 5 RedDuration CON 20000 '-- 20 sec GreenDuration CON 20000 '-- 20 sec YellowDuration CON 10000 '-- 10 sec ' -----[ Constants ]------------------------------------------------------- ' -----[ Variables ]------------------------------------------------------- ' -----[ EEPROM Data ]----------------------------------------------------- ' -----[ Initialization ]-------------------------------------------------- Reset: ' -----[ Program Code ]---------------------------------------------------- Main: DO '-- turn only Red light on for 20sec HIGH RedLED LOW YellowLED LOW GreenLED PAUSE RedDuration '-- turn only Green light on for 20sec LOW RedLED HIGH GreenLED PAUSE GreenDuration '-- turn only Yellow light on for 10sec LOW GreenLED HIGH YellowLED PAUSE YellowDuration LOOP END ' -----[ Subroutines ]-----------------------------------------------------
Hopefully, the code is strait forward. As you can see I set up some variables to hold the numbers of the I/O pins I am using. Then I have some variables to represent the duration that each LED will be turned on. Then if you jump down to the main body of the code, you would see that this code would run in a continuous loop and will never end until the battery is disconnected or runs out.
Then, you can see that I first turn on the Red LED by sending a HIGH to that I/O pin while setting all the other pins to LOW. Then I pause for the duration specified in the variable and then everything else is pretty cut and dry. I just turn on and off the other lights in the sequence that I want. Pretty simple huh?
Hopefully, this was helpful to you and you can see how easy Microcontrollers are to work with these days. I welcome any questions or comments you may have. Until next time…
Happy Coding!

