Interfacing LEDs & Electromechanical Relays to Microcontrollers

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.

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.

Interfacing LED to a microcontroller –connected as current-sinking
Figure 1.0 Interfacing LED to a microcontroller –connected as current-sinking

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 LED to a microcontroller –connected as current-sourcing
Figure 1.1 Interfacing LED to a microcontroller –connected as current-sourcing

Interfacing Electromechanical Relays to Microcontrollers

The figure below shows the typical connection diagram for interfacing an electromechanical relay to a microcontroller.

Interfacing an electromechanical relay to a microcontroller
Figure 1.2 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.

Please follow us and share:

Author: John Mulindi

John Mulindi has a background in a technical field and he writes on topics ranging from automation, computer systems, embedded systems, mechatronics to measurement and control.

One thought on “Interfacing LEDs & Electromechanical Relays to Microcontrollers”

Leave a Reply