ESP-01 Motion Sensor With Deep Sleep

19,633

30

13

Introduction: ESP-01 Motion Sensor With Deep Sleep

About: Started playing with electronics to add extra character to my model train layout. The hobby keeps growing.

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.

Step 1: ESP-01 Deep Sleep Module

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.

Step 2: 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.

Step 3: ESP-01 Buzzer Module

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

Step 4: 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);

Step 5: 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.

Be the First to Share

    Recommendations

    • Game Design: Student Design Challenge

      Game Design: Student Design Challenge
    • Big and Small Contest

      Big and Small Contest
    • For the Home Contest

      For the Home Contest

    13 Comments

    0
    dvpe
    dvpe

    Question 1 year ago

    Hi,
    sorry but I am very novice. I have two questions about step 1:
    - I would like to understand why there is current while the capacitor was charging
    - on the other hand, I would like to understand why do you have R2 and R3=
    Thanks.

    0
    SE00000
    SE00000

    Answer 11 months ago

    Hi dvpe,

    I'm not an expert either but I think I can answer your questions.

    - When used in a direct current or DC circuit, a capacitor charges up to its supply voltage but blocks the flow of current through it because the dielectric of a capacitor is non-conductive and basically an insulator.

    - R2 is used to discharge the capacitor to ground and R3 is used to avoid burning out the transistor. In fact, when I was building my own circuit I forgot to put R3, and it started to burn out. Luckily I quickly realized and disconnected the battery.

    Hope that helps.
    Regards.

    0
    SE00000
    SE00000

    11 months ago

    Hi,
    Amazing tutorial. It was very useful for the project I'm working on.

    Thank you very much for the tutorial!

    0
    FBMinis
    FBMinis

    2 years ago

    I'm testing this circuit with a Wemos D1 Mini and so far I'm getting positive results without false resets. Thank you for sharing such a clear and objective tutorial. The PIR HC-SR501 already has a 1K resistor before the Out pin, that is, between the output of the BISS0001 chip and the Out pin. Maybe it's ok to omit the 1K resistor in your diagram connecting to the base of the transistor.

    0
    bobinbd
    bobinbd

    Reply 2 years ago

    Thank you for your feedback.
    You might be interested in the other instructable I have for the solar powered security sensor (handy for outdoor use!). This one provides even better power performance because rather than being in deep sleep, the esp-01 is totally turned off. Only the sensor (and the HC-SR501 works here) is powered and the ESP-01 gets powered when the sensor is triggered. Because of the dual latching circuit (either the sensor or the ESP-01 keeps the circuit on) the ESP-01 can turn itself off when the sketch is finished. It will stay on only if the sensor is still triggered.
    Your point about the resistor on the transistor base is well taken. These sensors don't really provide much current and sometimes not enough for the transistor to switch on enough. In fact, I find mosfets, that are voltage driven rather than current driven to often be more reliable.

    0
    FBMinis
    FBMinis

    Reply 2 years ago

    I have built your project but I used an ESP-07S and it alerts me through Telegram. I will study your door sensor project because I have been reading about using Mosfets for even lower power but have yet to test this solution. Regarding the HC-SR501, I found in the BSS0001 datasheet that we can modify the inactivity period after motion-detected (originally 3 seconds), by replacing a capacitor connected to Pin 6. I modded it to 2min to keep my ESP from being reset too often by curious cats outside - detecting their visit is enough, their overstay an indication that i need to go downstairs. Right now Im trying to make a vibration sensor with a piezo amplified by an LM324, the goal is to have it reset the ESP. Cheers

    IMG_20201129_191944.jpgIMG_20201129_192506.jpgIMG_20201129_192538.jpgIMG_20201128_211421.jpgIMG_20201208_215918.jpg
    0
    bitcoinsharjah
    bitcoinsharjah

    3 years ago

    Very Good info.Can we use BC547 instead of 2N222
    • 2N2222A - 625mWBC547 - 500mW
    • collector current (output current in common emitter config):
      2N2222A - 600mA

      BC547 - 100mA

    0
    bobinbd
    bobinbd

    Reply 3 years ago

    Note that I am using a 2N7000 N-CH Mosfet, not a transistor. When I had a transistor in the configuration I found that the buzzer was going off all the time. With the simple mosfet it just beeps a second on the initial startup and then not until triggered.
    So you can try using a transistor and technically it should work. You will need a base resistor.
    It can all be tested on a breadboard before you solder it.

    0
    zufi110
    zufi110

    4 years ago

    Thanks for this nice tutorial, Just finished this project as per instructions. I have to change gateway and IP address in code because my router address is 192.168.0.1. I have used same IP address as gateway and assigned static IP to server and client accordingly.Thanks again.

    0
    bobinbd
    bobinbd

    Reply 4 years ago

    That is a smart catch easily missed! Most of the routers I have worked with do have a default network of 192.168.1.xxx but not all routers do (192.168.0.xxx is probably equally common) so for sure you have to match the ip address to your specific router. I can access my router's config screen with 192.168.1.254 but not all routers use the same address for this. As much as I tried to test my configuration to be sure it is functional, I can't actually test some one else's environment.

    0
    acidutzu
    acidutzu

    4 years ago

    hi, thanks for the tutorial .. i'm trying from some time to build something similar but using a door sensor instead of a PIR sensor. Can you give me any hints? I just did your setup and replaced the PIR with the door sensor but for some reasons i cannot find the proper baud rate in order to debug in the terminal even if i use in setup function Serial.begin(9600) the monitor still print gibberish and i'm also afraid that the PIR sensor send a bigger voltage on out that the door sensor that has no external power supply ... thanks in advance

    1
    bobinbd
    bobinbd

    Reply 4 years ago

    It would be helpful to have some more info on the door sensor. How does it provide a low or high signal? What is its steady state?

    This code and the circuit configuration is build based on the steady state of LOW (0 volts) for the PIR and a HIGH state (3.3v) when triggered. You can use different types of sensors as long as they meet this basic criteria. If they have a HIGH steady state and a LOW trigger, this can configured as well, but the circuit will be different.

    In the deep sleep code I am using the RX pin as an input for the PIR. As mentioned in the code, this isn't really necessary. If you are using the serial monitor then you cannot use this pin or configure it as input. It would be nice to be able to use the GPIO0 pin for input but if this happens to be held low during boot, the ESP-01 will go into flash mode and sits waiting for a program to be uploaded. Not helpful. I don't use GPIO2 because this is also the LED pin and then the blink functions don't work.

    So, to test that your code works, configure your serial monitor, don't connect anything to the RX pin (and don't set it as input).

    I have found that putting a delay at the beginning of setup seems to help in removing the gibberish that is printed on the serial monitor. So have a delay period as your very first line of code and maybe add another delay after Serial.begin.

    After you are sure the basic code works, you can comment the Serial calls out of the code and then use the RX pin again, if you want.

    0
    DIY Hacks and How Tos

    Great tutorial. Thanks for sharing. We need more ESP related projects on this site.