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
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 :
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:
This is write quit command to come back to the terminal window
8. Compile the code using the following command:
eg. pi4j SAMPLE1.java
9. If there are no errors, run the program using the undermentioned command:
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!!