How to Connect a Relay Board to a Raspberry Pi

by | May 31, 2016

In this tutorial I will show you how to connect a relay board to a Raspberry Pi, relays are an electromagnetic switch that allows you to control a high voltage electrical circuit by opening and closing contacts in another low voltage circuit. On the Raspberry Pi the control circuit will be operated by our GPIO pins. A relay generally has 3 connection points on the controlled circuit side, Normally Open (NO), Normally

Closed (NC) and a Common, an important convention to note is that when a relay contact is normally open (NO) the relay is not energized.

To configure the relay board with a Raspberry Pi I am assuming that you are running the latest version of Raspbian and have the ability to connect to your Pi through SSH with Putty and FTP with Filezilla, or directly with a keyboard and monitor, if you haven’t set-up your Pi yet then check out my getting started section.

I will not be connecting AC powered equipment to the output of the relay yet but we will be able to see that we have control by the led indicator lights on the circuit board itself. You could also use a buzzer or multi-meter to prove that the relays are switching.




In this tutorial I will be using the following materials:


When describing the physical pin connections I will be following the GPIO pin numbering convention shown below.




Warning: Working with AC current is Dangerous, Please exercise extreme caution and preferably consult a professional to make any AC Power connections. Always disconnect the circuit from the mains when not in use and mount in a secure, sealed enclosure to prevent accidental contact.


Connecting the Relay


The relay board that I am using is “ACTIVE LOW” which means that the relays are switched on when the inputs have a Ground (0 volts) connection applied to them while there are 5 volts connected to the Vcc pin.

Since the GPIO pins on the Pi output 3.3V when active we need a way to effectively short the input pins on the relay board to Ground when we activate the GPIO pin, the Transistor (2N3904)/Resistor (2.2kΩ, 10kΩ) circuit shown below will achieve this. It also ensures that when the Pi is shutdown that the relay does not stay energized when connected to and external 5V power source.

In this case, the transistor can be used as the equivalent of a switch which is controlled by the GPIO pin. When the GPIO pin is activated a voltage is applied to the Base (B) pin of the transistor, this effectively “closes the switch” and allows current to flow between the Collector (C) pin, to the Emitter (E) pin, completing the circuit from the 5 volt Vcc connection on the relay board to Ground.  The opposite is also true when the GPIO pin is deactivated there is no voltage applied to the Base (B) pin and this effectively “opens the switch” and breaks the connection to Ground from the relay board.

One of these circuits will be required for each GPIO pin to relay input that you are connecting.

2N3904 Transistor Pinout

Active Low input transistor circuit


To connect the relay module to your Pi you will need to make the following connections, I’ll be using GPIO pins 22-25 to cover 4 relays but you can use any spare pins.

  • Connect the 5V Pi pin to the Vcc pin of the relay board
  • Connect the Ground Pin of the Pi to the Ground pin of the relay board
  • Connect GPIO pin 22 to the input of the transistor circuit above and the output to IN1 pin of the relay board
  • Repeat the process above for any remaining relays (each relay requires a separate transistor circuit)
  • If not connected already then place the jumper between the JD-Vcc and the Vcc pin. (remove if you are using an external 5V power supply for the relay board)

It should be noted that each relay draws approx 72mA from the Pi when activated, given that the Pi itself required 500mA it is important to make sure that your power supply for the Pi can provide this current, a minimum of a 1.5 amp supply should be used to allow for a margin of safety. If you are connecting more than 4 relays then it is probably necessary to power your relay board with a separate 5V supply.


Note: Connecting the jumper between the JD-Vcc and Vcc pins removes the optical isolation provided by the circuit board but allows you to power the relay coils from the Pi. If you want full isolation then remove the jumper from JD-Vcc to Vcc and the Ground pin connection to the Pi. Connect a separate 5V power supply to the JD-Vcc pin and the Ground pin of the relay board.


Raspberry Pi with Relay Circuit Diagram

The Relay Control Code


Now that we have a circuit connected we can go ahead and add some code for our Raspberry Pi relay control and test the connections.



Now transfer the code to your chosen folder on the Pi using an FTP client and then run the program using SSH or from the desktop using a Terminal.



While there is no visible output from the program the led’s and relays should cycle on and off every 5 seconds until ctrl-c is entered to break the loop.

To ensure that it is working correctly when one of the relays is activated so that the led is on, use a multimeter to check that the output of the relay is active, shut down your Pi and the circuit should open. This will ensure that the transistor circuit is working correctly and in the event that your Pi loses power, whatever you are powering from the relay will also turn off.

All the python code is available on my Hydropi GitHub Repository.

All going well you have now configured your Pi to interface with a 4-Channel Relay Board.

If you have any thought’s about this article, improvements or errors let me know in the comments below and if you found this helpful, why not share it with others.


Pin It on Pinterest

Share This