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: 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 "", 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, 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. :)


AlekseyF2 (author)2016-11-26

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

HendotK (author)2016-04-30

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

AndreasO1 (author)2016-03-20

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.

AndreasO1 (author)2016-03-19

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 "" // 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:\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

shootermatic (author)2016-02-26

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

winkywalia (author)2016-02-08

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 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?

Azielaan015 (author)winkywalia2016-02-08

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?

LucasM81 (author)2016-02-01

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

Azielaan015 (author)LucasM812016-02-01

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!

IvanB60 (author)2016-01-25

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

virgozap (author)2015-12-13

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


Will I be able to connect it with my localhost in xampp..


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:\r\n\r\n\r\n";



Sorry for the late reply. Here you put the host where the php files are placed. So if your php files are in, than you put in that line.

MihaiCoz (author)2015-08-12


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

Azielaan015 (author)MihaiCoz2015-08-12

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

simonchin79 (author)2015-07-02

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...

tankapotamus (author)2015-04-13

$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.

Azielaan015 (author)tankapotamus2015-04-13

Arduino uno $4:
Wifi: $3:
PIR: $1:

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!

tankapotamus (author)Azielaan0152015-04-13

I would never tell someone to buy from eBay, or support arduino clones. I know what I'm talking about, and buying cheap Chinese clones only leads to head aches and doesn't support the open source movement in anyway.

Azielaan015 (author)tankapotamus2015-04-13

Arduino was build as open-source hardware, which means that everyone can make their own clones. These aren't necessarily worse than original, because they use the same (in china made) components, except for the usb chip sometimes. Furthermore: In my parts-list was already mentioned that I used an Arduino Clone. And it works. So there's my $10,- sum.

tomatoskins (author)2015-04-06

This is great! How long did all of this take?

Azielaan015 (author)tomatoskins2015-04-06

Most time is spend on figuring how to get the ESP8266 to work, which in total - I think - took me a full day, especially because there's just so little information available yet. But I'm glad you appreciate it ;-)