Introduction: Home Automation With Raspberry Pi Using Relay Board

A major count of people want great comfort but at reasonable prices. We feel lazy to light up the houses every evening when the sun goes down and next morning, turning the lights off again Or to turn the Air conditioner/Fan/Heaters on/off as were the weather or room temperature.

An inexpensive solution to avoid this extra work of turning off the appliances when required is here. It is to automate your houses in comparatively very less costs using simple plug and play products. It works as when the temperature goes up or down, it turns the Air conditioner or heater on, respectively. Also, when required, it will help to switch on or the lights of your home without manually switching them on. And many more appliances can be controlled.Automate the world. Let us start your home.

Step 1: Hardware Required

We will be using:

  1. Raspberry Pi 2
    The Raspberry Pi is a solitary board Linux based PC. This little PC packs a punch in registering power, used as a piece of electronics exercises, and PC operations like spreadsheets, word processing, web surfing and email, and games.
  2. I2C Shield or I2C Header
    The INPI2 (I2C adapter) provides the Raspberry Pi 2/3 an I²C port for use withmultiple I2C devices.
  3. I2C Relay controller MCP23008
    MCP23008 from Microchip is an Integrated port expander that controls eight relays through the I²C bus. You can add more relays, digital I/O, analog to digital converters, sensors, and other devices using the integrated I²C expansion port.
  4. MCP9808 Temperature sensor
    The MCP9808 is a high-accuracy temperature sensor which provide calibrated, linearized sensor signals in digital, I²C format.
  5. TCS34903 luminance sensor
    TCS34903 is a color sensor family product which provides value of RGB component of light and color.
  6. I2C connecting cable
    I2C connecting cable is a 4-wired cable which is meant for I2C communication between two I2C devices connected through it.
  7. Micro USB adapter
    To power up Raspberry Pi, we need a Micro USB cable.
  8. 12V power adapter for Relay board.
    MCP23008 Relay controller works on 12V external power and this can be supplied using a 12V Power Adapter.

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

Step 2: Hardware Hookup

The requisite connections(refer to the pictures) are as follows:

  1. This will work over I2C . Take a I2C shield for Raspberry pi and gently connect it on to the GPIO pins of Raspberry Pi.
  2. Connect the one end of I2C cable to the in-port of TCS34903 and the other end to the I2C shield.
  3. Connect the MCP9808 sensor’s in-pot to the TCS34903’s out using I2C cable.
  4. Connect the MCP23008 ’s in-pot to the MCP9808 sensor’s out using I2C cable.
  5. Also connect the Ethernet cable to Raspberry Pi .Wi-Fi router can also be used for the same.
  6. Then, power the Raspberry Pi using a Micro USB adapter and MCP23008 Relay board using 12V adapter.
  7. Finally, connect the light with first relay and a fan or heater with second relay. You can expand the module or can connect more devices with the relays.

Step 3: Communicating Using I2C Protocol

To make Raspberry Pi I2C enabled, proceed as mentioned below:

  1. In terminal, type the following command to open the configuration settings:
    sudo raspi-config
  2. Select “Advanced Options” in here.
  3. Select “I2C” and Click “Yes”.
  4. Reboot the system to set it up as per the changes made using the command reboot.

Step 4: Programming the Module

The reward of using Raspberry Pi is, that is provides you the flexibility to opt a the programming language in which you want to program to interface the sensing device with Raspberry Pi. Harnessing this advantage of Raspberry Pi, we are demonstrating here its programming in the Java.

To set up the Java environment, Install the “pi4j libraby” from 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 | sudo bash


curl -s

import; import; import; class MCP23008 { public static void main(String args[]) throws Exception { int status, value, value1= 0x00; // Create I2C bus I2CBus bus = I2CFactory.getInstance(I2CBus.BUS_1); // Get I2C device, MCP23008 I2C address is 0x20(32) I2CDevice device = bus.getDevice(0x20); // Get I2C device, MCP9808 I2C address is 0x18(24) I2CDevice MCP9808 = bus.getDevice(0x18); // Get I2C device, TCS34903 I2C address is 0x39(55) I2CDevice TCS34903 = bus.getDevice(0x39); // Set Wait Time register = 0xff (255) , wait time = 2.78 ms TCS34903.write(0x83,(byte)0xFF); // Enable Access to IR channel TCS34903.write(0xC0,(byte)0x80); // Set Atime register to 0x00 (0) , maximum counts = 65535 TCS34903.write(0x81,(byte)0x00); // Power ON , ADC enabled , Wait enabled TCS34903.write(0x80,(byte)0x0B); Thread.sleep(250); // Read 8 Bytes of Data with clear/ir data LSB first byte[] data1 = new byte[8]; // Read Temperature Data byte[] data = new byte[2]; status =; // Configured all pins as OUTPUT device.write(0x00, (byte)0x00); Thread.sleep(500); while(true){, data, 0, 2); // Convert data int temp = ((data[0] & 0x1F) * 256 + (data[1] & 0xFF)); if(temp > 4096) { temp -= 8192; } double cTemp = temp * 0.0625; System.out.printf(“Temperature in celsius is : %.2f C %n”, cTemp);,data1,0,8); double ir = ((data1[1] & 0xFF) * 256) + (data1[0] & 0xFF) * 1.00; double red = ((data1[3] & 0xFF) * 256) + (data1[2] & 0xFF) * 1.00; double green = ((data1[5] & 0xFF) * 256) + (data1[4] & 0xFF) * 1.00; double blue = ((data1[7] & 0xFF) * 256) + (data1[6] & 0xFF) * 1.00; // Calculate illuminance double illuminance = (-0.32466) * (red) + (1.57837) * (green) + (-0.73191) * (blue); System.out.printf(“Illuminance is : %.2f lux%n “, illuminance); if (illuminance < 200) { value1 = status | (0x02); } else { value1 = status & (0x01); } device.write(0x09, (byte)value1); if (cTemp > 30) { value = value1 | (0x01); } else { value = value1 & (0x02); } device.write(0x09, (byte)value); Thread.sleep(300); } } }

Step 5: Creating File and Running the Code

  1. To create a new file where the code can be written/copied,the following command will be used:
    sudo nano
    sudo nano
  2. After creating the file, we can input the code in here.
  3. Copy the code given in previous step and paste it in the window here.
  4. Press Ctrl+X then "y" to exit.
  5. Then compile the code using the following command:
    Eg. pi4j
  6. If there are no errors, run the program using the undermentioned command:
    pi4j FILE_NAME
    Eg. pi4j

Step 6: Applications

This system allows you to control the devices without going to the wall switches. This has extensive capabilities as the times of turning the devices on or off are auto-scheduled. There are a handful of applications of this module from houses to industries, hospitals, railway stations and many more places can be automated in an affordable and easy way by its plug-and-play components.

Step 7: Resources

For more information about TSL34903,MCP9808 MCP23008 Relay Controller , check out the below links: