<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Mounir</title>
    <description>The latest articles on DEV Community by Mounir (@supermnr).</description>
    <link>https://dev.to/supermnr</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2422738%2F8caa2a0d-8b3a-4b1c-b3fd-5053f606f88b.jpg</url>
      <title>DEV Community: Mounir</title>
      <link>https://dev.to/supermnr</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/supermnr"/>
    <language>en</language>
    <item>
      <title>Build Your Own Smart Motion Detector That Actually Works! (ESP32 + PIR Tutorial)</title>
      <dc:creator>Mounir</dc:creator>
      <pubDate>Mon, 24 Feb 2025 13:57:09 +0000</pubDate>
      <link>https://dev.to/supermnr/build-your-own-smart-motion-detector-that-actually-works-esp32-pir-tutorial-1i8n</link>
      <guid>https://dev.to/supermnr/build-your-own-smart-motion-detector-that-actually-works-esp32-pir-tutorial-1i8n</guid>
      <description>&lt;p&gt;Want to protect your home with a DIY solution? Create your own smart motion detector that works as effectively as commercial systems. Perfect for beginners in IoT and home automation!&lt;/p&gt;

&lt;h2&gt;
  
  
  What You'll Create:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;A reliable motion detection system that sends instant alerts&lt;/li&gt;
&lt;li&gt;Your own IoT device that works with smart home platforms&lt;/li&gt;
&lt;li&gt;A professional-grade security sensor you built yourself&lt;/li&gt;
&lt;li&gt;Automated email notifications when motion is detected&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Prerequisites :
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Active AskSensors account: &lt;a href="https://asksensors.com/pricing.html" rel="noopener noreferrer"&gt;Create AskSensors account&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Create a new sensor. Follow the simple process described &lt;a href="https://doc.asksensors.com/docs/d/s1/sensor-setup/" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Required Hardware:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;ESP32 board&lt;/li&gt;
&lt;li&gt;PIR motion sensor&lt;/li&gt;
&lt;li&gt;Jumper wires&lt;/li&gt;
&lt;li&gt;1kΩ resistor&lt;/li&gt;
&lt;li&gt;Breadboard&lt;/li&gt;
&lt;li&gt;Micro USB cable&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Quick Assembly:
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Connect PIR VCC → ESP32 5V&lt;/li&gt;
&lt;li&gt;Connect PIR GND → ESP32 GND&lt;/li&gt;
&lt;li&gt;Connect PIR DATA → ESP32 GPIO2 (with 1kΩ resistor)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fck0z2wr45qi1vbrnqc1o.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fck0z2wr45qi1vbrnqc1o.png" alt="PIR sensor and ESP32 diagram" width="800" height="273"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Note: The ESP32 GPIOs require 3V3 signals (not 5V tolerent). For quick hack, you can only add a serial resistor of 1K between the PIR DATA pin and the ESP32 GPIO pin to protect the ESP32 GPIO from damage. However, for production, a 5V/3V3 level shifter is needed to guarantee long term circuit reliability. You can check &lt;a href="https://makeradvisor.com/tools/logic-level-converter-module/" rel="noopener noreferrer"&gt;this page&lt;/a&gt; to get a 5V/3V3 level shifter module.&lt;/p&gt;

&lt;h2&gt;
  
  
  Setup Instructions:
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Prepare Your Environment
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Install the latest version of the &lt;a href="https://www.arduino.cc/en/Main/Software" rel="noopener noreferrer"&gt;Arduino IDE software (1.8.7 or higher)&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;In your Arduino IDE, go to File&amp;gt; Preferences.&lt;/li&gt;
&lt;li&gt;Go to the “Additional Board Manager URLs” field, Enter the following URL:
&lt;a href="https://dl.espressif.com/dl/package_esp32_index.json" rel="noopener noreferrer"&gt;https://dl.espressif.com/dl/package_esp32_index.json&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;If you already have the ESP8266 boards URL, separate the URLs with a comma as shown below:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://dl.espressif.com/dl/package_esp32_index.json,http://arduino.esp8266.com/stable/package_esp8266com_index.json" rel="noopener noreferrer"&gt;https://dl.espressif.com/dl/package_esp32_index.json,http://arduino.esp8266.com/stable/package_esp8266com_index.json&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Now, open boards manager (Tools &amp;gt; Board &amp;gt; Boards Manager), search for ESP32 and click the install button for the “ESP32 by Espressif Systems”.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  2. Code
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Install the &lt;a href="https://github.com/asksensors/pubsubclient" rel="noopener noreferrer"&gt;MQTT PubSubClient Library&lt;/a&gt; for the Arduino IDE if you haven’t already.&lt;/li&gt;
&lt;li&gt;Download this demo from the &lt;a href="https://github.com/asksensors/AskSensors-ESP32-PIR" rel="noopener noreferrer"&gt;AskSensors Github page&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;The provided code simply reads the status of the PIR sensor and send this information to AskSensors over MQTT. You need to modify the following variables:&lt;/li&gt;
&lt;li&gt;Your network credentials, so that ESP8266 can establish a connection with existing network&lt;/li&gt;
&lt;li&gt;The Api Key In and username to set the MQTT Topic:username/apiKeyIn
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;ssid&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"YOUR_WIFI_NAME"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;password&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"YOUR_WIFI_PASSWORD"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;username&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"YOUR_USERNAME"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;pubTopic&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"publish/username/apiKeyIn"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3. Upload and Test
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Connect ESP32 via USB&lt;/li&gt;
&lt;li&gt;Check your Sensor data stream on AskSensors.&lt;/li&gt;
&lt;li&gt;You can cross-check the AskSensors graph readings with the values being printed on your ESP32 Serial Terminal.&lt;/li&gt;
&lt;li&gt;You can customize a Binary graph to plot your data as showing by next pictures.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fiogijjiwi0nxhah1segl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fiogijjiwi0nxhah1segl.png" alt="PIR sensor on" width="384" height="363"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fphc9mx7j8t5pqrq3we3j.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fphc9mx7j8t5pqrq3we3j.png" alt="PIR sensor off" width="384" height="363"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Configure Email Alerts
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Log into your &lt;a href="https://asksensors.com" rel="noopener noreferrer"&gt;AskSensors Iot platform&lt;/a&gt; &amp;amp; select your sensor&lt;/li&gt;
&lt;li&gt;Click the 'Add Alert' button&lt;/li&gt;
&lt;li&gt;Select 'Email Alert' as the notification type&lt;/li&gt;
&lt;li&gt;Set your desired threshold values:

&lt;ul&gt;
&lt;li&gt;Choose 'Max Value' = 1 to detect motion&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Enter your email address&lt;/li&gt;
&lt;li&gt;Save the configuration&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now you'll receive instant email notifications whenever motion is detected!&lt;/p&gt;

&lt;h2&gt;
  
  
  Perfect For:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Home security&lt;/li&gt;
&lt;li&gt;Smart lighting automation&lt;/li&gt;
&lt;li&gt;Pet monitoring&lt;/li&gt;
&lt;li&gt;Visitor counting&lt;/li&gt;
&lt;li&gt;Energy saving&lt;/li&gt;
&lt;li&gt;Remote property monitoring&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;em&gt;This project takes only 30 minutes to complete and can be a great addition to your smart home security system. With email alerts, you'll never miss any unauthorized movement again!&lt;/em&gt;&lt;/p&gt;

</description>
      <category>iot</category>
      <category>cpp</category>
      <category>mqtt</category>
      <category>diy</category>
    </item>
  </channel>
</rss>
