Sie sind auf Seite 1von 4

172 Harvard Road

Waterloo, ON N2J 3V3


Beginner Project 2
Connecting LEDs to propelled by a forward voltage The value of the series limit-
charge (see figure 1). Before light ing resistor must be calculated
PICmicro I/O Pins
is produced, however, the forward based on the maximum allowed
voltage across the diode must be LED current and the difference be-
So, you’ve got a great idea for
higher than the internal barrier volt- tween the applied voltage and the
a project using a PICmicro micro-
age of the diode. This point, la- LED’s voltage drop, +VF.
controller and you want to use at
belled +VF (VoltageForward) on the Like any other diode, LEDs
least one LED in your project as a
graph in figure 1, is the point at pass current in the forward direc-
power indicator. Or, maybe, you’ve
which the diode begins to conduct tion, but block current in the re-
set out to design a bar-graph volt-
and produce light. verse direction (see figure 1). What
meter with eight LEDs, or a circuit
It is important to notice that this means is that the LED will only
using seven-segment LED digit
once the voltage across the LED light up if connected with its cath-
displays, or a controller for a poster
reaches +VF the diode conducts ode on the negative side of the cir-
with blinking lights. Connecting
current extremely well. This action cuit, and its anode on the positive
LEDs to PICmicro I/O pins is a
is shown by the sharp rise in the side of the circuit. Too much re-
breeze as long as you know a few
forward current (+I) indicated by verse voltage will also destroy
basic rules, and Ohm’s law.
the near vertial line on the conduc- LEDs and diodes.
tion graph. The LED attempts to The cathode side of an LED is
LED Characteristics
clamp the voltage near +VF and can usually marked with a flat spot on
LED (Light Emitting Diode) be easily destroyed by an excess of the flange that rings the body of the
lamps are electronic diodes that voltage. To protect the LED, a se- diode. The cathode wire is also usu-
produce light. The light is produced ries current limiting resistor should ally shorter that the anode wire of
only when current passes through be added, as shown in figure 2, an LED. Figure 3, below, compares
the diode in the forward direction, below. an LED with its schematic symbol.

Figure 1. The LED conduction curve Figure 2. A typical LED circuit with Figure 3. An LED schematic symbol
shows the safe operating area. a series current limiting resistor. compared with an LED.

©1999 Sirius microSystems Connecting LEDs to PICmicro I/O Pins 1

Calculating the Series If you can’t easily find a 180Ω
Current Limiting Resistor resistor, round up to the next com-
monly available size (figure 2
We know from figure 1, and
shows a 220Ω resistor). Rounding
the discussion on the previous
up errs on the side of caution, in-
page, that a series current limiting
creasing the circuit resistance and
resistor is required to prevent ex-
decreasing the LED current. Al-
cessive current from destroying the
though the LED may not be quite
LED. But, how do you know how
as bright as with the calculated
much current is too much? A rule
resitance, you will not have to
of thumb is that the maximum safe
worry about damaging it with ex-
current for most LEDs is 20 mA.
cess current. Figure 5. An active-high LED drive
Figure 2 shows a typical elec-
trical schematic with a series cur- connection to a PICmicro.
rent limiting resistor in circuit with
Driving LEDs using a The advantage of an active-
the LED. This circuit is a voltage
Microcontroller high drive circuit like that the one
divider in which the diode voltage
in figure 5, above, is that it’s easier
and the resistor voltage must add TTL logic gates, like the 7400
for a programmer to follow the
up to the total applied voltage. As family, are better at sinking current
logic that lights the LED. Making
long as the applied voltage exceeds than sourcing current. What this
the PICmicro output 1, or a high
the +VF of the LED the voltage means is that the output transistors
voltage, turns the LED on. Mak-
across the LED remains fairly con- of a TTL device work better when
ing the output 0, or a low voltage,
stant (see figure 1, again). A good they connect a load to ground (0V)
turns the LED off.
digital multimeter with a diode test rather than to the +5V supply. The
function will let you easily find the circuit in figure 4, below, shows a
forward voltage of the LED. Once TTL-like drive configuration in
The Example Circuit
you know the voltage across the which a PICmicro I/O pin must be
LED, you also know that the volt- cleared (logic 0, or 0V output) to The example circuit on the
age across the resistor is the differ- turn on the LED. next page shows an active-high
ence between the applied voltage LED circuit, just like the one in fig-
and the LED forward voltage. ure 5, connected to each I/O pin on
The calculations, below, dem- Port B of the PIC16F84 PICmicro.
onstrate how to find the series re- Resistors R1 and R2, along with
sistor voltage. Once you know the switch S1, make up the reset cir-
resistor voltage and the maximum cuit for the microcontroller. Ca-
diode current, Ohm’s Law lets you pacitors C1, C2, crystal Y1, and
easily determine the size of the cur- resistor R3 form the clock oscilla-
rent limiting resistor. We’ll assume tor circuit that runs the PICmicro.
that the LED forward voltage drop The power supply circuit, built
is 1.4 Volts. around regulator U2 at the top of
the schematic, produces the 5V
Figure 4. An active-low LED drive
output needed to power the PIC-
connection to a PICmicro.
micro from a 9-12V input. A more
detailed explanation of the function
PICmicro microcontrollers are of the components in the reset,
constructed of CMOS transistors as clock and power supply circuits can
opposed to TTL transistor. CMOS be found in Beginner Project 1.
First, we determined that the circuits work equally well when Printed circuit board construc-
resistor voltage would be 3.6 Volts. connecting an output device to the tion drawings and a parts list for a
Next, Ohm’s Law lets us calculate high voltage side (+5V) as they do simple PICmicro LED output cir-
that a 180Ω resistor will limit the when connecting to the low volt- cuit can be found on page four of
current to 20 mA. age side (see figure 5). this project sheet.

