In this article, we briefly describe how to interface LEDs and electromechanical relays to microcontrollers. Note, a detailed description on how this is done can be found in the official documentation of the microcontroller you are working with. Here, we only look at the fundamentals on how this is done.
Contents
Interfacing LEDs with Microcontrollers
The commonly used configuration to connect an LED to a microcontroller is illustrated in Figure 1.0 below. The LED glows when the microcontroller pin is driven LOW and OFF when the pin is set HIGH. The LEDs are connected in this manner as the current-sinking capability of microcontrollers is of the order of a few tens of milliamperes and the current-sourcing capability is of the order of microamperes. The resistor is used to limit the current through the LED.
The value of the resistance is selected as per the equation below:
R = (VCC โ VLED)/I
Where VLED is the voltage across the LED and I is the current.
The typical values of VLED and I are 1.5 V and 20 mA respectively.
If the current-sourcing capability of the microcontroller is sufficient to drive the LED directly, then LED is connected to the microcontroller as shown in the figure below. The LED in this case glows when the microcontroller pin is set HIGH.
Interfacing Electromechanical Relays to Microcontrollers
The figure below shows the typical connection diagram for interfacing an electromechanical relay to a microcontroller.
The NPN transistor is used to provide the desired current to the relay to the relay coil as the microcontroller cannot drive the relay directly. The freewheeling diode is required as the current through the inductor cannot be instantaneously reduced to zero. When the microcontroller pin is set HIGH, the transistor is switched ON. Current flows through the relay coil and the contact is closed. When the microcontroller pin is LOW, the transistor is switched OFF and the inductor current now flows through the freewheeling diode and slowly decays to zero value.
Related articles:
- The Basic Structure of Intel 8051 Microcontroller
- Polling and Interrupts in Microcontrollers
- Basic Features of PIC16F84 Microcontroller
- The Essential Features of Microcontrollers
Leave a Reply