Arduino Tutorial: How to use a relay to safely control a high voltage

Motivation

Transistors are handy for controlling devices like motors and bright lights, even those running at a higher voltage than the 5 volts of Arduino (e.g. this), but they can’t be used for anything that plugs into a wall socket for two reasons:

  1. Even though transistors provide some degree of isolation (separation) between the low voltage side (Arduino) and the high voltage side (motor in this example), there are still some connections between the two. The voltage at a wall socket is high enough to kill you, and so it should be completely isolated from you (sometimes referred to as galvanic isolation)
  2. Transistors only work with Direct Current (DC), i.e. current that always moves in the same direction. The current coming out of a wall socket is Alternating Current (AC), i.e. current that keeps changing direction.

One device that can provide full electrical isolation and works with Alternating Current is a relay. A relay is a switch that is controlled by an electromagnet. When the electromagnet is turned on it pulls the switch closed (or open). A spring pulls the switch back to its original state when the electromagnet is turned off:

relayThe drawing on the left shows the relay in its resting state, with the electromagnet turned off or de-energized. The drawing on the right shows the relay in its activated state, with the electromagnet turned on or energized.

The switch in a relay may have multiple contacts; in the drawings above there are three contacts: The contact labeled 10, connected to terminal 1, is called the common contact. The contact labeled 11, connected to terminal 4, is called the normally closed contact because in the relay’s normal state, i.e. de-energized, the circuit between the common contact and the normally closed contact is unbroken, or closed. The contact labeled 12, connected to terminal 5, is called the normally open contact because in the relay’s normal state the circuit between the common contact and the normally open contact is broken, or open.

It is important that there is no metallic contact, i.e. no possibility of a closed circuit, between the electromagnet (the coil ending at terminals 2 and 3) and the switch. In this particular drawing, the pin labeled 9 might be made out of plastic. Thus there is complete electrical isolation between the coil and the switch.

(Note that while relays are necessary for working with dangerous voltages and/or  Alternating Current, they will work for low voltage and Direct Current as well.)

Relays are often sealed so it is hard to know which terminal goes to what. In the picture below, the relay has a legend printed on the outside:

sealedRelayMore typically you look at the datasheet for the relay to determine which contact goes to what. It’s a good idea to make note of this information:

DPDT-relay-real-life-component-wiring-setupNormally open and normally closed are often abbreviated NO and NC respectively; common is sometimes abbreviated COM or just C. Note that this relay has two of each contacts. In fact this relay has two separate switches inside, both moved at the same time by the coil. This common configuration is called Double Pole, Double Throw, or DPDT. This configuration is so common that even if you don’t need two switches it might be easier or less expensive to get a relay with this configuration. As we will see, you just use whatever contacts you need and ignore the rest.

We will use this relay, which in fact has the DPDT configuration:

173914The relay is very common and available from many sources, such as Jameco. The datasheet is here. As you can see on the datasheet, the relay has 8 pins.

  • Pins 16 and 1 are connected to the coil. Pin 16 is the positive terminal.
  • As before, this relay has two switches inside.
  • This relay has a black stripe above pins 8 and 9 to indicate orientation.

The safest way to use this relay is with a transistor and a protective flyback diode:

testing_FET_Relay_schem

As with a motor, the transistor (in this case an n-channel MOSFET) makes sure that the high current required by the relay does not draw more than the 40 mA the Arduino can deliver safely.

The diode (in this case a 1N4148) is necessary to prevent the reverse voltage generated by the collapsing magnetic field caused when the coil is de-energized from damaging the Arduino pins. A diode used this way is called a Flyback Diode.

The 10K Ohm resistor is to make sure the transistor is turned off before the Arduino pin  is set (using pinMode) as an output. In this case the output is neither HIGH nor LOW and it might “float” up to a voltage high enough to turn on the transistor.

Sometimes you will see see circuits that don’t use a transistor or a flyback diode, so I’d like to explain why this can work:

In our particular case, we can see from the datasheet that the relay draws exactly 40 mA so we can drive the relay directly from the Arduino output pin. Similarly, we can drop the flyback diode if we believe that our Arduino can withstand the reverse voltage generated by this small relay. We should point out that by removing these safety features there is a risk of damaging the Arduino. (Experience and understanding the theory is often the guide here; until you gain that experience it is safest to use the transistor and diode.)

For example, suppose I am confident that my Arduino can tolerate slightly more than 40 mA occasionally, so I’ll skip the transistor, but I still want protection against reverse voltage. This is how we will experiment with the relay:

arduino5VDipRelay_schemarduino5VDipRelay_bbNow that we know how to control the relay, let’s use the relay to control something. Let’s start with low voltage so that we don’t have to worry about safety: We will use a 9 volt battery to represent the high voltage, and a motor to represent the lamp or whatever we want to control:

arduino5VDipRelay9VBatteryMotor_schemarduino5VDipRelay9VBatteryMotor_bbOnce you understand how to use the relay in a safe situation, you can replace the battery with a wall plug and the motor with a wall outlet.

Important: Wall outlet voltage (or Mains voltage) (120VAC or 220VAC) can kill you. If you do not know what you are doing, don’t do this. Do not work on any part of a project while it is plugged into the wall – unplug it first!

Some good resources for this are:

If you want to explore light dimmers and much more complicated control of Alternating Current, this Instructable on light dimmers is great.

 

 

Print Friendly
Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

Leave a Reply

Your email address will not be published. Required fields are marked *