ESP-01 Motion Sensor With Deep Sleep

by bobinbd in Circuits > Wireless

24004 Views, 31 Favorites, 0 Comments

ESP-01 Motion Sensor With Deep Sleep

ESP-01 Deep Sleep Motion Sensor and Separate Buzzer module

I have been working on making homemade motion sensors that send an email message when triggered. There are many example instructables and other examples of doing this. I recently needed to do this with a battery operated PIR motion sensor and an ESP-01. The ESP-01 is very functional and has all the capabilities required so why not use the minimum and least expensive necessary? Added to the mix was another separate and remote ESP-01 module that triggered a buzzer when the motion sensor was triggered.

The code and eventual circuit layout has been gathered from numerous sources across the web and I don't think I can identify them specifically. The idea of sending emails via gmail came from an instructable and other sources and the final code is an amalgam from those sources. Getting deep sleep to work led me on many paths that often proved fruitless. Funny thing is, once a path proves fruitful, you stop looking for more paths. So I say thanks to all those who have contributed to my success and are yet unknown.

I had the same issue getting the PIR sensor to work at triggering the ESP-01 deep sleep. Many paths until there was one that worked.

Needless to say, there were some interesting hurdles or perhaps more relevant, a better understanding of the electronics that I required. You keep learning until something works and then you don't have to learn any more.

The ESP-01 does deep sleep as well as any other ESP8266 module as long as you don't require timed sleep. If you want the module to wake after a set amount of elapsed time, The ESP-01 is not the module to use. But that is not what I wanted. Elapsed time is pointless when using a PIR. I wanted the ESP-01 to waken only when triggered by motion sensed by the PIR. If there is no motion sensed for hours or days, the ESP-01 stays asleep using minimal battery power.

You will see many circuits that use GPIO16 connected to the ESP8266 Reset because GPIO16 is the wake signal. This is true, but it is the wake signal from timed sleep. We can ignore this PIN, which is good because it is not available on the ESP-01.

Basically, all we need is to get the signal from the PIR to trigger the ESP-01 Reset pin. The first difficulty you will surmise is that Reset is triggered on a LOW signal and the PIR sends a HIGH signal when triggered. Reset also needs to be HIGH or floating on boot. So to keep this short, after trying some different circuits I settled on using an NPN transistor with a pull-up resistor to keep the RESET pin HIGH during boot. The output from the PIR is minimal but it provides enough base current turn the transistor on.

As you will see in the circuit diagram below, the ESP-01 was awakened from deep sleep every time the PIR sensed motion.

But there was another problem. The Reset of the ESP-01 only happened after the PIR stopped sensing motion and returned to a low signal turning off the transistor and returning the Reset pin to HIGH because of the pullup resistor. This would mean that the email would not be sent, nor would the buzzer be activated until AFTER the PIR stopped sensing motion. I wanted the trigger to happen as soon as motion was sensed.

What I determined from this behaviour is that the ESP-01 actually triggers on the rising edge of the signal. Holding the Reset pin to ground does not actually trigger the ESP-01 from deep sleep but the moment the voltage rises to the HIGH signal, then the reset happens.

My very simple response to this behaviour was to add a capacitor to the line between the PIR output and the transistor base. This caused the transistor to only turn on while the capacitor was charging. Once charged, there was no further current and the transistor turned off. The 5k resistor allows the current to drain to ground. I tested this with an LED in place of the ESP-01 and could see the LED flash on for a fraction of a second before turning off. This little pulse was enough to pull the Reset pin to ground momentarily and long enough to trigger the Reset out of deep sleep.

ESP-01 Deep Sleep Module

IMG_6652_1.jpg
IMG_6648_1.jpg
ESP-01_PIR_DeepSleep.png

The deep sleep module uses two working voltages. The random 5v+ of the battery pack for the PIR and also a 3.3 volt regulator board for the ESP-01. I also incorporate a diode into the circuit to prevent damaged parts from reverse voltages. This does use a bit of extra power and does drop the battery pack voltage by 0.7 volts. The diode can be left out of the circuit if you are sure you will never reverse the battery pack leads. A switch is also added out of convenience.

