Battery Operated ESP (Button, Sensors, ...) - Page 1
Forum section: electronics / beginners

author: penright
author_rank: Contributor
timestamp: February 12, 2020, 12:40:02 am
content: New to the forum, so I am getting my bearings on the culture, where to post, and how.The context of questions will be on soft switches and power regulators.Some Details....The project is using ESP devices and the ESP-Now protocol to create lightweight sensors. Let me use the first one as an example. Once it is working then to move to others would be easy, it more a matter of battery life. The first one is a push button. There is a single press, double press, and long press. I call this the "node". Then there is what I call the "gateway". May need to come up with a different name because sometimes as I am describing it, they think of it as connecting to a "network gateway". The gateway purpose is to take the short ESP-Now message, format it as MQTT and send it to the broker. This will be a mains powered device. The ESP can support both "Soft AP" and "Station" modes. So the button only sends using ESP-Now to the "Soft AP" mac address. Then the gateway has a more stable WiFi connection to the MQTT broker. So when you press the button, the ESP (node) will boot, send the message, and then shut off. I will post some code and video at the end of this post. The one issue so far is ESP-Now only seems to work on channel 1 or as far as I have read. The Soft AP and Station have to be on the same channel, so I had to lock my AP to channel 1 for now. This can be overcome by using 2 ESP for the gateway. Tie the serial pins together and the one receiving ESP-now, could serial transmit to the other one connected to the AP with the broker. I hope there is a better way, but for now, I am just locking the AP to channel 1.So for the "Soft Switch" ...I have searched for soft switches and found a video called "5 soft latches" The first one is what I think I need.I posted the circuit for more context. Right now I am waiting for p-channel MOSFETs. I had some n-channel just not any p-channel. I like this because in the video he demonstrated that when he pressed the button, the device power on, and use a pin to keep the switch going. Then when the software decided, it could turn off by changing the pin. And the button could then be used as input to the device. So it would be just a matter of watching input for a second press or it is still being press and therefor decode a single, double, or long-press. Then shut itself off. I think the battery would last for a while.To test, for now, I am just passing power when the switch is pressed. So you press the switch and it runs till you let it off. So I created an alternative way of decoding the switch. Until I get the parts for the soft switch, I am putting the smarts into the gateway. So the node on the press of the switch sends a "buttonPress1" message. If you hold the switch for one second, then it sends a second message ("buttonPressLong"). Then the gateway knows if within less than a second it sees two buttonPress1, then it was a double press. If it sees a buttonPressLong, it was a long press. And if after 1.75 seconds there are no other messages, it was a single press. This actually works. Here is the codehttps://github.com/penright/WiFi2MQTT/tree/GateWayCountingPresses. Here is a quick video showing ithttps://youtu.be/8LBjqeAB-dI. The broker is in the background. Eventually, my HomeAssistance will consume the MQTT and affect lights or whatever.I added a 1500uf cap and with a 3v button battery, that give enough shot for one message. But I have to run it off of the USB programmer to really test it.Which brings me to my first of what I suspect will be many questions.I have some LM1117-3.3 coming. Can I use that with a A23 12v battery? Will it have enough reserve to get past when the ESP power the WiFi transmitter? I don't have the equipment to measure it, but for what I have researched, the ESP runs about 70m while transmitting.Thanks for getting me started.

author: tubos
author_rank: Contributor
timestamp: February 15, 2020, 10:25:18 pm
content: In my experience the 1117 regulators have a too high idle current for battery operated projects that should run a long time.You can find regulators with only a few uA idle current.kdcircuits has a lot of videos with his esp8266 boards that consumes < 1uA while sleeping that can run for yearshttps://www.tindie.com/stores/kdcircuits/Hope this helps

