Microcontrollers

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.

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.

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.

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.

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.

View Comments

Recent Posts

Rigid-Flex PCBs: Enhancing Durability and Flexibility in Electronic Devices

The world of electronics is constantly evolving, allowing designers and manufacturers to push the boundaries…

2 months ago

Key Features of PIC16X84 Family of Microcontrollers

PIC microcontrollers are fabricated by Microchip Technology. PIC16C84 and PIC16F84 are the two microcontrollers in…

9 months ago

ADD Instruction in Intel 8051 (MCS-51) Microcontroller

The ADD instruction tells the microcontroller’s CPU to add the source byte to register A…

9 months ago

Main Features of Intel 8086 Microprocessor

8-bit microprocessors are limited in their speed (the number of instructions that can be executed…

9 months ago

Basic Features of 68HC11 Family of Microcontrollers

The 68HC11 (also abbreviated as 6811 or HC11) is an 8-bit microcontroller that was introduced…

9 months ago

Process Synchronization using Semaphores

Mutual exclusion typically imposes some conditions on access to a given resource by two or…

10 months ago