Raspberry Pi - 2-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 2-channel relay module. This type of module is composed of four relays on a single board.

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

Hardware Preparation

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

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.
Additionally, some of these links are for products from our own brand, DIYables.

Overview of 2-Channel Relay Module

The 2-Channel Relay Module Pinout

2-Channel Relay Module pinout

A 2-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
  • 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

    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 2 jumpers, allowing one to choose between the low trigger and the high trigger for each relay independently.

Wiring Diagram

The wiring diagram between Raspberry Pi and 2-channel relay module

This image is created using Fritzing. Click to enlarge image

※ NOTE THAT:

If the four devices controlled by a 2-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 2-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.output(PIN_RELAY_1, GPIO.HIGH) GPIO.output(PIN_RELAY_2, 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 2_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-2-channel-relay-module """ import RPi.GPIO as GPIO import time # Set the GPIO pin numbers PIN_RELAY_1 = 16 # GPIO16 PIN_RELAY_2 = 20 # GPIO20 # 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) try: while True: print("Turn on all 2 relays") GPIO.output(PIN_RELAY_1, GPIO.HIGH) GPIO.output(PIN_RELAY_2, GPIO.HIGH) time.sleep(1) print("Turn off all 2 relays") GPIO.output(PIN_RELAY_1, GPIO.LOW) GPIO.output(PIN_RELAY_2, 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 2_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 2 relays Turn off all 2 relays Turn on all 2 relays Turn off all 2 relays Turn on all 2 relays Turn off all 2 relays Turn on all 2 relays Turn off all 2 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!