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:
- Wiring is simpler with the 4-channel relay module.
- The 4-channel relay module requires less space.
- The 4-channel relay module is more cost-effective.
- Programming is the same for both.
|Raspberry Pi 4 Model B
|4-Channel Relay Module
|(Optional) Screw Terminal Adapter for Raspberry Pi
Or you can buy the following sensor kit:
|DIYables Sensor Kit 30 types, 69 units
Overview of 4-Channel Relay Module
The 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.
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
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.
- Set the relay's state by utilizing the GPIO.output() function.
Raspberry Pi Code
- 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:
- Create a Python script file 4_relay_module.py and add the following code:
- Save the file and run the Python script by executing the following command in the terminal:
- Listen for the click sound and LED indicator on the relays.
- Check out the result on the Serial Monitor.
The script runs in an infinite loop continuously until you press Ctrl + C in the terminal.