This module is a minor update to my original non-deep sleep layout. In the non deep sleep configuration, the PIR is directly connected to RX pin of the ESP-01. I am using the RX pin of the ESP-01 as the input pin for the PIR for a few reasons. GPIO0 did not work because on boot the PIR output PIN would be LOW causing the ESP-01 to enter flash mode. I did not use GPIO2 because then I could not use the built-in LED for visual feed back. The RX and TX pins are often described as extra IO pins but my experience is that RX is an extra INPUT pin and TX is an extra OUTPUT pin.

In deep sleep configuration, the RX connection is not strictly necessary. I am using it only to monitor how long the PIR is triggered by turning the LED on while input is HIGH. As mentioned before, if you clear out the loop function and only use the setup routine then the RX connection is unnecessary.

Here is the parts list for the ESP-01 deep sleep module:

1 - 5 x 7 cm PCB Prototype Board

1 - 2 pin connector

2 - 1 x 3 female headers

1 - AMS1117 - 3.3 voltage regulator circuit board

1 - 1 x 3 Right Angle Male header pin

1 - 1 x 3 female socket header pin

1 - 1 x 4 female socket header pin

1 - 2 x 4 female header

1 - 1uf capacitor

1 - HC-SR501 PIR Motion Sensor

1 - 2N2222 Transistor

1 - 10k Resistor

1 - 4.7k Resistor

1 - 1k Resistor

1 - 1N4148 diode

1 - switch SS12D00G4 SPDT

1 - ESP-01

1 - 4AA Battery Pack

Please note that in the video the circuit board uses a ESP-01 breadboard adapter instead of the 2 x 4 header. While this adapter is easier to solder the 2 x 4 header works fine and actually fits better.

ESP-01 Deep Sleep Code

The Deep Sleep code performs two functions. Send an email message (via gmail as default) and send an http web request to the associated ESP-01 buzzer module to trigger the buzzer.

When triggered, this module provides two notification options and may be especially useful when you aren't paying attention to email messages.

You will need to update six lines of code with your specific values to make the sketch work:

const char* ssid = "xxxxx"; // Your WiFi SSID
const char* password = "xxxxx"; // Your WiFi Password String Senders_Login = "xxxxx"; // your email provider login String Senders_Password = "xxxxx"; // your email provider password

To = "xxxxxx";
From = "xxxxxx"; // Gmail generally prefers this to be same as Senders_Login and may substitute

I found the deep sleep module work unpredictably when the PIR sensor was set to below 10 secs for the length of the trigger event. I have mine set to 20 secs. This has proven very reliable but it also means that triggering events could happen with that frequency.

I have also added code to the loop function to keep the ESP-01 led on as long as the PIR is still sensing motion. All the code in the loop function can be removed and the call to deep sleep moved to the end of the setup function.

I use the blink function for a visual indicator of activity with the ESP-01 module.

While I have used and tested connectivity with gmail, other email providers work as well. I have tried a couple. In fact, I have found gmail more troublesome. Gmail requires that you have your account configured for access by less secure apps. This account setting is OFF by default so make sure you find it and change it to less secure. Gmail will NOT work otherwise.

