Control your lights without sonoff or other things

Months ago I bought a new Raspberry…. and I put this object on rear the television in the living room. This zone is perfect because is near the electric box where I have relay for controlling 3 light… living room, kitchen and entrance. Now in this post, I want to explain how using a raspberry for controlling 3 light without “smart things” or sonoff. My system is complete integrated with my standard electrical system…. and following my motto “not hobby but real stuff” i made the system reliable.

The first thing, obviously is connect the raspberry to my lan with RJ45 (more reliable that wifi) and initialize this like this post . Now setup a static ip. I put (my first raspberry is  Now, before explain the details, we can view a small review of how the lights are controlled by a relay in a standard electrical system.

Electrotechnical lesson

In this schema we can view a 220V system for control a single light from 4 points. The buttons is real and when i “click” a button connect the F (phase) to the pin number 4 of the relay. The solenoid-valve in the relay (3pin and 4pin) gets excited and connect the pin number 5 (phase) to the pin number 2 (violet cable). The light turn ON and rest ON. If I click another time a button I put phase on the 4-pin and disconnect the 5-pin to the 2-pin…. the light turn OFF.

eletrotecnical standard system
eletrotecnical standard system

This is the standard system and it works perfectly!

I used this particular relay (Finder 26.02) because it’s have 2 contact…. Only one contact (between pin 2 and 5) is for normal use… for use standard in this schema. I use the Finder 26.02 because it have also another contact between pin1 and pin6. I will use this contact for monitoring light with my Raspberry 😉

My additions

In my idea I can use a little 5V-Relay controlled by my raspberry for simulating a real-button. I can control the state of light using the pin1-pin6 contact… I put 3,3V to the pin1… and I monitor if the pin6 is at 0V (off) or 3,3V (ON).

my additions to eletrotecnical system
my additions to eletrotecnical system

I hope everything is much clearer with the scheme. I connect the mini relay5V to my raspberry and control it with a software. It’s clear that for change the light status is sufficient simulate a click… in other words excite the relay for 100ms and after de-excite. I can monitor the state of the light with the GPIO4 in this example…. if the GPIO4 have 0V… the light is off becase the contact pin1-pin6 is disconnected. If the GPIO2 read 3,3V the status of light is ON.

It’s all ok? Now I can write some code.

First of all, a PULSE script in C

First of all, for convenience i wrote a simple script that simulate a click. I called “gpio_pulse” and i write it in C language.

#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <wiringPi.h>
#include <unistd.h>

// compile
// sudo gcc gpio_pulse.c -o /usr/local/bin/gpio_pulse -lwiringPi

//** launch with argument the bcm/gpio pin to monitor.
//** The program pulse the pin from LOW, after HIGH for tot milliseconds,
//** and after LOW
//** Using >> gpio_pulse 5

int pin=0;

int main (int argc,char **argv)
   if( argc==1 )
   fprintf (stderr, "Error: pass the pin in GPIO/BMC\n") ;
   return 1 ;


   if (wiringPiSetupGpio() < 0)
      fprintf (stderr, "Errore: Unable to GPIO: %s\n", strerror (errno)) ;
      return 1 ;

   pinMode (pin,OUTPUT);

   delay (10) ; 
   delay (100) ;

   return 0;

This script accepts as argument the PIN where launch the “click” simulation. Compile it, create the executable gpio_script and copy in the /usr/bin path

Now some setup

I suggest some things… In the /etc/rc.local write

gpio -g write 17 1   #this pin control the 5 relay... that work active-low
gpio -g mode 17 out
gpio -g mode 4 in   
gpio -g mode 4 down    #this is the pulldown resistor

My 5v-relay board is active low…. this initial setup ensure to you that when your raspberryPi is rebooting your relay not excite. In other case every time you reboot your raspberry the 5v-relay do “click”…. very fastidious… and the light turn on and off, bleah.

As you can see, I use pin 17 (gpio) for write the 5v-relay, and pin 4 for reading the state of light.


Now from your prompt you can write this for change state of light:

sudo gpio_pulse 17

and this for read the state of light…. it returns 0 for OFF and 1 for ON

sudo gpio -g read 4

Now, it’s clear that you can control your light with Google Home making a simple web API like i wrote in this post, and after use IFTTT like in this post

My work

This is a photo of my “rear Television zone”. There is a raspberry PI with a relay board that control my 3 lights.

real photo raspberry control lights
real photo raspberry control lights

I connect also HDMI output for debugging reason.  In this photo we can see that there is another things….  a infrared led! There is arguments for another post in future….

Please follow and like us:

Leave a Reply

Your email address will not be published. Required fields are marked *

Fork me on GitHub