Sunday, March 30, 2014

Microchiping - Light Up An LED

Basically there are two ways to drive a LED using a microcontroller:

  • using direct drive (when the current needed by the LED is less then the maximum rated current of the digital I/O pin and the application doesn't need a stable light intensity).
  • using an external driver circuit (like a current source - when you need more precision/constant light intensity).


Direct Drive

In this case you connect the LED directly to the digital I/O pin, together with a current limiting resistor. The value of the resistor can be computed using formula (1), after you know the Vf (forward voltage) and If (forward current) of the LED (this values can be found in the datasheet).

R = (Vdd - Vf) / If     (1)

Our circuits Vdd is 5 V and the LED has a Vf of 3.3 V (typical) at If = 20 mA. The series resistor computed value is 85 Ω.

LED Datasheet Example
LED Datasheet Example

In the following schematic LED1 will light-up when the output pin value is '1' and LED2 will light-up when the output pin value is '0'.

Direct Drive Connection
Direct Drive Connection

If you want to drive more then a single LED, you will need more current (if you connect the LEDs in parallel) or more voltage (if you connect the LEDs in series). Probably this will exceed the capabilities of the microcontroller, so you'll need to use an external drive circuit (as simple as an transistor).

The code example will blink the two LEDs. Pay attention the way LED2 is light-up (by clearing the value of the associated bit from LATA register).

   void main(void) {

      // internal oscillator configuration
      // use INTOSC with Fosc = 8MHz
      OSCCON = 0x73;

      // ports configuration
      // bits 0 and 1 of port A configured as digital I/O
      ANSELA &= 0xFC;
      // bits 0 and 1 of port A configured as digital outputs
      TRISA &= 0xFC;
      // LEDs off - LATA.B0 = 0, LATA.B1 = 1
      LATA = 0x02;

      // endless loop
      // change the state of the LEDs every 1 second
      while(1) {

         Delay_ms(1000);   // wait 1 s (1000 ms)
         LATA = 0x01;      // turn on LEDs
         Delay_ms(1000);   // wait 1 s (1000 ms)
         LATA = 0x02;      // turn off LEDs

      }

   }

Note: code developed using <MikroC PRO for PIC>. For more information about MikroC IDE go to the dedicated article.


External Driver Circuit

External drivers are used in two scenarios: when you have to switch on more than one LED using a single uC output pin or when you need an exact light intensity.

To drive a group of LEDs using one single digital output you need a semiconductor switch (a transistor) and a power supply. For example if you have n LEDs connected in series the formula (2) for R and the schematic are presented below:

R = (Vext - (n Vf + 0.2)) / If,     (2)

                              n -number of LEDs
                              0.2 - voltage drop on transistor

Driving n LEDs Using One Single Digital Output Pin
Driving n LEDs Using One Single Digital Output Pin

Light intensity is directly dependent on the current that flows trough the LED. That makes current sources the best drivers for LEDs.

The next schematic contains a simple current source powering a LED.

Driving LEDs Using A Current Source
Driving LEDs Using A Current Source

One single digital pin required to switch the LED on and off. Value of R is computed with formula (3).

R = 0.6 / If     (3)

No comments:

Post a Comment