2 Beginner Project 2 ©1999 Sirius microSystems

©1999 Sirius microSystems Connecting LEDs to PICmicro I/O Pins 3
Circuit Ideas
B2 Parts List
The simple PICmicro LED cir-
cuit shown in this project can be C1,C2 15pF ceramic capacitor

used in a variety of ways. Here are C3 0.1µF ceramic capacitor

just a few possibilities:
C5,C6 1µF, 25V electrolytic cap.
• LED sequencer/chaser
LED1-8 5mm (T1-¾) LED
• poster/display light controller
R1 100Ω, ¼W resistor
• analog bar graph level meter
R2 4.7kΩ, ¼W resistor

R3-R11 220Ω, ¼W resistor

Circuit Programming Parts layout diagram, top view. S1 Omron B3F1100 or equiv.

The PICmicro assembly code U1 Microchip PIC16F84-04/P

example shown below demon- U2 7805T 5V regulator
strates the steps required to initial-
ize Port B of the PIC16F84 for out- Y1 HC-49 4MHz crystal

put and how to output a pattern on Misc: Battery clip B1, or power sup-
the LEDs connected to Port B. ply wires; printed circuit board; regu-
This assembly code example lator mounting hardware.
was designed to be used in Micro-
Note: Any momentary contact
chip Technology’s MPLAB devel-
switch should work for S1. Y1 crys-
opment environment. Some
changes may be needed in order for tal should be “microprocessor” type.

the assembly code to function with Printed circuit board wiring diagram,
third-party tools. foil (bottom) side, top view.

;B2.ASM V1.0 Last modified on November 5, 1999

;Sirius microSystems provides this software on an “as is” basis, without any
;warranty, either expressed or implied. All Sirius microSystems software is
;provided for educational use only, and Sirius microSystems does not assume
;any liability for damages, either incidental or consequential, arising out of
;the application, use, or misuse of any of its software or hardware products.
;Sirius microSystems reserves the right, without further notice, to make
;changes to any of its software or hardware referred to in this program or
;library program in order to improve its function, design or reliability.


;This program demonstrates port output. It initializes all port B pins

;to be outputs and then places a pattern on port B to illuminate LEDs. MPLAB uses the list
and include directives
list p=16f84 to incorporate support
include <> for specific microcon-
org 00h ;Reset Vector location trollers.

Initialize bsf STATUS,RP0 ;Select memory register page 1 TRISB is the port
clrf TRISB ;Make Port B output by clearing TRISB control register for
bcf STATUS,RP0 ;Go back to register page 0
Port B.
Main movlw 01010101b ;Move 01010101b into W and
movwf PORTB ;output pattern to LEDs on Port B A sleep instruction puts
a PICmicro to sleep
Done sleep ;Stop executing the program
until it is reset

4 Beginner Project 2 ©1999 Sirius microSystems