DIY Project - Automatic Light Switch with Movement

A modern home cannot be complete without a smart lighting system. You need lights that go on and off all by themselves depending on human activity within the house or depending on a pre-set lighting schedule. Smart lighting increases your home security and functionality and helps you lower your energy bills. But as beneficial as auto lights are, they can be extremely pricey to install. That is why you need to try this DIY project.

DIY Automatic Switch 

With a simple adapter, a Raspberry Pi 4 device, and a PIR sensor, you can convert a normal bulb into a smart Bulb. This tutorial will walk you through the process of making a simple automatic switch with motion control using the following hardware: 

  • A  Raspberry Pi 4 with a custom Raspbian disk image. You can use any model Raspberry Pi running Raspbian. 
  • An ultrasonic PIR sensor 
  • A single-channel, active-low relay module 
  • Jumper wires
  • LED 100Ω Resistor 

Component Description

 Raspberry Pi 

A Raspberry Pi 4 is a tiny computer that, like a regular computer, can be controlled over the internet. All you need is to connect it to a network and it becomes a smart device that you can control from a remote location. If you connect it to a Wi-Fi network and to your regular lights, you can convert the entire lighting system into smart lights that can be controlled through an override signal sent over the network.

If you connect a Pi to a relay module and a PIR sensor, you can control your lights through a programmed schedule or the ambient light level. 

 A Relay Module

The main function of a relay module in our project is to enable our Raspberry Pi 4 to act as a microcontroller. It helps us to program the Pi to control a lighting circuit in a programmed on and off schedule. 

A sensor

A PIR sensor helps the Pi to detect the ambient light levels in a home so that, instead of depending on a programmed schedule to turn the lights on or off, it can tell when to turn the lights on or off depending on how dark or clear the surroundings are at a given time of day.

A PIR sensor can also detect motion so it switches off lights whenever there is no activity in the home, e.g. when nobody is home or everyone is asleep. This is the automatic light switch that we will make in this project.

Circuit Design

You can just connect the PIR Sensor to the Pi’s GPIO pins but we need to use a relay module in order to make a more stable device.  In order to identify the correct physical pins in the Pi, you could use Raspberry Pi GPIO pins and Raspberry Pi's GPIO interface guides, such as Pinout. After that, this is how you proceed:

  • Locate these 3 PIR sensor pins: PIR-VCC (red in color and labeled “3-5VDC in”), PIR-OUT (brown and labeled “digital out”), and PIR-GND (Black and labeled “ground”). Into each one of those pins, plug a male to female jumper wire. 
  • Take the wire that you plugged into the PIR-VCC pin and plug its free end into the positive rail of your Relay Module, and then the wire from the PIR-GND into the module’s negative rail. The PIR-OUT goes into any of the remaining rails.
  • Take a black jumper wire, plug it into the GPIO GND [Pin 6] on the Pi, and then to the module’s negative rail so that the PIR-GND on the PIR sensor and the Pi’s GPIO GND pins are connected through the negative rail of the module.

Take a red jumper wire, plug it into the GPIO 5V [Pin 2] on the Pi, and then to the module’s positive rail so that the PIR-VCC on the PIR sensor and the Pi’s GPIO 5V pins are connected through the positive rail of the module.

At this point, the Pi can now power the PIR sensor. To complete the circuit though, we need to connect the sensor’s PIR-OUT pin with the Pi’s GPIO 7 pin (Pin 26) through the Module’s rail that you plugged the PIR-OUT wire. That done, your smart sensor is complete and can be connected to a light switch to automate it.

Working of the Project

When the PIR sensor detects a human motion, its output becomes HIGH, prompting the Pi to activate the active-low Relay Module, consequently turning the lights ON. Once the motion stops, the PIR output becomes LOW, prompting the Pi to turn off the relay, consequently turning the lights OFF. “Smart lighting can seem incomprehensive to implement at first, however, it will contribute to a much smarter and greener house, which will reflect in your home’s energy efficiency and sustainability” - says experts from Cold Air Central. 

Using Python

In order to program the GPIO pins on Raspberry Pi, you could use Python or other languages. You will need to reference a module named RPi.GPIO. if you want to communicate to the GPIO pins using Python. The two ways to number the IO pins on a Raspberry Pi within RPi.GPIO. The first one is by using the BOARD numbering system and the second one is the BCM numbers. The first numbering system is usually recommended because the hardware system will always work, no matter if you do revisions. You will not even need to change the coding.  

Final word 

While the project might take some time, its results are absolutely worthwhile. You have saved money for yourself, but wouldn’t you like to help others as well? This can easily be done by finding the right tutor flyer templates online, where you can put all the information needed for this DIY project and then share it with others.