Traffic Lights Project - Step 1
In this step you will design driver circuits for the LEDs that represent the traffic lights, assemble them on the printed circuit board and verify that they operate correctly. More advanced users may argue that driver circuits are not necessary. Modern microcontrollers can swtich enough current at their general purpose I/O pins to drive a couple of LEDs. However, our LEDs represent traffic lights and in the real world a traffic light control system would have carefully designed driver circuits to turn the lights on and off. So, for added realism, we will have drivers in our project too. This will also save a lot of scrutiny of the data sheet of the microcontroller we choose to make absolutely sure it can handle the current of two LEDs. So, let's begin by learning about LEDs and designing the driver. You should have a basic understanding of electricity and the distinction between voltage and current.
(Use the menu on the left hand side to go back to the Traffic Lights Project Learning Steps or Introduction.)
Using LEDs
The interesting property of the Light Emitting Diode is that when you pass an electrical current through it, it emits light. So, LEDs are commonly used in electronic circuits as visual indicators. They are also used for illumination, although only recently have LEDs become available that emit enough light for use in flashlights, automobile lights and even road-side warning signs. LEDs that produce low levels of light are inexpensive and operate on low currents, hence they are ideal for our application in this project.
Note that to light an LED, we "pass an electrical current through it". We don't apply a voltage to it the way we do to a flashlight bulb. The reason is that the voltage across an LED tends not to change much with current. We must control the current to get the right amount of light and to not exceed the LEDs maximum rating, which can be as low as 20mA. We have to allow the voltage across the LED be whatever it wants to be. This voltage "drop" is in the region of a couple of volts and depends on the type of LED, particularly the color.
The simplest and most common LED circuit uses a series resistor. The value of the resistor in combination with the applied voltage sets the LED current. Consider, for example, operating a red LED, which "drops" about 1.7V, from two alkaline cells producing 3V. The resistor must drop the remaining 1.3V. Suppose we want the LED current to be 10mA, which is 0.01A. Ohm's law (R = V / I) tells us that the value of the resistor, R, must be the voltage (V) of 1.3 volts divided by the current (I) of 0.01 amps which gives a value of 130 ohms.
The current in an LED must pass from anode to cathode. An LED is, after all, a diode, which only passes current in one direction. (Unlike rectifier diodes, LEDs are not very good at resisting large voltages in the reverse direction, so this should be avoided.) The anode must be towards the positive side of the supply and the cathode towards the negative. A series resistor can go on either side. (We are talking here, of course, about conventional current flow, which was established as being from positive to negative. When it was discovered that current is for the most part carried by electrons it was too late to change this convention to correspond to the direction of electron flow.)
Before leaving the subject of LEDs, it is worth considering what happens if the supply voltage is changed. Supose we connect our red LED and 130 ohms resistor to four cells instead of two so that the voltage is 6V instead of 3V. Will the current also double? We said above that the voltage drop of an LED tends to be independent of current, so it will still be about 1.7V. This leaves 4.3V across the resistor. Another variation of ohm's law, I = V / R, allows us to calculate the current as 4.3 / 130 which is 0.033A, that is 33mA. The current has more than trebled and would likely burn out the LED! We need to watch out for this effect when we are using LEDs. The Traffic Lights Project Parts Kit contains two alternate values of resitors for use with low and high supply voltages.
Using Transistors as Switches
One way to look at a transistor is as a current amplifier. If you pass a small current into the base terminal, then a much bigger current will flow into the collector terminal. We can use this to switch a large current using a smaller current. Our LED driver circuit will switch the current in our "traffic lights" using the output of a microcontroller I/O pin. Once again, notice that we are talking about currents, not voltages. (A different type of transistor, the FET or Field-Effect Transistor uses a voltage as the control but our "bipolar" transistor uses current.) So, we will use ohm's law a lot!
We
can use one transistor to switch the current in two traffic lights as shown
in the diagram at right. The red lights shown to traffic arrving from
the North and from the South will be controlled together and therefore always
in the same state. Similarly, the yellow and green lights for North-South
traffic and all three lights for East-West traffic. In total, we will
need six LED driver circuits and six signals from the microcontroller to set
the states of the lights.
When the input signal to the LED driver (NS_RED in the diagram at right) is at a logic high voltage, current flows in the resistor R1 and into the base of the transistor Q3. This causes the transistor to allow a much larger current to pass into its collector from R13 and R4. These resistors set the current in the LEDS LR4 and LR1 so that they light at the right intensity. When the input signal is at a logic low voltage, no base current flows, the transistor does not allow current into the collector and the LEDs are off.
We will first select the values of the LED current setting resistors. A current of 6mA in the LEDs gives reasonable brightness for indoor use and keeps the total power requirement of the project low. What will be the voltage across the resistors? Let's take the example of a microcontroller with a 5V power supply. The voltage across the resistor will be 5V less the voltage drop of the LED and any voltage dropped from collector to emitter of the transitor. Although red LEDs drop about 1.7V, green and yellow LEDs drop a little more, so let's allow 2V. If we supply enough base current to the transistor it will go into "saturation", which means that it will pass all the current at the collector we want it to with a very small collector-to-emitter volltage of about 0.2V. So, subtracting 2V for the LEDs and 0.2V for the saturation voltage of the transitor leaves 2.8V across the resistor. From ohm's law, then, the resistor's value should be 2.8 / 0.006 = 467 ohms. The closest standard value is 470 ohms.
Next, we need to choose a value for the base resistor, R1, to make sure that the transistor is driven into saturation, as we assumed above, when the input is a logic high. We need the collector to pass 12mA, 6mA from each of two LEDs. We have chosen a 2N3904 transistor because it is inexpensive and widely available. How much base current do we have to supply so that this transistor will pass 12mA at the collector? The answer can be found in the data sheet. We must look for a parameter called "DC Current Gain", which has the symbol hFE. Several values are given for different collector currents and collector-to-emitter voltages. Fortunately, one is very close to what we need and tells us that the current gain is a minimum of 100 for a collector current of 10mA and a collector-to-emitter voltage of 1.0V. If the current gain is 100, we would need 0.12mA into the base to get 12mA into the collector.
Now, we don't need exactly 12mA. From the figures above, we need at least 12mA. Any extra current will just make extra sure that the transistor is fully turned on and saturated. The LED current setting resistors will limit the current to our 6mA design figure. So at this point we use our judgement as much as mathematics in selecting the value of the base resistor. The voltage from the base to the emitter of a transistor is about 0.7V (just like a forward-biased diode). We want the circuit to operate with a microcontroller supply of 3.3V, as well as 5V, so let's guess that a high logic level will be at least 2.4V. This leaves 1.7V across R1 and so to get 0.12mA of current its value should be 14kohms. If we round this down generously to the standard value 10kohms we will allow also for current gains of less than 100 at our higher collector current of 12mA and lower collector-to-emitter voltage. Even so, the current drawn from the microcontroller will be less than half a milliamp at 5V so there should be no risk of overloading its output pin.
The Resistor Color Code
Having chosen values for the resistors in the LED driver circuit, you need to shop for them or find them in the parts kit. Resistor values are marked with colored bands around the body and you will need to become familiar with the color code used to find the right parts. After a while, you will be able to read the value at a glance, just as you read entire words rather than each letter in turn. Until you reach this point, you will need to read each color, map it to a digit and use the digits to find the value.
The first trick is to find out which end to read from. To do this, look for a gold or silver band. This means that the tolerance (accuracy) of the value is 5% (gold) or 10% (silver). Now start reading colors from the opposite end. You should find three bands (before the tolerance band). Map them to numbers using the following table:
Black | Brown | Red | Orange | Yellow | Green | Blue | Purple | Grey | White |
---|---|---|---|---|---|---|---|---|---|
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
Keep the first two numbers you read and add after them the number of zeros equal to the third number. So, for example, if the bands are yellow, purple, brown and gold, the digits are 4, 7 and 1 so the value is 470ohms and the tolerance is 5%. Similarly, brown, black, orange, gold is 1, 0, 3 which is 10,000 ohms (10kohms) 5%. These are the two values you need if you are using the parts from the kit and your microcontroller runs from 5V. For a 3.3V microcontroller, the kit includes 220 ohm resistors to use instead of the 470 ohm resistors and these will be banded red, red, brown, gold.
Assembly and Soldering
Verifying Circuit Operation
When you've assembled the LED driver circuits, it is a good idea to check that they work. It's always a good idea to verify the operation of what you're building in stages. This is much easier than figuring out what is wrong after you've put it all together. In a commercial setting, the people who put together the hardware (which is what you've just done) hand it over to someone else to write the software (which is what you will be doing next). The software people tend not to be happy with the hardware people if the hardware does not work. Again, it's always best to check what you've done as soon as feasible.
You don't need the microcontroller system now, but you do need a source of power at about 3 to 6 volts, so you can tap this off of the microcontroller system if you have it. A DC "wall-wart" of a suitable voltage or a pack of four AA cells will also do nicely. Connect negative to pin 9 of the 10-pin microcontroller connector and positive to pin 10.
To test the LEDs and their driver circuits, connect a wire to the positive supply and touch it, in turn, to the pads P0, P1, P2, P4, P5 and P6 that are in a column to the right of the microcontroller connector. The LEDs should light in pairs, first green, then yellow, then red in the east/west direction and then the green, yellow and red in the north/south direction.
When you have assembled the traffic sensor push-button switches, you can test them by using a wire to link first P3 and then P7 to one of the other pads, for example P0. The LED pair should light, but then go out when you push the appropriate switches. With the link at P3, the east and west switches should turn off the LEDs and with the link at P7, it should be the north and south switches.
(Use the menu on the left hand side to go back to the Traffic Lights Project Learning Steps or Introduction.)