Home / news / Using relays with microcontrol /

A relay is a switch which is operated by electricity. It is usually electromagnetic device which has coil. When this coil is supplied with power, a magnetic field created and will operate mechanical switch. There are solid state relays which do not have moving parts but are very expensive compared to small mechanical relays. Pictures in this page shows typical small electro mechanical relay. Relay is used when we need to handle high voltages and currents through microcontroller operated system.

Relay coil's current requirement is usually more than 100mA (for small relay about 100mA) and microcontroller cant supply this much of current to relay by it self. So as shown in following schematic diagram, we have to use transistor to handle this current requirement. Base pin of NPN transistor used here is connected to ground pin via resistor to make sure that relay will stay off when microcontroller does not output +5V to transistors base. This will make sure only logic 1 on microcontroller pin will activate relay. It is better to use Darlington transistor to handle current requirement for relay, because darlington transistor can handle more current than single transistor.

It is essential to connect a diode across relay coil in reverse bias to protect transistor form back EMF created when relay is released from energized or ON state. If this diode is not used, transistor and/or microcontroller which is driving relay may be damaged upon releasing energized relay. The diode will short out the back EMF produced by coil when it turned OFF from ON state.

Following example shows schematic diagram and source code written in Mplab C18 to operate a bulb connected to AC power line. Program will blink the AC bulb.