Introduction: Raspberry Pi With Sensor to Turn On/off Light


This tutorial will help you build the home automation to turn on/off light using sensor. Also, it allows you to turn on/off light or sensor using command. It is basically a rest end point and people can directly use with desktop or mobile browser to do various action.

The project is written in scala programming language and a war will be generated which needs to deploy in tomcat (web server) in the raspberry pi to make it work.

Step 1: Parts That You'll Need

  1. Raspberry Pi 2 or 3
  2. Bread board
  3. T-Cobbler (optional to connect raspberry pi gpio pin to breadboad)
  4. Ultrasonic Range Finder Module Sensor - HC-SR04
  5. 8 Channel DC 5V Relay Module (we used 8 channel relay module however with 1 channel is fine)
  6. Jumper wires (male to female and male to male)
  7. Lamp or any wired bulb

You can buy all above parts from Amazon.

Step 2: Ultrasonic Range Finder Module Sensor


  1. Model: HC-SR04
  2. Working votage: 5V DC
  3. Renging: 2cm - 500cm
  4. Resolution: 0.3 cm
  5. Frequency: 40 khz
  6. Cycle Period: 50 ms

The unit has four pins labelled VCC, trigger, echo and ground. VCC is connected to the 5 volt pin on the Raspberry Pi and without much surprised ground connects to one of the ground pins, trigger activates the sensor it needs to be connected to a gpio output pin whereas echo returns a signal which must be read by gpio input pin.

here's how it works a 10 microseconds high pulse is sent from the raspberry pi to the sensor this signals the sensor to produce an eight cycle sonic burst at 40 kilohertz when the sensor here's the echo it can determine the time between producing the burst and receiving its echo. To communicate this back to the pi it sends one high pulse for a length equal to how long it took to hear the echo so the length of the pulse is proportional to how far away the object is all we need to do is listen to the echo pin and time how long its high for.

To calculate the distance will use the formula:

Distance = Speed * Time

The time variable is the time it takes for the ultrasonic pulse to leave the sensor, bounce off the object, and return to the sensor. We actually divide this time in half since we only need to measure the distance to the object, not the distance to the object and back to the sensor. The speed variable is the speed at which sound travels through air.

Distance = (Speed * Time) / 2

Circuit diagram:

The wiring is fairly straightforward that we must be careful with how we set up the echo pin as it outputs a high at 5 volts to deal with this the echo pin is attached to a voltage divider. I chose a 1K ohm resistor for our two to provide some protection between the gpio pin and ground after all if I accidentally set the GPIO pin as an output setting a high would result in a current from the pin to ground when kilohms enough resistance to prevent any damage to the pin if such a mistake occurred. With our two chosen I need to calculate our one using the voltage divider equation. I know that I want 3.3 volts, so solving the equation

3.3 = 5 * 100 / R1+1000

I have R1 is equal to 515Ω. I need a resistor that's at least this size, so the closest largest resistor I had was 680Ω that will give 2.98 v which is easily a high enough voltage to be read as high by the GPIO pin.

Step 3: 8 Channel DC 5V Relay Module


  1. Relay voltage (JD-Vcc): 5v DC
  2. Relay Current: 60 mA
  3. Input voltage(VCC) - 5v DC (3.3v DC)
  4. Input current (In1/ In2): 2 mA
  5. Input type: Active Low

The relay voltage JD-VCC is 5 volts and will require 60 milliamps to control each relay a GPIO pin must be connected to an input pin. The input pins are designed to work off of 5 volts but should still work off of 3.3 volts and they will draw about 2 milliamps of current from the connected gpio pin. Lastly the modules inputs are Active Low which means that the relay is triggered when the GPIO pin is set to zero volts.

Step 4: Setting Up the Respberry Pi

    Raspberry Pi Setup:

    1. Connect the raspberry pi with sensor and relay module as per the diagram mentioned in the step 2 and 3. Also connect the light with relay as per the diagram in the step 3 and plug to the main switch.
    2. Install tomcat 8 in your raspberry pi and start the tomcat.
    3. Download the war from below location and deploy to the tomcat

    Once every thing is done, it's time to test. Now we have following urls to command raspberry pi and you can use with desktop or mobile browser. Make sure when you start the sensor there should be an end like a wall so that it can understand that this point is an end point and in between if any object comes it should turn on the light.

    1. Start sensor:
    2. Stop sensor:
    3. Turn on light:
    4. Turn off light:

    Note: is a IP address of your raspberry pi and 8080 is port of the tomcat server which is default.`

    If you want to do some changes in the code, download the repository from here HomeAutomation. After the changes you need to build and deploy to the tomcat. You can also use those end points with any web or android application which will be a better user experience with UI.

    Sensors Contest 2017

    Participated in the
    Sensors Contest 2017