Mini PIR Movement Detection With Arduino and Home Assistant
by taste_the_code in Circuits > Sensors
1902 Views, 8 Favorites, 0 Comments
Mini PIR Movement Detection With Arduino and Home Assistant
In this article, we're exploring the world of motion detection using a mini PIR (Passive Infrared) sensor alongside Arduino and Home Assistant. This compact and efficient setup not only adds a layer of interactivity to your projects but also paves the way for a multitude of smart home applications, all triggered by mere movement.
The beauty of integrating a PIR sensor into your projects is its simplicity and functionality. With a few connections and some straightforward code, you can have an alert system that lights up an LED when someone is nearby or triggers various home automation, enhancing the intelligence of your living space.
By following this Instructable, you'll learn not only the hardware assembly and the necessary code to get your mini PIR sensor up and running with an ESP32 board but also how to integrate it with Home Assistant using ESPHome. This guide is designed to be accessible for beginners, so let's make your environment smarter and more responsive to human presence!
Supplies
By purchasing from the links below, you support the channel at no extra cost to you!
- Mini PIR Sensor - https://s.click.aliexpress.com/e/_DDyKDU9
- ESP32 Development Board - https://s.click.aliexpress.com/e/_oBvQBi8
- Mini Breadboards - https://s.click.aliexpress.com/e/_mNhW5go
- Jumper Cables - https://s.click.aliexpress.com/e/_mqDNJCo
- LEDs Kit - https://s.click.aliexpress.com/e/_EJWj67T
- Assorted Resistors - https://s.click.aliexpress.com/e/_EyOblBn
- Rework Station - https://s.click.aliexpress.com/e/_oBKJvvq
- Multimeter - https://s.click.aliexpress.com/e/_mM2oryg
Wiring the Sensor to the ESP32 Board
Wiring the mini PIR sensor to the ESP32 microcontroller is the first step towards creating our motion detection system. Starting with the sensor, it's important to note that it has three pins. The pin on the far right is for the positive connection, the middle pin is for the signal, and the far left pin is for the ground connection.
To begin the wiring process, take the PIR sensor's positive pin and connect it to the 5V output on the ESP32. This is crucial because the sensor comes with a voltage regulator, which requires slightly more voltage than 3.3V for stable operation—in this case, 5V sourced from the board will suffice. Next, I'll connect the sensor's ground pin to the ground on the ESP32 to complete the power circuit.
For the signal, the middle pin of the sensor, I'll connect it to pin 25 on the ESP32. This connection is what allows our microcontroller to read the sensor's signal, indicating motion detection. Additionally, I have an LED with a current-limiting resistor connected to pin 32, which goes to ground. This LED will serve as a visual indicator, lighting up whenever the PIR sensor detects movement. With this setup, we're ready to develop our logic and test the sensor's response.
Detecting the Movement With Arduino Code
Now that we've successfully wired our mini PIR sensor to the ESP32 board, the next step is to breathe life into the system with some Arduino code. To do this, I'll employ the Arduino IDE, which is an excellent platform for programming our microcontroller.
To initiate, I open the Arduino IDE and set up my serial communication in the `setup()` function. This allows me to output debug messages to the console for monitoring. Then, I define the PIR sensor pin, in this case pin 25, as an input. For the LED, pin 32 is set as an output. Initially, I ensure that both pins are set to LOW, turning off the LED.
The main loop of the code continuously checks the status of the PIR sensor. Upon detecting motion, the PIR sensor's signal pin goes HIGH. When this happens, I print the message "Somebody is here." to the serial console and simultaneously turn on the LED. If no motion is detected, meaning the signal is LOW, my code prints "Nobody." and switches the LED off. This loop repeats every second so any movement is promptly detected and indicated both visually through the LED and on the serial monitor. By doing this, we introduce the possibility to trigger actions like turning on lights or starting other automations based on motion detection.
Detecting Movement With ESPHome and Home Assistant
After testing our PIR sensor with Arduino code, let's take it a step further and integrate it with Home Assistant using ESPHome. ESPHome brings the power of simple yet powerful automation and seamlessly integrates with Home Assistant. Our aim here is to allow the PIR sensor not only to detect movement but also to communicate that information to Home Assistant for advanced automations.
I start by flashing my ESP32 board with ESPHome firmware from within the Home Assistant interface. Once the device is up and running, in the ESPHome dashboard, I configure the PIR sensor as a binary sensor. For this setup, I still utilize pin 25, where the sensor signal is connected. Within the configuration, under 'binary_sensor', I specify the platform as GPIO and define the pin number, labeling the device class as 'motion', which accurately describes the sensor's purpose.
To visually confirm motion detection like before, I have an LED connected to pin 32 and define this within ESPHome as an output. Crucially, I set up automations using ‘on_press’ and 'on_release' events: when the PIR sensor state is ON (motion detected), the LED lights up, and when the state is OFF (no motion), the LED turns off. This mirrors the functionality we had with the Arduino code, now within an ESPHome and Home Assistant ecosystem. In Home Assistant, the sensor state is displayed with an icon—when motion is detected, the icon changes, providing a clear visual indication within the user interface.
With this integration, every time motion is detected, not only does the LED on our ESP32 respond, but also the PIR sensor's state is updated in Home Assistant, opening up a range of possibilities for smart home automations based on presence detection. Whether it's turning on lights, activating security cameras, or sending notifications, the sensor can now act as a trigger for any connected device within my home automation setup.
All of the code is available on my website.
Next Steps
As we wrap up this tutorial on Mini PIR Movement Detection with Arduino and Home Assistant, I am eager to invite you into the conversation. The beauty of DIY projects like this lies not just in the following of the steps but in the endless possibilities for customization and innovation that come afterward. Whether you're a seasoned maker or a curious beginner, your ideas and input can spark incredible solutions and inspire the community.
I now turn to you: how might you employ this motion detection setup in your environment? Picture the potential automations that could enhance both convenience and security in your home. Maybe it's about lighting up a path when you enter a room, or perhaps it's about triggering an alarm when unexpected movement is sensed. Your concepts can influence the evolution of home automation, and I am eager to collaborate and exchange ideas.
Please feel free to comment below with your proposals or share your experiences with similar projects. Your feedback not only enriches the community's collective knowledge but could also give rise to the next great automation idea. Remember, each suggestion helps to propel this fascinating hobby forward. I'll be looking forward to your thoughts and insights.
Thank you for joining me in this adventure, and until next time, happy inventing and automating!
Cheers!