4 Channel Relay Hat Board For Raspberry Pi SKU: EP-0099

From 52Pi Wiki
Jump to navigation Jump to search

4 Channel Relay Hat Board For Raspberry Pi[edit]



4 Channel Relay Hat Board For Raspberry Pi is one of the "docker pi" module.
It has 4 channel relay that you can put it on your smart home project.
This four-channel relay uses high-quality relay modules combined with
the most popular microcontroller technology to communicate with the Raspberry Pi.
You can use multiple modules stacked together for use.
You can stack up to four layers.
Switch the I2C address of the device through the DIP switch on the circuit board to ensure that
the four-layer module gets different addresses, thus avoiding the problem of address duplication.
The module is very fast, easy to install, and supports multi-language development, making it ideal for smart home projects.
PS: The 4pin pin is a backup that supplies power to future products. You can ignore those pins. Please do not connect any device to that pins.


  • Easy to use
  • 4 channel relay
  • Support I2C protocol
  • Support for stacking
  • Support address switching by dial switch
  • Support multi-language programming
  • Support 3A 250V AC
  • Support 3A 30V DC
  • Support relay status light prompt

Package include[edit]

  • 1x 4 Channel Relay Hat Board
  • 4x M2.5x11+6 copper stick
  • 8x M2.5x5 screws & nuts
  • 1x Instructions

Device Address Map[edit]

DIP switch status icon Device Address
Dipswitch01.jpg 0x10
Dipswitch02.jpg 0x11
Dipswitch03.jpg 0x12
Dipswitch04.jpg 0x13






How to connect electrical device[edit]

    • Caution: If the wrong cable is connected, it may cause fire or personal injury.
    • Please pay attention to disconnect the power when connecting the cable and ensure the wiring is correct!

Please pay attension to this mark on the board:

Ddl shiyitu.jpg

  • NC means Normal Close
  • COM means Earth or GND
  • NO means Normal Open

Mechanical Drawing[edit]

  • PCB Information

  • Dimension Information

How to use it[edit]

  • Install i2c-tools package to operate the device.
sudo apt-get update
sudo apt-get -y install i2c-tools
  • Turn on the I2C interface

Open a terminal and Run sudo raspi-config
Use the down arrow to select 5 Interfacing Options.
Arrow down to P5 I2C .
Select yes when it asks you to enable I2C.
Also select yes if it asks about automatically loading the kernel module.
Use the right arrow to select the <Finish> button.
Select yes when it asks to reboot.

  • Detect the register of the chip on board

i2cdetect -y 1
you will see this result, the default device address is 0x10:


Demo code[edit]

  • Turn on channel No.1 relay

i2cset -y 1 0x10 0x01 0xFF

  • Turn off channel No.1 relay

i2cset -y 1 0x10 0x01 0x00

  • Turn on channel No.2 relay

i2cset -y 1 0x10 0x02 0xFF

  • Turn off channel No.2 relay

i2cset -y 1 0x10 0x02 0x00

  • Turn on channel No.3 relay

i2cset -y 1 0x10 0x03 0xFF

  • Turn off channel No.3 relay

i2cset -y 1 0x10 0x03 0x00

  • Turn on channel No.4 relay

i2cset -y 1 0x10 0x04 0xFF

  • Turn off channel No.4 relay

i2cset -y 1 0x10 0x04 0x00

  • Program in Shell script

Create a file and name it "relay.sh"

# create a loop to turn each relay on and off.
while true
   for i in `seq 1 4`
      i2cset -y 1 0x10 $i 0xff
      sleep 1 
      i2cset -y 1 0x10 $i 0x00
      sleep 1 

Then add executive permission to the script:

chmod +x relay.sh

Program in Language C[edit]

Create source code and name it "relay.c"

#include <stdio.h>
#include <wiringPi.h>
#include <wiringPiI2C.h>

#define DEVCIE_ADDR  0x10
#define RELAY1  0x01
#define RELAY2  0x02
#define RELAY3  0x03
#define RELAY4  0x04
#define ON      0xFF
#define OFF     0x00

int main(voide){
    printf("Turn on Relays in C\n");
    int fd;
    int i = 0;
    fd = wiringPiI2CSetup(DEVICE_ADDR);
       for (i=1; i<=4; i++){
          printf("turn on relay No.$d", i);
          wiringPiI2CWriteReg8(fd, i, ON);
          printf("turn off relay No.$d", i);
          wiringPiI2CWriteReg8(fd, i, OFF);
    return 0;
    • Compile it with following command:

gcc relay.c -lwiringPi -o relay

    • run it


Program in Python[edit]

you can also use Python script to driver the board.<br。 first, make sure you have installed "RPi.GPIO" library by import RPi.GPIO in python idle.
open a terminal and input python and then:

>>import RPi.GPIO 

if return nothing means the RPi.GPIO library is installed.
and then Create a new file named " relay.py" and copy and paste this code:

#!/usr/bin/env python
import time as t
import smbus

bus = smbus.SMBus(DEVICE_BUS)

while True:
        for i in range(1,5):
            bus.write_byte_data(DEVICE_ADDR, i, 0xFF)
            bus.write_byte_data(DEVICE_ADDR, i, 0x00)
    except KeyboardInterrupt as e:
        print("Quit the Loop")

save it and run:
python relay.py

Git Repository[edit]

You can also clone the repository from github:

sudo apt-get update 
sudo apt-get -y install git
git clone https://github.com/geeekpi/dockerpi
cd dockerpi/

Running Differenet program with different way.