Raspberry Pi - 4-Channel Relay Module

When we need to control four high-voltage devices such as pumps, fans, actuators, etc., we can utilize multiple relay modules. Alternatively, there is a simpler solution: a 4-channel relay module. This type of module is composed of four relays on a single board.

A 4-channel relay module compared to 4 x 1-channel relay modules:

Hardware Preparation

1×Raspberry Pi 4 Model B
1×4-Channel Relay Module
1×Jumper Wires
1×(Optional) Screw Terminal Adapter for Raspberry Pi

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you. We appreciate your support.

Overview of 4-Channel Relay Module

The 4-Channel Relay Module Pinout

4-Channel Relay Module pinout

A 4-channel relay module has the following pins:

  • Power pins for relay boards
    • DC+: connect this pin to 5V pin of power supply
    • DC-: connect this pin to the GND pin of the power supply and also to the GND pin of the Raspberry Pi
  • Signal pins:
    • IN1: this pin receives the control signal from Raspberry Pi to control relay 1 on the module
    • IN2: this pin receives the control signal from Raspberry Pi to control relay 2 on the module
    • IN3: this pin receives the control signal from Raspberry Pi to control relay 3 on the module
    • IN4: this pin receives the control signal from Raspberry Pi to control relay 4 on the module
  • Output pins: NCx (normally closed pin), NOx (normally open pin), COMx (common pin),
    • NC1, NO1, COM1: These pins are connected to a high-voltage device that is controlled by relay 1
    • NC2, NO2, COM2: These pins are connected to a high-voltage device that is controlled by relay 2
    • NC3, NO3, COM3: These pins are connected to a high-voltage device that is controlled by relay 3
    • NC4, NO4, COM4: These pins are connected to a high-voltage device that is controlled by relay 4

    For information on connecting a relay to high-voltage, as well as the differences between normally closed and normally open, please refer to Raspberry Pi - Relay tutorial.

    It also has 4 jumpers, allowing one to choose between the low trigger and the high trigger for each relay independently.

Wiring Diagram

The 4-channel relay module requires a large amount of energy, so it should NOT be powered directly from the 5V pin of Raspberry Pi. An external 5V power source should be used instead.

Therefore, we require three power sources:

  • A 5V power adapter for the Raspberry Pi
  • A 5V power adapter for the 4-channel relay module
  • A higher-voltage power adapter (12VDC, 24VDC, 48VDC, 220AC...) to power the items controlled by the 4-channel relay module
The wiring diagram between Raspberry Pi and 4-channel relay module

This image is created using Fritzing. Click to enlarge image

※ NOTE THAT:

If the four devices controlled by a 4-channel relay module have the same voltage, then a single high-voltage power adapter can be used for all. But, if the voltages are different, then separate high-voltage power adapters must be used.

How To Program For 4-Channel Relay Module

  • Sets up the Raspberry Pi pin to digital output mode with the GPIO.setup() function.
GPIO.setup(PIN_RELAY_1, GPIO.OUT) GPIO.setup(PIN_RELAY_2, GPIO.OUT) GPIO.setup(PIN_RELAY_3, GPIO.OUT) GPIO.setup(PIN_RELAY_4, GPIO.OUT)
GPIO.output(PIN_RELAY_1, GPIO.HIGH) GPIO.output(PIN_RELAY_2, GPIO.HIGH) GPIO.output(PIN_RELAY_3, GPIO.HIGH) GPIO.output(PIN_RELAY_4, GPIO.HIGH)

Raspberry Pi Code

Detailed Instructions

  • Make sure you have Raspbian or any other Raspberry Pi compatible operating system installed on your Pi.
  • Make sure your Raspberry Pi is connected to the same local network as your PC.
  • Make sure your Raspberry Pi is connected to the internet if you need to install some libraries.
  • If this is the first time you use Raspberry Pi, See how to set up the Raspberry Pi
  • Connect your PC to the Raspberry Pi via SSH using the built-in SSH client on Linux and macOS or PuTTY on Windows. See to how connect your PC to Raspberry Pi via SSH.
  • Make sure you have the RPi.GPIO library installed. If not, install it using the following command:
sudo apt-get update sudo apt-get install python3-rpi.gpio
  • Create a Python script file 4_relay_module.py and add the following code:
# This Raspberry Pi code was developed by newbiely.com # This Raspberry Pi code is made available for public use without any restriction # For comprehensive instructions and wiring diagrams, please visit: # https://newbiely.com/tutorials/raspberry-pi/raspberry-pi-4-channel-relay-module import RPi.GPIO as GPIO import time # Set the GPIO pin numbers PIN_RELAY_1 = 12 # GPIO12 PIN_RELAY_2 = 16 # GPIO16 PIN_RELAY_3 = 20 # GPIO20 PIN_RELAY_4 = 21 # GPIO21 # Set the GPIO mode to BCM GPIO.setmode(GPIO.BCM) # Setup the GPIO pins as outputs GPIO.setup(PIN_RELAY_1, GPIO.OUT) GPIO.setup(PIN_RELAY_2, GPIO.OUT) GPIO.setup(PIN_RELAY_3, GPIO.OUT) GPIO.setup(PIN_RELAY_4, GPIO.OUT) try: while True: print("Turn on all 4 relays") GPIO.output(PIN_RELAY_1, GPIO.HIGH) GPIO.output(PIN_RELAY_2, GPIO.HIGH) GPIO.output(PIN_RELAY_3, GPIO.HIGH) GPIO.output(PIN_RELAY_4, GPIO.HIGH) time.sleep(1) print("Turn off all 4 relays") GPIO.output(PIN_RELAY_1, GPIO.LOW) GPIO.output(PIN_RELAY_2, GPIO.LOW) GPIO.output(PIN_RELAY_3, GPIO.LOW) GPIO.output(PIN_RELAY_4, GPIO.LOW) time.sleep(1) except KeyboardInterrupt: # Cleanup GPIO on keyboard interrupt GPIO.cleanup()
  • Save the file and run the Python script by executing the following command in the terminal:
python3 4_relay_module.py
  • Listen for the click sound and LED indicator on the relays.
  • Check out the result on the Serial Monitor.
PuTTY - Raspberry Pi
Turn on all 4 relays Turn off all 4 relays Turn on all 4 relays Turn off all 4 relays Turn on all 4 relays Turn off all 4 relays Turn on all 4 relays Turn off all 4 relays

The script runs in an infinite loop continuously until you press Ctrl + C in the terminal.

Video Tutorial

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!