The blinking LED experiment is an introductory application that will provide the novice hardware experimenter experience with the SUPERPROTO board and some fundamental electronics concepts. This circuit allows an output port of the 6522A VIA to control an LED. The same basic concepts can be used for many real time control applications. Other items that could be controlled through transistor switches include motors, solenoids, speakers, relays and so on. Be aware that motors and other high amperage components may require heavy duty MOSFET transistors or relays inserted in the circuit instead of the basic 2N2904.
The blinking LED project is controlled by the 6522A Port A bit 0, which is pin 2 on the 6522A. Here is the schematic:
In addition to the basic SUPERPROTO card, the experimenter needs to provide the following components. All of these components can be found a typical electronics supply store, such as Radio Shack in the USA.
- 2n3904 transistor or equivalent bipolar NPN transistor
- resistor 1 - value can be determined using equations described below
- resistor 2 - value will be determined using equations described below
- LED - Any low current LED can be used - it is helpful if you know the current rating of the LED that you are using
- LEDS - The LED illuminates when a specific current passes through it. Too little current and the LED is off or isn't very bright. Too much current and the LED will be destroyed. There is a voltage drop across the LED when the LED is in a circuit. A typical LED has a voltage drop of around 2 volts. It is helpful, if you can find the specification of the LED that you are using in this experiment. If you don't know the specifications, but you think you have a typical LED assume 20 milliamps current with a 2 volt drop.
- 6522A output port A - When configured for output, the 6522A output port A can source up to 10 milliamps of current. Sourcing more current than this, may destroy the 6522A. Sourcing less current will not cause harm and is preferable to using the port at maximum rating. Note that if you have a LED that requires less than 10 milliamps, you could drive it directly from the output port, without using the transistor. This configuration assumes that you connected the correct current limiting resistor into the circuit.
- 2N3904 Transistor - A bipolar transistor is a current amplifier. There are three connections on the 2N3904 transistor.
- The base - the left side connection to the transistor in the schematic.
- The emitter - bottom transistor connection (with the arrow) in the the schematic
- The collector - the top transistor connection in the schematic
The current going through the base to the emitter is multiplied by the hFE rating of the transistor. That is amount of current allowed to pass from the collector to the emitter. Less current could flow, depending upon other characteristics of the circuit, but not more. The hFE rating is a defined as a curve, depending upon collector current which is the amount of current flowing through the top connection. The 2N3904 is specified to have a hFE of 100 at 10 milliamps.
Like a diode, there is a voltage drop between the base (left side in schematic) and the emitter (bottom) when a transistor is fully switched on. It is about .6 volts.
Voltage Drops in the Circuit
Using the known voltage drops of the transistor (if fully turned on) and the LED, we can calculate the voltage drops of the resistors R1 and R2 using simple math.
- The drop across R1 is 5(5 volt supply)-2(LED) = about 3 volts.
- The drop across R2 is 5 (output of 6522A) - .6 (transistor) = about 4.4 volts. In reality the output of the 6522A will be a little less than 5, but for purposes of this experiment, we can assume 5, as the result will be close enough.
How To Select Resistor Values
For purposes of this discussion, let us assume a 20 miliamp LED. We want about 20 milliamps flowing from top power supply input, through the LED, through resistor R2, through the collector of the transistor and out the emitter and to ground. The current gain of the 2N3904 is close to 100 at 10 milliamps, which indicates that we need at least 20 milliamps divided by 100 or 200 microamps flowing from the base (6522A output) to ground in order to light the LED.
- Resistor R1 needs to be set such that at least 200 microamps flows in order to switch on the transistor to light the LED, but not more than 10 milliamps, so the 6522A port isn't burned out. Since we have a range of values to select from, lets set the base current something in the middle, 1 milliamp. Providing more current than required on base to emitter of the transistor will do no harm, and ensures that the current flowing through the LED is not limited by the transistor, as we want to limit that current by resistor R2. We can use ohms law to calculate the resistance. Ohms law is:
current in amps = voltage / resistance in ohms
Since we have current and voltage we calculate resistance by:
resistance = voltage/current
4.4 volts/.001 amps = 4400 ohms. 4400 ohm resistors are not common, so a 4700 (4.7K) ohm resistor can be substituted. For this application, 10K resistors are commonly used and would result in a current of 4.4/10000 or 440 microamps, which still fits within our minimum and maximum values.
- Resistor R2 needs to be set to limit the flow of current through it to 20 milliamps to match the current required by the LED. We already know the voltage across R2 and the current. Once again, we can use ohms law to calculate the resistance. We can calculate resistance by:
resistance = voltage/current
So 3 volts/.020 amps = 150 ohms.
One you have calculated the resistor values, you should gather together the needed components. If you want, you can use the breadboard extension. described elsewhere on the SUPERPROTO wiki to construct the experiment. This will allow you to do more experiments in the future, without risking damage to the SUPERPROTO board.
Duplicate the connections in the schematic with the real components in order to build your circuit. Note that the Transistor and Diode must be connected correctly. See the image of the transistor to determine correct pin for emitter (connected to ground), base (connected through resistor to 6522A) and collector (connected through resistor to LED).
The long lead of the diode (anode) must be connected to the power supply. The short lead connects to the 150 ohm resistor.
The LED can be blinked by simple monitor commands, or a program can be used to control the LED. Note that the Diagnostic Program described elsewhere on the SUPERPROTO wiki will automatically blink the LED.
To control the LED using the monitor, you must change the 6522A port A, bit 0 setting, to output mode. If you are in BASIC call - 151 to get to the monitor prompt. Write a one to output port control register bit 0, to set the output port, bit 0, to output mode. The address is C0X3 where X is a hex number computed by adding 8 to the slot number. So if installed in slot 7, execute the following command to set port A, bit 0 to output mode.
Now to turn the LED on write a bit 1 to the PORT A, data register, bit 0.
Now to turn the LED off again, write a 0 to the PORT A data register, bit 0.
Note that with the 6522A in input mode (default), the output will float high, which may source enough current to turn the LED on.
Compare Curcuit Calculations with Actual Performance
If you measure voltages on each end of the two resistors, you can calculate actual current to expected. I did this on my prototype and found the following voltages. The first value when the LED is turned off and the second value when the LED is turned on.
The values with the output circuit off reveal a few things.
- Resistor 1
- The 6522A doesn't bring voltage completely to ground, but down to 50 millivolts. This is within spec as the 6522A specified maximum low voltage is .4 volts.
- There is no current flowing across resistor 1 or from the base of the transistor to the emitter
- Resistor 2
As expected there is no current flowing across R2 (and the LED). This can be calculated with ohms law.
current in amps = voltage / resistance in ohms
Voltage is 3.85 - 3.85 = 0. 0/150 ohm = 0 AMPs.
The values with the LED circuit on reveal a few other things.
- The 6522A actual outputs 3.54 volts when turned on, which is quite a bit less than the 5 volts that we used in our calculations. The 6522A VIA, has a specification for min high output voltage of 2.4 volts, so it is actually performing within spec
- The voltage drop from the base to the emitter of the transistor is actually .762 volts, a bit more than the .6 volt drop we used in our calculations. The 2N3904 has a specification for max Base/Emitter saturation voltage of .94 volts when current is 5 milliamps, so it also appears within spec
- Because of the difference in value from expected, the current flow through R1 is actually less than calculated. Using ohms law, and the voltage (3.54-.762 = 2.778) and resistance of 4700 ohms gets this result.
2.778/4700 = .000591 AMP or 591 microamps This is still more than enough to fully turn on the transistor. We had calculated that 200 microamps would be enough to turn it on.
- The voltage drop of this particular LED is a little more than expected
5 volts input - 2.75 = 2.25 volts drop over the LED
- There is a small drop from collector to emitter of the transistor of .121 volts, which we didn't account for in our equations. The 2N3904 has a specification for max Collector/Emitter saturation voltage of .2 Volts, so it is performing within spec.
- Current flow of the LED can be calculated with ohms law by calculating current flow of the 150 ohm resistor. Note all that all current flowing through the LED must also flow through that resistor, so current flow is essentially the same for both
2.75-.121 = 2.629 volts / 150 ohms = 17.52 milliamps
All in all, though the circuit did not perform exactly as expected in our rough calculations, it performs acceptably and drives the LED with a current of 17.5 milliamps when turned on. Though the rough technique we used for calculating resistor values worked, if parameters are not closely examined when designing a circuit, it may not always function properly. Professional engineers carefully examine all parameters when designing to make sure that the design always will work, even in worst case conditions.