How to Build a Smart Motion-Sensing Light Switch

2022-09-12 03:09:57 By : Ms. JANE MA

Turn lights on and off automatically with a motion sensor. You can even connect one to your smart home server via Wi-Fi.

A motion-sensing light turns on when it detects motion. It is commonly used for staircases, washrooms, and outdoor areas, to switch on lights automatically once it gets dark. There are two different sensor types used to detect motion: microwave-based motion sensors and Infrared PIR sensors.

Microwave sensors can detect motion though a solid concrete wall while a PIR motion sensor has a limited field of view. You can choose either type based on your needs and the location where you want to install the sensor.

For this guide, we will use a PIR motion sensor to build a motion-sensing light switch that turns on and stays on when motion is detected, and turns off after a certain time when motion stops. We will also connect a switch to a smart home network via Wi-Fi.

A PIR motion sensor such as the HC-SR501 can be used without a microcontroller to switch a relay module on/off to control an AC load for a specific duration, such as a lamp. The PIR sensor module comes with two presets that you can rotate to increase or decrease the sensitivity and time duration.

You can increase the duration or switch-on time to up to 5 minutes, after which the load (light) will shut down. You will need to tinker with the presets on the PIR sensor to find the sweet spot that fits your needs.

To build a motion-sensing light switch, you will need the following:

You'll need to connect the PIR sensor to the relay board and provide a 5V power input, such as via an optional micro USB module. Use jumper wires to make the connections between the relay module and the PIR sensor as in the diagram below.

If anyone moves in front of the PIR sensor, it will detect the motion and trigger the output pin to a high state (3.3V). This signal is sent to the relay, which turns on the connected AC load (lamp or light in this case). Once the time is over, the output pin is set to low (0V) and the relay turns off, switching off the connected AC load. To increase or decrease the time duration and sensitivity, you can adjust the presets on the sensor, as shown in the photo below.

That’s your standalone motion sensor switch. After the test, you can solder all components on a PCB, enclose it in a 3D printed enclosure or any box, and install it where you want it to switch lights on/off automatically when someone walks in or out.

A Wi-Fi-based motion sensor can be integrated with a home automation server, such as Home Assistant, to automate or trigger other smart home devices, lights, switches, etc. installed in your home or office. You may also enable notifications that you can receive on your smartphone or via an Echo Dot (Alexa).

There are many great ways to use a motion sensor in your home. For instance, when the sensor detects motion, Home Assistant can switch on the smart lights installed in a location such as your staircase or washroom. Similarly, you can use this motion sensor to capture pictures or trigger recording on a Wi-Fi camera as soon as motion is detected.

The possibilities are endless when combined with home automation software. You can follow our guide on setting up Home Assistant on a Raspberry Pi to quickly get started.

To build a Wi-Fi connected light switch, you will need the following:

To build a smart Wi-Fi-based motion sensor switch, we will use Tasmota firmware. Alternatively, you may use ESPHome to compile custom firmware for Home Assistant.

After the firmware is flashed, you will need to connect the switch to your Wi-Fi network and configure it.

Press the Enter key after entering each command. This configures the PIR as a sensor that sends an On/Off message via MQTT to the %topic% (find it under Configuration > Configure MQTT).

Connect the PIR sensor to the NodeMCU or D1 Mini as shown in the following diagram.

Once everything is wired up as shown in the diagram, connect the power supply. Now to add the switch to Home Assistant.

Motion sensor switches can help you save energy with the added convenience of not needing to manually turn lights switches on or off while walking up or down a staircase or entering/exiting a garden or porch area.

Besides automating light switches based on motion, you can use the motion sensors to trigger automation in your home or office by integrating it with a smart home automation server, such as Home Assistant or openHAB. The possibilities are endless.

Ravi is an expert tech explainer, an IoT enthusiast and Linux lover with a background in big data and app development. He spends most of his weekends working with IoT devices and playing games on the Xbox. He is also a solo traveler who loves hiking and exploring new trails.

Join our newsletter for tech tips, reviews, free ebooks, and exclusive deals!