Arduino Relay Tutorial

Arduino Relay Tutorial

There are many relay boards available, each with some sort of driver device to protect the Arduino's outputs. Some have a driver transistor, while some have both a driver and an opto-isolator. Which you use depends on your comfort level. But you have to research a little in order to determine what you are getting. They generally don't say. Look for the opto-isolators to be much larger than a tiny little transistor.

The Arduino can supply enough current on the 5V line to power one or two relays at the same time, but I wouldn't try much more than that. If you need to have more relays on at the same time, power the relay board from a separate 5V power supply.

An Arduino Uno driving 1 relay on an 8 relay board.
Figure 1.
Arduino Uno and the 8-channel relay board.

The first relay board I used has 8 relays and provides a driver transistor and an LED for each. This is typical of the relay boards available. When you write a HIGH, or 5V, to the relay board input pin, the transistor comes on, energizing the relay's coil. When you write a LOW, or 0V, to the relay board input pin, the transistor turns off, relaxing the coil.

int pin7_Value = 0;

void setup() {
  pinMode(7, OUTPUT);

void loop() {
  pin7_Value = ~pin7_Value;
Figure 2.
Arduino code to toggle a relay on and off.

The code is fairly simple. A LOW is written to start, then the port bit is toggled every second from on to off or off to on.

An Arduino Uno driving 1 relay on a 4 relay board.
Figure 3.
Arduino Uno and the 4-channel relay board.

The second relay board I used has 4 relays and provides an opto-isolator, a driver transistor, and an LED for each. This is less typical, but a safer choice if you want to keep high voltage and low voltage systems separated. The code was the same, but these relays are energized by writing a LOW to the port pin, rather than a HIGH.

The opto-isolator has an led and a photo transistor in the same package. You can see the "large" black device above and to the left of the lit LED. With opto-isolators you can keep the power supplies for the relays and the Arduino completely separate - even the grounds can be separate.

The schematic shows how you can use a transistor to drive a relay. The transistor must be able to handle the current the relay requires. When the port pin D2 is written with a HIGH, the transistor will turn on, causing current to flow through the relay. When the port is written with a LOW, the transistor turns off, and the relay "relaxes", or turns off.

To determine the current requirement, measure the relay coil with an ohmmeter. Divide 5V by the result and you will get the current in amps. The 2N3904 shown has a maximum current of 0.2 amps. Typically larger relays require more current. Don't run it over 0.1 amps, and then only if you have good airflow. You can also use a power MOSFET or a larger transistor with a metal tab, like a TIP120, if you need to drive a larger relay.


When the relay is de-energized the magnetic field collapses and actually generates a high voltage across the terminals. This voltage will destroy the transistor if not blocked by the blocking diode, D1. Be careful with the polarity of the diode. Hooking it up backwards will also damage the transistor.

Arduino Board Logo


Arduino-Board is the go-to source for information on many available Arduino and Arduino-like boards, tutorials and projects.

Help and Support


Stay updated

Sign up if you would like to receive our once monthly newsletter.