$10 Arduino WiFi Motion Detector (PIR) That Writes to SQL Via Cheap ESP8266 Module





Introduction: $10 Arduino WiFi Motion Detector (PIR) That Writes to SQL Via Cheap ESP8266 Module

Hi there!

This is my first instructable so if anything is unclear, please let me know and I'll try to explain it!

My scenario was as follows: I have an external location of which I want to know if somebody's there. The easiest way to do this is using a motion detector (PIR) and an Arduino. There are a lot of examples of how to get this working using an Ethernet shield, but in my case I wanted to use the cheap (±$3) and new ESP8266 WiFi module. Since this module is fairly new, not much tutorials can be found, so it was quite a struggle to figure things out.

So my goal in one sentence: Make an arduino post information from the PIR motion detector via PHP to an SQL database using the ESP8266 WiFi module.

Total cost: Less than 10 dollars.

Step 1: List of Materials

For this tutorial I used:

- Fake Arduino Uno
- PIR motion detector
- The cheapest possible ESP8266 WiFi module
- Some jumper wires
- A breadboard

Step 2: Wiring Things Up

Note: From the documentation of the ESP8266, I found that this modules requires more power than the Uno's 3v3 port can give, but since I didn't have any alternative 3.3v power source I tried powering it from the 3v3 port and it just seemed to work.

In my case I used the V090 of the ESP8266 module (see image attached). Since this module isn't really breadboard friendly, using the breadboard is only necessary because you need to power 2 pins of the ESP8266 chip with 3.3v.

The PIR sensor is wired just as you would do in the default PIR example from the Arduino library.


Arduino | ESP8266
RX (D0) | TX
TX (D1) | RX
3v3 | VCC and CH_PD

Arduino | PIR
D3 | OUT
5v | VCC

Note how the PIR is powered with 5v, while the EPS8266 only takes 3.3v! Don't connect the EPS8266 to 5v, because that will fry the board.

Step 3: PHP

Because this tutorial is not about making SQL database etc. I assume that you're able to make a database containing the right tables yourself.

In my example, I will use 3 pages: dbconnect.php, add_data.php and review_data.php and I have a database called 'motion', containing a table 'motion' (as well) which has the colums 'id', 'event' and 'motion', where id and event (timestamp) are automatically created with every insert of motion.

dbconnect.php --> In this file we make the connection to the database. You can find the code for this page here: Github: dbconnect.php

add_data.php --> In this file the values are actually added to the database. In fact, when just browsing to add_data.php, nothing will be added, but you should enter add_data.php?motionornot=1 instead, with 1 in case of motion, or 0 in case of no motion (defined by the arduino code later on). The code can be found here: Github: add_data.php

data_review.php --> This is the page that shows a table with values 1 or 0, depending on if there was motion or not. This table is our final result. The code for this page can be found here: Github: data_review.php

Step 4: Arduino Code

Now to detect the motion and write therefore a 1 (in case of motion) or a 0 (no motion) to the database, we have to program the Arduino. I made the code so that when there's motion, the Arduino won't check for further motion for the next 5 minutes. Then, the Arduino will check for another 5 minutes if there's motion without writing a 0 to the database yet. Then, when there's no motion after all, a 0 will be written to the database. For more info about this particular part, check out this link.

My Arduino code can be found here:

Github: Arduino code (I'm sorry that it's not as efficient as possible yet, will make it better in the future)

Now, some changes in this code must be made to get this working:

  • On lines 6 and 7 you enter your Wifi settings
  • On line 8 you enter the host where the previously made .php files are hosted (eg: example.com) No http, no slahes etc.
  • On line 22 you define the number of seconds that you want between measurements.
  • On line 98 you define the path to the add_data.php file. If it's located at "example.com/arduino/add_data.php", you fill in here: "/arduino/add_data.php"

Now upload the code to your Arduino.

Step 5: Result

Now if you visit the page at example.com/arduino/data_review.php, you should get a nice table with detections of motion!

I hope this instructable will get you on the road since there aren't too many instructables containing an Arduino and an ESP8266 WiFi module yet.

Have fun :)

