Controlling a Relay

In this project, you'll learn how to connect and control a relay. A relay is simply a switch that is capable of turning on high-power devices such as 120V AC.

A relay is a switch that is controlled by an electromagnet. When the electromagnet is energized the switch is turned on, otherwise the switch is turned off. So controlling a relay is similar to controlling an electromagnet. The switch portion of the relay consists of three connections: normally open (NO), normally close (NC), and common.

Parts needed:
  • Arduino
  • Either a relay module with the transistor, diode and resistor already connected,
  • or a relay by itself with separate components:
  •   Transistor NPN 2N3904 or 2N2222
  •   Diode 1N4148
  •   A 22K Ω resistor (red-red-orange)
  • Wires
  • Breadboard
Arduino      Relay      Relay      Transistor      Diode      22K ohm resistor
1 The symbol for the relay is shown on the right. Notice that the left half of the symbol is the same as that for an electromagnet with the two connections for it.

If you have not already done so, you may want to first read about the electromagnet before you continue.

The switch portion of the relay has three connection terminals: NC for normally closed, common, and NO for normally open. When the relay is not energized (i.e. the electromagnet is turned off) the common terminal is connected to NC, and when the relay is energized (i.e. the electromagnet is turned on) the common terminal is connected to NO.
Relay symbol
2 Making the connections for the relay electromagnet

Follow the schematic diagram shown next to connect up the relay electromagnet. Relay connections
  • Be careful with the orientation of the diode and the transistor.
  • Connect a diode between the two ends of the electromagnet, with the negative side of the diode also connected to 5V.
  • Connect the C (collector) leg of the transistor to the positive side of the diode and also the electromagnet.
  • Connect the E (emitter) leg of the transistor to GND.
  • Connect the B (base) leg of the transistor to a 22K Ω resistor.
  • Connect the other end of the 22K Ω resistor to pin 2 on the Arduino to control the relay.
3 Code.
  • Send a HIGH signal to pin 2 to turn on the electromagnet. You should hear the relay switch click.
        digitalWrite(2, HIGH);
  • Send a LOW signal to pin 2 to turn off the electromagnet. You should hear the relay switch click.
        digitalWrite(2, LOW);
4 Making the connections for the relay switch
  • You can use the relay to turn on and off any electrical devices including high power ones.
  • Use the common connection on the relay, and either the NO or the NC connection to connect between the power source and the device that you want to turn on and off.
  • For this tutorial, we will just use the relay to turn on and off a LED. Make the connections between a LED and the relay switch as shown next.


Relay with LED connection