Introduction: Lights Control System

About: I am a linux fan, a maker, a boy who like make thing and have fun with my friends.

Recently I was working on understanding Microcontrollers and IOT based devices for Security Research purposes. So, I thought of building a small home automation system for practice. I am yet to complete this, but for startup i’ll be sharing how I used Raspberry Pi 2 and some other electrical components to control my room’s lighting in this post. Also, I will not be talking about Initial setup for Raspberry here, you may find various tutorials for that.

But in this project, I will introduce this docker pi series product to you.

Supplies

Component List:

  • 1 x Raspberry Pi 3B+/3B/Zero/Zero W/4B/
  • 1 x 16GB Class 10 TF card
  • 1 x DockerPi series 4 Channel Relay Board (HAT)
  • 1 x 5v@2.5A power supply which is from 52Pi
  • 4 x Light strip
  • 1 x DC connector
  • 1 x 12V power supply for the light strips.
  • several wires.

Step 1: Knowing About DockerPi Series 4 Channel Relay Board

DockerPi 4 Channel Relay is a member of the DockerPi Series,more commonly used in IOT applications.

DockerPi 4 Channel Relay can relay AC/DC, instead of traditional switches, to achieve more ideas. DockerPi 4 Channel Relay can stack up to 4, and can be stacked with other DockerPi expansion board. If you need to run for a long time, we also recommend that you use our DockerPi Power expansion board to provide more power.

NOTE OF CAUTION
Before we proceed further I’d like to WARN you about the DANGER of experimenting with the “Mains Electricity”.If anything goes wrong the worst consequence may be death or atleast burning down your own house. So, please DO NOT ATTEMPT to do anything mentioned in this article if you don’t understand what you are doing or better take the help of some experienced electrician. Lets get started.

Step 2: Features

  • DockerPi Series
  • Programmable
  • Control directly(without programming)
  • Extend the GPIO Pins
  • 4 Channel Relay
  • 4 Alt I2C Addr Support
  • Relay Status Leds Support
  • 3A 250V AC Support
  • 3A 30V DC
  • Can Stack with other Stack board Independent of the mainboard hardware (require I2C support)

Step 3: Device Address Map

This board has seperate register address, and you can just control each relay by one command.

Other Requirements:

  • Basic Understanding of Python or C or shell or Java or any other language (I'll be using C, python, shell, and java)
  • Basic Understanding of Linux systems
  • Presence of Mind

Now, before moving ahead you’ll need to understand the electrical components we will be using:

1. Relay:

A relay is an electrical device which is generally used to control high voltages using very low voltage as an Input. This consists of a coil wrapped around a pole and a two small metal flaps(nodes) that are used to close the circuit. One of the node is fixed and other is movable. Whenever an electricity is passed through the coil, it creates a magnetic field and attracts the moving node towards the static node and the circuit gets completed. So, just by applying small voltage to power up the coil we can actually complete the circuit for the high voltage to travel. Also, as the static node is not physically connected to the coil there is very less chance that the Microcontroller powering the coil gets damaged if something goes wrong.

Step 4: Connect the Relay to the Bulb Holder Powered by Main Electric Supply

Now to the tricky part, We’ll connect the relay to the Bulb holder powered by Main Electric supply. But, first I want to give you a brief idea about how the lights are switched ON and OFF via direct power supply.

Now, when the light bulb is connected to the main supply, we usually do this by connecting two wires to the bulb. one of the wire is a “Neutral” wire and other one is the “Negative” wire which actually carries the current , also there is a switch added to the whole circuit to control the ON an OFF mechanism. So, when the swith is connected (Or Turned ON) the current flows through bulb and the neutral wire, completing the circuit. This turns the light bulb ON. When the switch is turned of, it breaks the circuit and the light bulb turns OFF. Here’s a small circuit diagram to explain this:

Now, for our experiment, we’ll need to make the “Negative Wire” pass through our relay to break the circuit and control the power flow using relay’s switching. So, when the relay will turn ON, it should complete the circuit and the Light bulb should turn ON and vice-versa. Refer to the below diagram for Full circuit.

Step 5: Configuring I2C(Raspberry Pi)

Run sudo raspi-config and follow the prompts to install i2c support for the ARM core and linux kernel

Go to Interfacing Options

Step 6: Direct Control Without Programming(Raspberry Pi)

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

Step 7: Program in Language C(Raspberry Pi)

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(void)

{

printf("Turn on Relays in C\n");

int fd;

int i = 0;

fd = wiringPiI2CSetup(DEVICE_ADDR);

for(;;){

for (i=1; i<=4; i++)

{

printf("turn on relay No.$d", i);

wiringPiI2CWriteReg8(fd, i, ON);

sleep(200);

printf("turn off relay No.$d", i);

wiringPiI2CWriteReg8(fd, i, OFF);

sleep(200);

}

}

return 0;

}

  • Compile it.

gcc relay.c -lwiringPi -o relay

  • Exec It!

./relay

Step 8: ​Program in Python(Raspberry Pi)

The following code is recommended to be executed using Python 3 and install the smbus library:

Create a file named it : "relay.py" and paste following code:

import time as t

import smbus

import sys

DEVICE_BUS = 1

DEVICE_ADDR = 0x10

bus = smbus.SMBus(DEVICE_BUS)

while True:

try:

for i in range(1,5):

bus.write_byte_data(DEVICE_ADDR, i, 0xFF)

t.sleep(1)

bus.write_byte_data(DEVICE_ADDR, i, 0x00)

t.sleep(1)

except KeyboardInterrupt as e:

print("Quit the Loop")

sys.exit()

* Save it and then run as python3:

python3 relay.py

Step 9: Program in Java(Raspberry Pi)

Create a new file named: I2CRelay.java and paste following code:

import java.io.IOException;

import java.util.Arrays;

import com.pi4j.io.i2c.I2CBus;

import com.pi4j.io.i2c.I2CDevice;

import com.pi4j.io.i2c.I2CFactory;

import com.pi4j.io.i2c.I2CFactory.UnsupportedBusNumberException;

import com.pi4j.platform.PlatformAlreadyAssignedException;

import com.pi4j.util.Console;

public class I2CRelay {

// relay's register address.

public static final int DOCKER_PI_RELAY_ADDR = 0x10;

// channel of relay.

public static final byte DOCKER_PI_RELAY_1 = (byte)0x01;

public static final byte DOCKER_PI_RELAY_2 = (byte)0x02;

public static final byte DOCKER_PI_RELAY_3 = (byte)0x03;

public static final byte DOCKER_PI_RELAY_4 = (byte)0x04;

// Relay status

public static final byte DOCKER_PI_RELAY_ON = (byte)0xFF;

public static final byte DOCKER_PI_RELAY_OFF = (byte)0x00;

public static void main(String[] args) throws InterruptedException, PlatformAlreadyAssignedException, IOException, UnsupportedBusNumberException {

final Console console = new Console();

I2CBus i2c = I2CFactory.getInstance(I2CBus.BUS_1);

I2CDevice device = i2c.getDevice(DOCKER_PI_RELAY_ADDR);

console.println("Turn on Relay!");

device.write(DOCKER_PI_RELAY_1, DOCKER_PI_RELAY_ON);

Thread.sleep(500);

console.println("Turn off Relay!");

device.write(DOCKER_PI_RELAY_1, DOCKER_PI_RELAY_OFF);

}

}

  • Compile it and running:

javac I2CRelay.java -classpath .:classes:/opt/pi4j/lib/'*'

sudo java -classpath .:classes:/opt/pi4j/lib/'*' I2CRelay