» » » Relay Control with Raspberry Pi

Relay Control with Raspberry Pi

posted in: Others | 2

Problem Statement:

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.


Using a relay board:

Steps to connect Raspberry Pi with I2C Relay controller MCP23008:

Here,we will be using the Raspberry Pi 2 Model B V1.1, I2C Relay controller MCP23008, GPIO header, I2C connecting cable.


  • Take Raspberry Pi and GPIO header4
  • imgpsh_fullsize


  • Connect the GPIO Header to the Raspberry Pi’s GPIO pins.


  • Using an I2C cable,connect the I2C Relay controller MCP23008 to the Raspberry via the I2c connecting port on GPIO header


  • Power up the boards(Raspberry Pi and MCP23008).
  • Raspberry Pi can be  powered by any Micro USB adapter. It works on 5V and 2A.
  • Input voltage to the I2C Relay controller MCP23008 is 12V and this can be given by a 12V adapter
  • Now this board is ready to be used.


Steps to use this set-up: (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 libraby” 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
    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
    1. After creating the file, we can input the code in here.
      • These are ready to be used simply by copying them from here.
  3. To insert the code press “i” key.
  4. Copy the code from the above mentioned repository and paste it in the window here.
  5. Click “esc” once done with the coding.
  6. Then use the below-mentioned command to exit the code window:

    This is write quit command to come back to the terminal window
  7. Compile the code using the following command:

    pi4j FILE_NAME.java
    Eg. pi4j SAMPLE1.java
  8. If there are no errors, run the program using the undermentioned command:

    pi4j FILE_NAME
    Eg. pi4j SAMPLE1


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

Lets see which tune can you make , to make the relay dance!!

2 Responses

  1. uddish kapoor
    | Reply

    sir plz blog some basic interfacing of ARDUINO and raspberry pi
    best of luck…..

Leave a Reply