Hardware Design of the Traffic Lights Project
Transistors as Switches
Each transistor switches the current in two LEDs. So, the collector current when the transistor is ON will be 12mA. We must supply enough current into the base to keep the transistor in saturation. From the data sheet of the 2N3904, we can see that the current gain is a minimum of 100 at IC = 10mA and VCE = 1.0V. This is not quite what we want to know, but it's very close. We'll continue with a current gain of 100 and allow some extra base current to get deeper into saturation later when we choose the resistor value. To get 12mA at the collector with a current gain of 100 we must supply 12 / 100 = 0.12mA at the base. Assuming that the control input goes all the way up to the supply voltage when the LED is to be on, the voltage across the base resitor will then be 3.3 - 0.7 = 2.6 volts. Ohm's Law tells us that the resistor value must be 2.6 / 0.00012 = 21,667 ohms. However, we don't select the nearest standard value, which is 22kohms. What we have calculated is the maximum value of the resistor that will produce 0.12mA of base current to saturate the transistor with a 12mA collector current. But, we can use a lower value to give more current and this is what we do. Somewhat arbitrarily, let's chose 10kohms.
Using Port Bits as Both Outputs and Inputs
What would happen if we connected a port bit from the microcontroller to both one of our transitor switches and one of our bush-button inputs? Would something be damaged? If not, could we do anything useful?
First, imagine that the port bit is configured as an output. As well as driving the base resistor of the transistor switch, the microcontroller now also has to drive the resitors of our push-button circuit. If the push-button is not pressed (open), then extra drive is needed to get the port bit low because the two resistors in series to VCC are trying to pull it up. If the push-button is pressed, then extra drive is needed to get the port bit high because one resistor and the closed switch to ground are trying to pull it down.
Second, imagine that the port bit is configured as an input. Can we still read the state of the push-button? When it is pressed, there can be no problem. The push-button grounds a resistor which pulls the port bit low and if the port bit is low, no current passes into the base resistor of the transistor switch. The problem can occur when the push-button is not pressed. Now there are two resistors in series trying to pull the port bit high, but they must supply current into the base resistor of the transistor switch as well. This extra load prevent the port bit rising all the way up to VCC. What we must do it make sure it is high enough that the microcontroller reads its input as a logic high.
Taking the Atmel ATmega16 microcontroller as an example, we see from its datasheet that the minimum voltage we must present to an input to have it read as logic high is 60% of VCC. Using the formula for a voltage divider, we can calculate that the sum of the two series resistors must be no greater than 6.667 kohms. If we have chosen 3.3kohms for the push-button pullup resistor, we'd be sailing a bit close to the wind to use 3.3kohms for the protection resistor too. Let's choose 2kohms, for a total of 5.3kohms. Actually, we've not accounted for the fact that the base resistor doesn't go directly to ground but goes into the base of a transistor. Thus, it goes only to about 0.6V. Taking 3.3V for VCC and calculating the actual port bit voltage with the components we've chosen, it comes out to 2.365V, which is 72% of VCC. This is a reasonable margin.
Of course, from the above you can see that if the push-button circuit and the transistor switch circuit are connected together the LED is going to come on. Unless, that is, the microcontroller's port bit is configured as an output. In that case, the microcontroller decides on the state of the LED. The trick, then, is to have the port bit work as an output almost all the time and only turn become an input for brief periods to read the switch. The tiny flashes of the LED will not be noticeable. When a human being is operating the push-button, this turning around of the port bit to take snapshots of its state is a very practical and useful tehnique.
(Use the menu on the left hand side to go back to the Traffic Lights Project introduction.)