author: penright
author_rank: Contributor
timestamp: February 17, 2020, 02:52:58 pm
content: Quote from: tubos on February 15, 2020, 10:25:18 pmWow, you are right ...PARAMETER CONDITIONS MIN TYP MAX UNITSQuiescent Current LT1117-3.3 VIN ≤ 15V 5 10 maFor now, it will get me through my prototype and code development, but any thoughts on what to use instead?Quote from: tubos on February 15, 2020, 10:25:18 pmActually, the circuit above was based on kdcircuits. The modification was adding the diodes so that once up and running, it could also be used as input. Which is exactly what I am looking to do. If the button is pressed a second time withing X or held for X time, then the "button press" is a buttonPress1, buttonPress2, or buttonPressLong. This is why I can not do any deep sleep. The wake for ESP devices is to trigger the reset. I assume the device knows the difference between running/reset and deep sleep/reset. I would need to experiment and see if I could track the button, but my hunch is pressing the button a second time would be a running/reset and therefore reboot. I really think this is the circuit I need. I just not 100% sure how to combine the voltage regulator and the soft switch. The example is a P-channel high side, which is still where I think I need the switch. But the datasheet had this example of a light switch. Could I flip the circuit and use an n-channel FET on the low side (where the photosensor is)?Quote from: tubos on February 15, 2020, 10:25:18 pmYes.

author: Koen
author_rank: Frequent Contributor
timestamp: February 17, 2020, 03:16:02 pm
content: You don't need a soft latching circuit. Keep the esp32 on in deep sleep and wake it up from ext0. Then make any logic you want.

author: penright
author_rank: Contributor
timestamp: February 17, 2020, 03:41:15 pm
content: Quote from: Koen on February 17, 2020, 03:16:02 pmDoes that work on ESP8286? I am not locked into the device (which ESP) I use, I was just starting with ESP-01 just because of size.All the examples show DO to reset for the internal wake-up, and just to reset for the external wake-up. So would not pressing the button a second time just reboot?

author: Peabody
author_rank: Super Contributor
timestamp: February 17, 2020, 11:50:56 pm
content: I don't know about the ESP32, but the ESP8266 does not sleep very soundly, and it doesn't awake where it left off - it basically reboots. I think the need for the latching power switch depends on how long the Off times are going to be. I designed a mailbox notifier using the Lolin D1 Mini (ESP8266), and it is turned on once a day for 10 seconds. So it made sense to include the power latch so everything could be completely off instead of using up battery power sleeping. Also, if the circuit is only going to be On occasionally, with the power latching circuit you don't need to worry much about things like the regulator's quiescent current, or current used by the USB adapter, because they are going to be completely Off most of the time.You didn't actually post a circuit in your first post, but I agree that the first latch circuit in the video is probably what you need. Here's a clearer version of that circuit:The capacitor shown in the video is only needed if your processor is slow to boot up. I didn't need one with the ESP8266.

author: penright
author_rank: Contributor
timestamp: February 18, 2020, 06:20:07 pm
content: Quote from: Peabody on February 17, 2020, 11:50:56 pmYou are right on what I am looking at. I have downloaded KiCad and am learning it. I binge-watched Johns Basement on using it. Thanks for cleaning the drawing up for me.My next step is how to add the regulator.Quote from: Peabody on February 17, 2020, 11:50:56 pmClose. Actually, once I get my buttons to work, that mailbox sniffer maybe the next one. Here are the buttons thathttps://www.securifi.com/almondclickthat I am basing the functionally off. They are no longer available and they are Zigbee protocol. There are other buttons, but what the fun if you buy them when you can build something. :-)I assume you are using WiFi from the mailbox back to the house. If so I also assume you are connecting to an AP in your house. The auth process of WiFi is several seconds. In the mailbox example, 5 seconds to be alerted that there is mail is not that big of a deal. But in my case, let us say the button is controlling a light. You would not want to wait that long for the command to reach whatever is controlling the lights. In my case, I am using HomeAssistant. That is why I am using ESP-Now.https://www.espressif.com/en/products/software/esp-now/overviewI was going to have a node/gateway where when you pressed the button on the node, it would send the ESPNow message and the gateway would then be connected to AP and therefore have access to an MQTT broker. So the gateway would reformat and send it on. The issue is ESPNow only uses channel 1. So when the gateway starts up a Soft+AP it has to be channel 1 to get the ESPNow message. But when the station connects it might be channel 11. When that happens if the gateway has just sent an MQTT message, it seems to take about 4 seconds before the Soft+AP can receive an ESPNow message. I could fix this by connecting two ESP-01 together and use the serial pins. One listens on ESPNow the other is WiFi.So @Peabody how did you power the mailbox sensor? Battery?

