16F LED Example

    PIC's can produce around 25mA on an output. This is much more than most logic outputs will deliver, and it helps to interface with many devices.

LED's are great as indicators, and are very easy to interface with.

Here's a simple program that will turn an LED on and off every 1 second in PICBasic ;

DEVICE = 16F877               ' Specify What PIC your using
XTAL = 4                      ' The Crystal/Osc Frequency

ALL_DIGITAL = True	      ' Make all pins digital

Symbol LED_1 = PortA.0	      ' Define a symbol in the program

TRISA.0 = 0	   	      ' Make PORTA.0 an output
PORTA.0 = 0		      '  and set it low (0V)

Main:

    If LED_1 = 0 then	      ' Check the status of the LED
        LED_1 = 1	      '  and toggle it
    Else
        LED_1 = 0
    Endif

    DelaymS 1000              ' Delay for 1 second

    Goto Main		      ' Loop forever

The wiring diagram is as follows

Note the PIC's power supply/oscillator are not shown

    Your LED will have a predefined forward voltage and current, which can be found from the supplier your buy it from. You need to use this forward voltage to calculate the series resistor required. Say for example, my LED had a Vf of 2.0 volts, and and If of 20mA(forward current) . This means that the series resistor needs to drop 3V and yet allow 20mA to pass through it. Simple maths allows us to calculate exactly what resistance we need.

Ohms Law: V = IR
Therefore R = V/I

And if R = 3/0.020
Then R = 150 ohms

    Now you know that the series resistor must be 150 ohms or greater to safely run your LED.

 

Where you can get the components;

  Skip Navigation Links.

Collapse Site Tutorial IndexSite Tutorial Index
Collapse 16F PIC Examples16F PIC Examples
LED's
Switches
Keypads
LCD's
7 Seg Displays
ADC
DAC
Expand MotorsMotors
Expand ThermometersThermometers
DS1307 RTC Chip
DS275
EEPROM's
RF Modules
RGB LED's
Code Snippets
Expand Code SnippetsCode Snippets
Expand 18F PIC Examples18F PIC Examples
Expand Handy TipsHandy Tips