Introduction: Relay Control With Raspberry Pi

About: We are a group of makers. We work in IoT, IOS app, android app, embedded design, sensor design, raspberry pi, arduino, beaglebone, particle electron, particle photon, Bluetooth.

Most of us have faced the problem when the raspberry pi board is not capable of switching between multiple devices simultaneously. So connecting multiple devices using the 26 GPIO pins is not possible. Moreover, it cannot be extended beyond 26 so more than 26 devices cannot be connected.

This problem can be solved by using a GPIO header. On one header, we can connect a Relay Board with up to 16 Relays and we can extend the count of boards to 128. So, in total, 128*16 devices can be connected.

Let's Start then!

Step 1: Hardware Needed

For this project, we will be using:

1. Relay Controller

2. Raspberry Pi

3. I2C Shield

4. 12V Power Adapter

5. I2C Connecting Cable

You can buy the product by clicking on them. Also, you can find more great material at Dcube Store.

Step 2: Hardware Connections

Steps to connect Raspberry Pi with I2C Shield/Adapter

First, take the Raspberry Pi and place the I²C Shield on it. Press the Shield gently and we are done with this step as easy as pie(see picture #1 & #2).

Connection of the MCP23008 Relay Controller and Raspberry Pi

Using an I2C cable, connect MCP23008 Relay controller to the Raspberry via the I2C connecting port on I2C Shield(see picture #3).

Power up the boards

Raspberry Pi can be powered by any Micro USB Cable. It works on 5V and 2A. Plug in the Micro USB cable into the power jack of Raspberry Pi. Also, don't forget to power up the Relay Controller with 12V Power Adapter. Plug it in and we are good to go!

The final connections are given in picture #4.

Step 3: Working and Code (Using Java)

We have booted the device with Linux(Raspbian). In this, we are using the Raspberry Pi with a Monitor Screen

1. Install the “pi4j library” from http://pi4j.com/install.html. Pi4j is a Java Input/Output Library for Raspberry Pi. An easy and most preferred method to install the “pi4j library” is to execute the undermentioned command directly in your Raspberry Pi :

curl -s get.pi4j.com | sudo bash OR curl -s get.pi4j.com

2. To create a new file where the code can be written,the following command will be used :

vi FILE_NAME.java

eg. vi SAMPLE1.java

3. After creating the file, we can input the code in here. Some sample java codes are available on our GitHub Repository. These are ready to be used simply by copying them from here.

4. To insert the code press “i” key.

5. Copy the code from the above-mentioned repository and paste it into the file created by you.

6. Click “esc” once done with the coding.

7. Then use the below-mentioned command to exit the code window:

:wq

This is write quit command to come back to the terminal window

8. Compile the code using the following command:

pi4j FILE_NAME.java

eg. pi4j SAMPLE1.java

9. If there are no errors, run the program using the undermentioned command:

pi4j FILE_NAME

Eg. pi4j SAMPLE1

The code repository has 5 sample codes and can control relay in many different combinations. So we have done Relay control with raspberry pi.

Let's see which tune can you make, to make the relay dance!!