author: Peabody
author_rank: Super Contributor
timestamp: February 18, 2020, 07:22:39 pm
content: My mailbox notifier has an ESP8266 which connects to my router, then sends a Webhooks command to IFTTT.com, which then sends an alert to the IFTTT app on my phone. I have an initial delay built in which is only relevant when it's connected to serial for testing, but even without that, it takes several seconds to connect to the router, and then a few more until the alert comes through. Everything is on my github:https://github.com/gbhug5a/Mailbox-NotifierI agree this would not work for just turning on a light. I have no experience with things like Zigbee or ESPNow, so I can't help with that. But I assume they all operate without going through all the formalities of establishing a secure WiFi connection, and are therefore much faster.Do I understand correctly that a normal ESP8266 can be programmed to use the ESPNow protocol? If so, you might look at the Wemos/Lolin D1 Mini that I used for the mailbox. It has a regulator built in, along with a USB port. The regulator is a lot better than a 1117. I find that no matter how short the power-on button press is, the Mini boots up fast enough to assert a high on the output pin to keep power on. But I don't know if it takes any material time for RF to get up to speed. I still think that for occasional button presses, it makes sense to shut down the power altogether rather than going into sleep mode. It might even be possible to power with a coin cell battery, but the ESPs use a lot of power when transmitting, so that might not work.I'll have to read up on ESPNow.

author: penright
author_rank: Contributor
timestamp: February 18, 2020, 09:36:00 pm
content: Quote from: Peabody on February 18, 2020, 07:22:39 pmYes.So what kind of battery are you using for the mailbox?I was going to play with a A23 battery. They are small 12V, so I was hoping that would help with the battery lasting and give a little more zap. I added a cap and that helped.We are on the same page. Instead of IFTTT, I was going to use MQTT.Yes, ESP-Now if fast. Here is a video showinghttps://youtu.be/8LBjqeAB-dIshowing how fast the ESP device can boot and send the message. The button is momentary press and only supplies power while the button is pressed. There is no soft-latch at this point. So that means in the moment of pressing the button, the device powered up, sent the message. That board's header (power) is in parallel with the battery connector. To program or run off of USB, I just pop it out. Kind of a prototype board. When I tested with the button battery, I could get the single press to output, but it would die before the long press. The video is running off of USB power.

author: Peabody
author_rank: Super Contributor
timestamp: February 19, 2020, 12:01:19 am
content: The mailbox battery can be any lithium rechargeable, such as the 18650, but it will also work with three AA lithium iron disulfide non-rechargeable batteries, which may be preferred because they tolerate temperature extremes. But these are all too big for what you're doing. I don't think the A23 is right for this. Something closer to the 3.3V you need for the 8266 would be better. Really any 3.7V (max 4.2V) LIPO that's big enough should work fine. The A23 is used in garage door remotes, but in that case the 12V is actually used for RF transmission. That's not the case with the 8266, which uses the regulated 3.3V.The button battery just may not be able to provide enough current to power an 8266. If that's the case, your meter would show the battery voltage drooping significantly.One thing you might want to consider is the method KDCircuits uses in his TrigBoard to keep sleep current low. Instead of the mosfet, he has a voltage regulator with an Enable input. Only when Enable is active does the regulator output 3.3V, which of course means the 8266 is powered down at all other times, not sleeping. The regulator's "sleep" current is 20nA, or essentially nothing. He quotes the total sleep current of the TrigBoard as <1µA. But if your 8266 module already has a regulator, I still like the P-channel mosfet switch.

author: penright
author_rank: Contributor
timestamp: March 23, 2020, 03:19:27 pm
content: Some parts have started coming in and now it is time for the rubber to meet the road. Before I started wiring up a prototype, I wanted to get something on paper. I just noticed the hand sketch had the P Channel Mosfet is drawn wrong.Since it has been a while, let me recap my thoughts and goals. The goal is to have a device (client) that will report button presses via ESPNow to a second device (receiver/relay). The code related to the circuit would control the 'HoldPower' by pulling it high. From this point on the code will determine when the power is turned off. So after a check of a second button press or if it is held down for a long press, the device sends the result via ESPNow and turns itself off. I went with a 23A 12v battery thinking it will last longer and it is small. In the future, I may make a 3.3v coin cell versions just to see how long it lasts.For now, this schematic is what I am thinking and would like a second set of eyes to see if I am in the ballpark.