If you choose to have more than one buzzer module just add extra calls of the http client (repeat the three lines of code but change the ip address used and also only define the httpCode variable as int once!

Note that the ip address of the buzzer is hard coded in this module. You don't have to use the ip address I have chosen, but you do have to match the ip address of the web call in this module with the ip address of the web server setup in the next module.

ESP-01 Buzzer Module

IMG_6635.JPG
IMG_6642.JPG
esp-01-buzzer.png

The buzzer module has a pretty simple setup. It uses a USB connector instead of a battery pack because I don't think this module is suitable for a battery pack. It must remain on and nework/wifi connected at all times because it never knows when a web request will be made. This requires more continuous power than battery packs are useful for.

Buzzer modules can be placed conveniently in multiple locations providing notification of a motion sensor trigger event no matter where you are!

The buzzer is connected to the 5v of the USB connector and there is another 3.3v regulator board that provides power to the ESP-01.

The buzzer module will function using TX, GPIO0 or GPIO2 for the output. In my configuration I am using GPIO0. (In the picture of the module the wire is connected to GPIO2 but I have since moved it.) While GPIO0 did not work for the deep sleep module (as INPUT) it works fine with this layout as OUTPUT. It is not pulled to ground on boot which will cause problems. I did use GPIO2 but then I couldn't use the onboard LED for any feedback but by using GPIO0 for OUTPUT I can use the on board LED.

I tried using an NPN transistor to power the buzzer in the circuit when the ESP-01 put a HIGH signal on the GPIO0 pin but the results were terribly inconsistent. The buzzer seemed to want to sound at all times, even with very little power. So instead I used an N channel MOSFET (2n7000) and the result was terrific. The IO pin drives the Gate as required.

While we only need two pins from the USB connector Vcc(+) and Gnd(-) I use a 5 pin header to connect to the PCB board for extra stability and for soldering before connecting the USB to the regulator. My 3.3v regulator board came with the pins preinstalled and in my mind, upside down. So to put the regulator into the header pins you can see that the circuit board is hidden, but worse than that, vcc and gnd on the regulator are reversed from vcc and gnd on the USB connector. So the wires cross over.

Also note that + power for the active buzzer comes from the 5v of the USB. Also, a 4 pin female socket header works nicely with the pin placement of the buzzer.

ESP-01 Buzzer Module Parts List:

1 - 5 x 7 PCB Board

1 - USB mini connector with pin headers (7 pins)

2 - 1 x 3 female headers

1 - AMS1117-3.3 v voltage regulator board

1 - 2 x 4 female header

2 - 1 x 4 female socket headers

1 - 2N7000 N-channel MOSFET

1 - 10 ohm resistor

1 - 5v Active Buzzer

ESP-01 Buzzer Module Code

The buzzer module acts as a simple ESP-01 web server. It responds with a simple message to a root request and when it gets the buzz request, it will trigger the buzzer. GPIO0 is used for the GPIO pin for the buzzer signal.

Note that the ESP-01 is configured with a hard coded ip address. This is required so that the deep sleep module is coupled to the buzzer address.

Like the previous module, you will have to update two lines of code with your specific values:

//SSID and Password of your WiFi router
const char* ssid = "xxxxxxx";

const char* password = "xxxxxxxx";

If you have multiple buzzer modules created, each one should be loaded with its own unique ip address.

You can also add different buzz methods which produce different buzzer melodies. For instance, if you have a PIR sensor at the front door and one at the back door, they can each make a web request to each of your buzzer modules but one sensor might have a sketch that calls buzz and the other sketch can call buzz2 so that you can tell from the sound which sensor was triggered. And so on and so on! The buzz2 function doesn't exist but just copy the buzz function and change the delay values.

For the web server you would just have to add a line of code like this:

server.on("/buzz2", buzz2);

Final Thoughts

This is my first instructable so I may have missed some practical things I should have included. The AMS1117-3.3 regulator board I used includes a tiny led that lights up when powered on. For the deep sleep module I did not want this led on and draining power unnecessarily. So I unsoldered what I could on one side of the led on the board and then used a utility knife to cut the trace line. This was easier than I thought and prevents the LED from lighting up. I haven't been able to determine what the power draw is when the ESP-01 is in deep sleep but I might have an answer in a few weeks. A colleague of mine was running the sensor (not in deep sleep) and found the batteries drained (5AA) in about a week. I think this setup should give a month or even more. We shall see.

The deep sleep module cost about $8 CDN in parts (batteries not included!) and the buzzer module $5.