Watch the Video Tutorial

Light Emitting Diode (LED) is a semiconductor light source, when forward biased, it emits light. 
LEDs are used mainly to indicate the status of electronic circuits, for example to indicate that power is on or off but nowadays they are used in many applications including lighting and beam detection. 
Early LEDs emitted low-intensity red light, but today high brightness and many colour LEDs are available (white, blue, red, green, yellow or even infrared). 
LEDs have many advantages over the traditional lights (the incandescent and neon light bulbs)  such as: low voltage of operation, very low  energy consumption, smaller size, longer lifetime, available in many colours etc. In many energy efficient applications, the LEDs are tending to replace the traditional light source.

In this article we will learn how to connect and switch on and off various LEDs to a microcontroller using the MikroC Pro for PIC Compiler.
This is the simplest project a beginner in embedded programming can start with before attempting any complex projects as we have learned from the Introduction to MikroC Pro Compiler for PIC article.

                                           Connecting LEDs
An LED is similar to a diode, it has two legs: the longer leg is the anode (+) and the shorter leg the cathode (-). The cathode is also identified by a flat side on the body. 
The intensity of the light emitted by an LED depends on the amount of forward current passed through the device but we must take attention not to exceed the maximum allowable forward current or draw more current than the PIC output pin can handle. A PIC can source or sink 25mA of current per Input/Output pin.
When designing an LED circuit, we have to know the typical voltage drop, table 1 below lists some few characteristics of some LEDs.

Colour Typical Voltage Drop Typical Forward Current
Red 2.0V 20mA
Orange 2.0V 20mA
Yellow 2.1V 20mA
Green 2.2V 20mA
Blue 3.0V 20mA
Infrared 1.2V 50mA

            Table 1: Typical LEDs Characteristics

Most LEDs have a typical forward voltage drop of about 2V, with a typical operating current of around 10 mA (it is always good not to operate a device at its high end current), it’s important to read the datasheet to get the correct values. 
An LED can be connected to a microcontroller in two different ways: in current sourcing mode (figure 2) or current sinking mode (figure 1).

LED connected in current sinking mode       LED connected in current sourcing mode

  Figure 1: LED connected in current sinking mode     Figure 2: LED connected in current sourcing mode

In current sinking mode, a logic LOW (output 0) has to be applied to the connected pin for the LED to switch on while in current sourcing mode a logic HIGH (output 1) has to be applied to the pin for the LED to switch on.

The port output voltage can be assumed to be +5V when the port is at logic HIGH. Assuming that the LED is to be operated with 10mA forward current, and that it has a forward voltage drop of 2V, we can easily calculate the value of the current limiting resistor as:
                                                    

As the PIC can supply up to 25mA, the current can be increased for more brightness. We are going to choose a resistance of 220Ω (forward current of about 13.6mA) in our example but a resistance of 330 could be also do the job very well.

Digital Input/Output

On Figure 1, the pin diagram of the PIC18F45K22 reveals that there are some pins which have more than one name, or more functions to be specific. That is because pins can serve more than one purpose and it is up to the designer to decide how each pin is configured. For example Pin 2 of the Microcontroller has the names: RA0/C12INO-/AN0. RA0 means the pin can be used as a digital Input or Output pin depending on its set direction, it is a general purpose PORTA.0 pin. C12IN0- means the pin can be used as a comparator, it is Comparators C1 and C2 Inverting Input. AN0 means the pin can be used as an analog input pin. It is Analog input 0. They are 5 registers that control the Input/Output PORTS, the TRIS register (data direction register, to choose if a pin is input or output), the PORT register (reads the levels on the pins of the device), the LAT register (output latch), the ANSEL register (analog input control) and the SLRCON register (port slew rate control).

ANSEL Register

The ANSEL register control whether or not which analog chanels are operational. Each bit in the register controls one pin as outlined in the register tables. A ‘0’ sets the pin to digital mode and a ‘1’ sets the pin to analog mode. For example, if you make ANSELA = 0x02 it will configure only RA1 pin as analog (AN1) or ANSELC = 0x00 it will Configure the whole PORTC pins as digital while ANSELD = 0xFF will configure the whole PORTD as analog (AN20 to AN27). Reading a pin that is set to analog mode will return a ‘0’.

 TRIS Register

The PORT bit Direction is set with the TRIS Register. TRIS stands for Tri-State, which determines the direction of each General-Purpose Input/Output pin. Logic 1 at a particular bit of TRIS register makes the corresponding pin Input and Logic 0 at a particular bit of TRIS register makes the corresponding pin Output.

Example:

The LAT Register

The LAT register is used to write data to the General-Purpose Input/Output pins. Logic 1 at a particular bit of PORT register makes the corresponding pin at Logic High (VDD) and Logic 0 at a particular bit of PORT register makes the corresponding pin at Logic Low (VSS) if that pin is an Output pin (TRIS bit is 0).
Note that you can use the PORT Register as well it is going to work but it is not a reliable way to write to a PORT, the best way is to use the LAT. Always use the LAT if the microcontroller has it (note that the PIC 16F series don’t have the LAT register so you will have to use the PORT register to write to a PORT (Output a value) and the PORT to read from the PORT).
Example:

The PORT register

The PORT register is used to read data from or write data to the General-Purpose Input/Output pins. Logic 1 at a particular bit of PORT register makes the corresponding pin at Logic High (VDD) and Logic 0 at a particular bit of PORT register makes the corresponding pin at Logic Low (VSS) if that pin is an Output pin (TRIS bit is 0). The PORT register can be used to read digital data from an Input pin, like reading the status of a switch for example. Logic 1 indicates the pin is at Logic High and Logic 0 indicates that the pin is at Logic Low.

Example:

Writing to the Entire Register

You should be familiar with following C Programming concepts.

  • A number with a prefix ’0b’ indicates a binary number.
  • A number with a prefix ’0′ indicates an octal number.
  • A number with a prefix ’0x’ indicates a hexadecimal number.
  • A number without prefix is a decimal number.

Example: 

Writing to an individual Bit of a PORT

You can simply use the direct member selector (.) with a variable, followed by one of the identifiers B0, B1, … B7, F0, F1, … F7, with F7 being the most significant bit.

Examples:

 Delay Functions

A delay function is used to create a delay in the program, if let say you need a 1 second delay between  the ON and OFF of the LED, it’s easier to use a delay function to generate a 1 second. MikroC Pro for PIC provides a set of built-in delays. You can use these days in any part of your project and you don’t need to include any header file.

Delay_us: This routine creates a software delay in duration of time in microseconds (a constant). Range of applicable constants depends on the oscillator frequency.  Example: Delay_us(100);  // 100 microsecond pause

Delay_ms: This routine creates a software delay in duration of time in milliseconds (a constant). Range of applicable constants depends on the oscillator frequency. Example: Delay_ms(1000);  // 1000 millisecond pause which is the same as 1 second.

MikroC Code – Blinking an LED

In this example, we are going to write a simple program to switch on and off an LED connected to PORTB.0 at an interval of 1 second:

An LED connected to PORTB.0 of a PIC18F45K22 Microcontroller

Figure 3: An LED connected to PORTB.0 of a PIC18F45K22 Microcontroller

You can download the full project files (MikroC project) below here.

All the files are zipped, you will need to unzip them (Download a free version of the Winzip utility to unzip files).  

Download Introduction MikroC project