author: Peabody
author_rank: Super Contributor
timestamp: March 23, 2020, 05:55:08 pm
content: penright, I can't see any of your pictures in your posts. There are no links to them. Is it just me?

author: penright
author_rank: Contributor
timestamp: March 23, 2020, 08:26:04 pm
content: I may not be doing it right. Still learning this forum. I tried re-sharing, does it work now?Do you have to be a certain level to attach pictures? When I expand the "Attach:" then I try and drag or choose a file, I can not get it to show in the preview. I have tried different selections in the dropdown.If you pick a different dropdown, do you have to re-upload the file?

author: Peabody
author_rank: Super Contributor
timestamp: March 23, 2020, 09:22:02 pm
content: I still can't see any of them. There's an icon indicating something is there, but when I mouse over that, there's no link. I don't remember if there's an approval process for new users. Normally you would just expand the Attachments "+", select or drag a file, and select how you want it to be presented (end of message thumbnail is usually safest), and that does it. If you did that, maybe there's a newbie delay of some kind. I just don't remember.

author: penright
author_rank: Contributor
timestamp: March 23, 2020, 09:56:17 pm
content: I hate trashing this thread, but a guy got to learn.Maybe it does not show up on preview, so I am posting it anyway.EDIT: Looks like it on from my perspective, but so are the others.

author: Peabody
author_rank: Super Contributor
timestamp: March 24, 2020, 03:51:08 am
content: I now see all of the pictures except the ones on replies #8 and #10.In reply #14, your BS208 is drawn backwards. The pin numbers are correct, but the drawing is backwards. See my drawing in reply #5.As I said before, I don't think a 12V supply is right for this. You will be dropping over 8V of that across the regulator, which will waste most of the battery power and generate a lot of heat.

author: penright
author_rank: Contributor
timestamp: March 24, 2020, 12:20:16 pm
content: Quote from: Peabody on March 24, 2020, 03:51:08 amGood catch. I must have flipped it when I was trying to rotate the pins to help draw it.Quote from: Peabody on March 24, 2020, 03:51:08 amI probably will go back to the 3.3 coin type. It will save on parts.My first run at it did not work. Not sure if I have constructed it wrong, but as soon as I plug in the battery, I get 3.3 at the ESP. I read where P-channel from overseas can have issues. I was trying to breadboard one to test. I expect + to the source with the 1m resistor between gate and source and my meter between - and drain, to read 0V and when I ground the gate, to read battery voltage.It looks like Lucy has some more studying to do.Thanks again for your help and I am sure it will not be my last question on this subject. If I don't make any progress, I will take some pictures.

author: Peabody
author_rank: Super Contributor
timestamp: March 24, 2020, 02:58:00 pm
content: When you first connect the battery, there is a possibility that the mosfet can turn on. That's because the gate is at ground until the resistor can charge up the gate capacitance. But it should not stay on. If it stays on, then something else in your circuit is grounding the gate.

author: penright
author_rank: Contributor
timestamp: March 24, 2020, 05:32:19 pm
content: I am trying to breadboard it.If I have the part right, then Source is on the right, Gate middle, and Drain left.I expect when the gate is grounded, the Drain to tied to the + battery (switch on).If I move the red wire to the Drain, the led will light up.But if I short the gate to ground it does not.Also if I put a meter reading the battery voltage when the gate is high I see the battery voltage, when I short gate to ground, the voltage drops to almost 0v. It is almost like the BS208 is shorting the battery positive to the ground.

author: Peabody
author_rank: Super Contributor
timestamp: March 24, 2020, 05:44:56 pm
content: Notice that the datasheet is showing you the bottom view of the pinout. You can also use your multimeter with nothing else connected to the transistor to find the direction of the body diode, which will tell you which pin is source and which is drain.