How to drive a relay using Microcontrollers

This post will outline how you can drive a relay using micro-controllers like AVR. Concepts are same for any other micro-controller used either in standalone mode or embedded in a development board like Netduino or Arduino.

Before we begin, I want to introduce the relay to you. If you already know, then you can skip this section.

What is a relay

A relay is an electrical switch that turns on or off, based on an external electrical signal. It is just like a normal switch that we see in our homes. The only difference is that instead of a human being switching it on or off, the switching is controlled via an external electrical signal. When the external signal is applied, the relay energizes and the switch is on, and when, the external electrical signal is removed, the relay is de-energized and the switch is off.

There are two types of relays – mechanical, that are based on a coil and solid-state. For most hobby work, you will deal with mechanical relays, since they are cheap and easily available as compared to the solid state relay.

Relay Symbols and Types

Relay Symbols and Types

The image on the left is a link to a wikipedia article on relay ( here ) . In most hobby shops you will find the SPDT (Single pole double throw) relay. The working principle is simple (look at SPDT picture on the left) – When the coil is not energized (meaning, no electricity passes through it), the points B and C are connected. When electricity passes through the coil, the point A and C are connected (electromagnet). Now, if you connect an LED between A and C, the LED will be off by default. When you energize the coil, point A and C are connected and the LED will light up. It’s that simple.

The picture below demonstrates the simple concept. To energize or de-energize a relay, you need to ensure that current flows through the coil or is stopped.

Drive LED from Relay

Drive LED from Relay

Before you connect a relay to a microcontroller, you need to first make a few decisions. Key ones are listed below:

1. What will be connected to the relay output and how much current is required for the relay output.

2. How much current is required to energize the relay and what is the voltage rating.

For most hobby projects, the output that the relay will drive would probably not be much, may be a few hundred milli-amperes or in some cases, a couple of amperes. The voltage and current required to energize the relay should also be in similar range (e.g. 5V DC – 12 VDC). Hobby grade or small relays, come in various shapes and sizes. You should pick the ones that can run on 5V (since you can use the same power supply as microcontroller). Most relays I have come across are rated 6V, but work fine on 5V too. You should also try to get PCB mountable relays , as they are easy to solder on the PCB boards.

Now, let’s take a typical hobby grade relay example. The image on the right is a link to Sparkfun website.

5V DC SPDT Relay

5V DC SPDT Relay

I hope this product continues for a long time till this post lasts and they do not take off the image 🙂 This is rated as 5VDC, 5A. This means, that the coil needs about 5V to energize and the points (A,B,C) can take a load current of upto 5A. One question remains, how much current is required to energize the relay. Some sellers will write the “Coil Rated Current” else check the datasheet. The rated coil current for this model as per the datasheet was about 40mA. Therefore, we need to provide 5V DC and a flow of about (max.) 40mA should be enough to energize the relay.

This is where, you now need to decide – is the microcontroller you are using capable of sourcing 40mA current? Probably not, and even if it is, it is not a good idea to directly drive the relay from a microcontroller.

This is where, the next item comes is – a power transistor. We will use a transistor as a driver to provide the required current to the relay. Ensure that the ratings of the transistor, far exceed the coil ratings of the relay (meaning, the CE voltage must be much larger than 5V DC and the collector current must be much larger than 40mA).

How to create the driver circuit

Once such transistor is BD 139. We will use this transistor as a switch. The microcontroller will provide the on/off signal to the base of this transistor. This transistor will be driven to saturation and full current will start to flow. The picture below outlines the circuit.

Relay Driver

Relay Driver

The transistor collector is connected to the relay coil. The transistor base is driven by a microcontroller pin. When the microcontroller wants to switch on the relay, it provides 5V (logic high) at its output pin. The output pin is connected to the base of the transistor. The transistor goes into saturation and current starts to flow from Vcc to ground (logically) via the coil and the transistor (CE). This energizes the relay and point C now switches and connects to point A, completing the LED circuit. If no signal is present, then the transistor is cut-off, no current flows through the relay coil and thus, point C is in contact with point B, keeping the LED off.

How to calculate component values

One crucial aspect to note here is what is the value of the resistor R. Consider it this way, if you are driving the point M, from a microcontroller that works on 5V (as these days, they also work on 3.3 V), then the drop across the resistance is 5V – 0.7V = 4.3 V (0.7 V drops across BE of the transistor).

If the hFE of the transistor (typical value) is 63 (see the datasheet of your transistor for this), then it means, that based current is amplified 63 times and that is the current flows in the collector. Now, for the given relay, we need about 40mA. Therefore, the base current should be:

Ib = IC/hFE
Inserting the values we get Ib = 40 mA/63
Or Ib = 0.6 mA

Now, you need 0.6 mA across 4.3 V, which translates to a resistance of about 7K.
Final point – 0.6 mA is what is needed at the base and that should be sourced from the microcontroller. Any microcontroller will be able to deliver this !

Note: You should put a diode between the coils (Vcc and collector) which is reverse biased to take care of reverse EMF while switch off.

Advertisements

6 thoughts on “How to drive a relay using Microcontrollers

  1. Thanks for sharing this information with us.This post has shared good concept of Relay.We can totally understand working of Relay,functionality of Relay ,importance of Relay,types of Relay.Relays are very important in sensing any kind of fault in power system.Larsen & Toubro also has some registered product.You may visit at Larsen & Toubro

  2. As microcontroller sends 1 in while loop. Will electromechanical relay work for long duration? Which one will suit best for home automation?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s