P.S. I read a LOT of pages and used some code of many people, but unfortunately I'm unable to trace all my sources back. If you recognise any of your code used in mine, please let me know and I'll credit you. :)



    • Oil Contest

      Oil Contest
    • Woodworking Contest

      Woodworking Contest
    • Clocks Contest

      Clocks Contest

    We have a be nice policy.
    Please be positive and constructive.




    Can i use Arduino UNO with build in wifi chip esp8266?

    sorry, i have tried but data cant show in review_data.php. and my program php databse and arduino same with you..

    For those, they have problems to bring this to work.

    I have done it with some modifications, i will make my own instructable with an

    mail notification and NODEMCU later, just follow me.

    I have made this setup so far and i can feed the database with:


    But when i try this with my nodemcu it will not work, i see in the serial output motion and no motion.

    But how can i check: Is the add_data.php? website called?

    my serial output shows me:


    No motion









    I have setup my host:

    #define HOST "quakeit.de" // Webhost

    and change your lines:

    cmd = "GET /arduino/add_data.php"; //complete url is

    cmd += "?motionornot="; //first selector

    cmd += motion; //value (1 or 0)

    cmd += " HTTP/1.1\r\nHost: quakeit.de\r\n\r\n\r\n";

    i get no entrys in the database... any ideas?

    for the people they missing the SQL Create table here it is:






    INDEX ( `event` , `motion` )

    ) ENGINE = InnoDB;


    ^^ you can call it in phpmyadmin SQL

    This seems to report motion every 5 minutes, whether there is motion or not.

    Hi, thanks for sharing this example. I am trying to work with:

    ESP8266 12E NodeMCU, Arduino UNO, PIR Motion Sensor

    I believe your example using ESP8266 01 should work for 12E NodeMCU as well. The problem is I am doing exactly same as you mentioned, but for some reasons it is not working for me.

    ESP is configured to use Arduino and I have tested it with blink sketch, and it works. I changed your sketch for PIR and uploaded on ESP:

    Currently I connected :

    PIR output to UNO on ~3
    UNO 5v and ground to PIR
    UNO TX to ESP TX
    UNO RX to ESP RX
    UNO 3.3v and ground to ESP 3.3v and ground

    after that, I powered UNO with USB from laptop, but not working.

    Can you please advice?

    1 reply

    I'm sorry. I haven't work with this since I made this instructable, so it's been too long for me to help you with this. Maybe somebody else has an answer?

    Hi, I followed the tutorial and it worked perfectly. But, sometimes the PIR detects fake movements beetween 2 minutes, and sometimes works perfect.... why this happends? xD

    1 reply

    No, but I noticed the same. It's already a long time ago for me, but I remember that I solved this by adjusting the code so that it needed 2 movements with a pause of 5 seconds between them, before sending a '1' to the database. This solved it for me!

    can I put localhost on line 8 and line 101 so that it looks like this:

    8 #define HOST "localhost" // Webhost


    101 cmd += " HTTP/1.1\r\nHost: localhost\r\n\r\n\r\n";

    or do I need to have a real hosting service

    i think you should give the sql files too...

    you can create a virtual server and try it !

    Hi Help me out..

    with what should i replace the following line ;

    cmd += " HTTP/1.1\r\nHost: www.google.com\r\n\r\n\r\n";


    1 reply

    Sorry for the late reply. Here you put the host where the php files are placed. So if your php files are in www.example.com/arduino/add_data.php, than you put www.example.com in that line.


    Can you please tell me if the WiFi module can be replaced with a Bluetooth module?

    1 reply

    Sorry, haven't tried that and don't have a BT module available at the moment.

    i haven't try this out yet.
    But i appreciate your work very much.... thumb up!!!
    I hope if it work in nano as well...

    $10? That looks more like $30! You should use the total of the bill of materials. That way people know how much to expect when recreating a project.

    1 reply

    Arduino uno $4: http://m.ebay.com/itm/351352342180?nav=SEARCH
    Wifi: $3: http://m.ebay.com/itm/291352639350?nav=SEARCH
    PIR: $1: http://m.ebay.com/itm/221719806210?nav=SEARCH

    Total $8,- leaves 2 dollars for a breadboard and some jumper wires. Which is easy to find. Don't judge if you don't know what you're talking about!