ESP32-C3 latch circuit - Page 1
Forum section: electronics / beginners

author: pettirosso
author_rank: Contributor
timestamp: April 29, 2023, 02:23:13 pm
content: Dear All,I am attempting to make a door sensor where the ESP32-C3 powered by a 14500 lifepo4 starts when the door opens (magnet moves away from the device), sends a quick message via wifi, then terminates using a latch circuit.Any attempt at using mosfets to date has resulted in magic smoke, so to get started I believe that I copied a design for the latch circuit verbatim from an internet forum (unfortunately I have lost the source for now). Unfortunately the PCB took a very long time to come and in the mean time I have forgotten any rationale behind the design.Trying to work out what is happening:With 3.3V applied the gate of the AO3401A should be pulled high via the 2M2 resistor, allowing current to flow through AO3401A to the ESP32C3 3.3V input.Pin 19 on the ESP32-C3 should be high on boot, until it is set as an output and low, this would then open the 2N7002 pulling the gate of the AO3401A to ground and turning off the power to the ESP32.The ESP32-C3 is currently powered and working fine/can be programmed as if it is connected directly to 3.3V and GND. At present there is no voltage across the reed switch/no effect in absence and presence of the magnet, and the net connected to GPIO19 stays high even when it is driven low.I was wondering if anyone could possibly very kindly tell me at a glance if this design has any chance of working (by modifying the circuit/direction of the mosfets), or is it completely beyond hope?Many thanks,Robin

author: Peabody
author_rank: Super Contributor
timestamp: April 29, 2023, 02:38:36 pm
content: A P-channel mosfet is turned on when its gate goes low relative to its source. But I think the main problem is that your AO3401A P-channel is oriented backwards in the schematic (I don't know about the board layout). The source should be connected to the battery, and the drain to the load.Then you would want to be sure the switch grounds the gate when the door is opened, which would turn on the mosfet. But note that even if the GPIO output is taken low, the power will not shut down until the door is closed. In fact, if the door will always be open long enough for the processor to do its business, there's no need for the 2N7002 circuit at all.And I think I would eliminate R2. It shouldn't be needed, and could prevent the power from shutting down.

author: tooki
author_rank: Super Contributor
timestamp: April 29, 2023, 02:51:05 pm
content: Quote from: pettirosso on April 29, 2023, 02:23:13 pmQ1 is backwards, so it'll always conduct through the body diode. If your other MOSFET circuits also used backwards MOSFETs, you might have been inadvertently causing direct shorts.

author: pettirosso
author_rank: Contributor
timestamp: April 29, 2023, 03:12:54 pm
content: Thank you to you both for your quick responses.Based on your comments I will try the following modification to correct the direction of the mosfet (at least!) and see what happens.With your help if I can finally begin to understand how to use mosfets or at least connect them in the right direction I will be very happy indeed!

author: tooki
author_rank: Super Contributor
timestamp: April 29, 2023, 04:06:00 pm
content: Yeah, give that a try!

author: pettirosso
author_rank: Contributor
timestamp: April 29, 2023, 05:33:01 pm
content: I made the change and replaced the AO3401A in case it was damaged but no joy unfortunately.After the change the ESP32-C3 is off by default, however the voltage across the reed switch with R1 at 2M2 is now 1V (and at the AO3401A gate). I tried changing R1 to 1M, 500K and 300K giving voltages of 1.65V, 2.18V and 2.45V respectively across the reed switch and at the gate, however this did not activate the AO3401A/turn on the ESP32-C3.I think this looks like it could have been the circuit that I tried to reproduce, not sure where the difference is now if I have managed to connect the components correctly:https://youtu.be/nOGPfMO0PJYBest,Robin

author: pettirosso
author_rank: Contributor
timestamp: April 29, 2023, 06:04:38 pm
content: Looks like I completely misunderstood the application of the circuit presented in the video, the switch is present to start the sequence and probably can't be replaced by a reed switch as I have it (not sure what I was thinking).Thanks again for your help, I think from this I am perhaps starting to get an idea of how these things work. I will go back to the drawing board ;-)Best,Robin

author: tooki
author_rank: Super Contributor
timestamp: April 29, 2023, 06:55:54 pm
content: The video description includes the github link for the schematic and board files:https://github.com/pcbreflux/espressif/tree/master/esp32/kicad/ESP32-bat-monitor-latchAnd if you look at the schematic, we see a few things:1. Their pin numbering on the MOSFETs is wrong. Lucky for them, it was wrong on both schematic and footprint, so the board came out as they intended! Yours appear to be drawn correctly.2. It has a second P-channel MOSFET as reverse protection. It looks like you copied that one's orientation instead of the switch.3. Well, if a 2.2M resistor as the top resistor of a voltage divider is giving you about 1/3 the voltage, that means the reed switch is acting as a roughly 1M resistor, which is obviously far, far,fartoo much contact resistance for a closed switch, and far, far,fartoo little resistance for an open switch. Or perhaps the reed switchisopen, but you're getting leakage through the 2N7002: (3.3V-1V)/2.2Mohm ≈1µA, which is within the 0.01...1µA drain-source leakage current specified in the 2N7002's datasheet.Moreover, if there's any residual voltage whatsoever on the gate of the 2N7002, then it could conduct more. That's easy to test by shorting its gate to ground. Because you have a big electrolytic (and a few ceramic) capsafterthe switch, then any current through R2 (if you haven't removed it yet) or through the ESP32 could pull Q2's gate up slightly.What happens to the voltage at the gate of Q1 when you close the reed switch?

author: tooki
author_rank: Super Contributor
timestamp: April 29, 2023, 07:08:04 pm
content: Quote from: pettirosso on April 29, 2023, 06:04:38 pmWhat makes you think that? It doesn't matter how long the reed switch is closed; as long as the ESP32 keeps the shutdown line low until it actually loses power, it should be fine.A more basic question perhaps: where did you source your components? Just to rule out that they could be eBay/AliExpress parts thatclaimto be 2N7002 and AO3401A, but in fact are something else...

author: pettirosso
author_rank: Contributor
timestamp: April 29, 2023, 07:57:00 pm
content: Thanks Tooki, if you think there is hope I will pursue this!R2 was removed while switching the AO3401A. When decreasing R1 I thought R1 and R3 must be acting as a voltage divider. Reducing R3 by half to 1M didn't seem to affect the value, which I thought was strange, but perhaps this is related to the other sources of resistance which I think you are suggesting.I will make a fresh board with all new components in case I have damaged anything with the experimentation, perhaps the 2N7002 could be suspect at this point, all parts are from DigiKey so I hope the components are genuine.For the transistors these are the specific parts/manufacturer:https://www.digikey.it/en/products/detail/alpha-omega-semiconductor-inc/AO3401A/1855773https://www.digikey.it/en/products/detail/diodes-incorporated/2N7002K-7/1934378Reed switch:https://www.digikey.it/en/products/detail/standex-meder-electronics/SW-GP560-20-25-AT/3979782Ceramic capacitors are samsung, and resistors panasonic apart from the 2.2M which is Yageo.I will make the new board (without R2 and without the electrolytic capacitor) and report the voltage at the gate of Q1 when the reed switch is closed.Thanks,Robin

author: tooki
author_rank: Super Contributor
timestamp: April 29, 2023, 08:34:15 pm
content: From Digi-Key you can be sure it's genuine, so we can rule that out.Can you test a board without the ESP32 on it? (But with R2, since it's what latches it on*.) That should, in theory, work just like the breadboarded version in the video.If possible, I would populate only R1, R2, R3, Q1, Q2, and the reed switch. No caps, no MCU, nothing else. Just the absolute minimum to test the latching circuit.*Without R2, the microcontroller has to power up and pull the gate of Q2 high before the "power on" switch (the reed switch in your case, the first button in the video) goes open again. Many devices use that approach. With R2, it's completely self-latching.

author: Peabody
author_rank: Super Contributor
timestamp: April 29, 2023, 08:57:19 pm
content: I would think that if this is to be triggered by a door opening, there would be no need for R2. Surely a physical door would stay open long enough for the ESP32 to boot up. In fact, I bet you would have a hard time closing it quickly enough to prevent it booting up.I think the 2.2Meg resistors are way too high to work well. I normally use 100K for pullup and pulldown resistors.If the gate voltage is anything lower than battery voltage when the switch is open, then either the switch is leaking or the 2N7002 is leaking, or perhaps partially on. Can you measure the resistance across an open switch with your meter (out of circuit)? And the resistance across the drain and source of an out-of-circuit 2N7002, with the gate connected to the source. Both should be infinite on most meters.The other thing, unless I misunderstand, is that if the voltage across the switch is 1V, and the battery is 3.7V or whatever, the power should be on. However, with the high-value resistors, the presence of your meter could be distorting the readings. So maybe it's not really 1V. I'd like to see you get this working with 100K resistors first, then worry about making them larger.

author: tooki
author_rank: Super Contributor
timestamp: April 29, 2023, 10:49:18 pm
content: Quote from: Peabody on April 29, 2023, 08:57:19 pmI didn't say that it wasn't fast enough. But the MCU is definitely not gonna boot fast enough to latchif the MCU isn't even installed: I was simply explaining how the circuit worked, to explain why R2 is mandatory with the MCU unpopulated in the isolated test I suggested they do. But moreover, even with the MCU, this isn't installed on a door yet, it's being tested manually.I also just realized that OP's reed switch is normally open, which isn't gonna work for their application. But it'll do for testing the circuit.Quote from: Peabody on April 29, 2023, 08:57:19 pmDid you watch the video? It explains the rationale behind the component values and principles of operation of this circuit.

author: pettirosso
author_rank: Contributor
timestamp: April 30, 2023, 08:24:56 am
content: Thanks both,I made a new board (ESP32-C3 in place, no R2, AO3401A orientation corrected) and it behaves the same way, 1V on the multimeter at the gate but no activation. Shorting the 2N7002 or AO3401A gates to ground did not have any effect.For some reason I must have thought this would work with a normally open switch, but even if it was the case this would result in draining the battery through the 2M2 resistor when the door is closed (albeit at low current). Unfortunately the cheapest normally closed reed switch available at digikey was about 3 EUR (https://www.digikey.it/short/w303n53v) vs ~0.7 EUR for the normally open. To be advantageous against a commercial solution I hoped to stay below 8 euros for the total cost, hence the drive (perhaps wishful thinking) to avoid a normally closed reed switch, but this is a secondary concern at this point! For this specific part given the potential saving I will try Aliexpress where they are available at 3 EUR for 10 pieces, but otherwise I will get the component from digikey. With that it would probably be possible to cut out the latch circuit and use deep sleep when the door/window is open, which would allow periodic checking to see if it is still open, but I would like to stick to the initial plan with the latch circuit in place if it is possible to get it working. Having both options available would probably be good depending on the effect of how often each door/window is opened/left open on the battery life.I will make the circuit on a breadboard without the ESP32-C3/caps and with 2M2 and 100K resistors for testing, and get the values for the out of circuit component resistances. It will be nice to observe the AO3401A switching an LED at least then can build up again from there!Thanks again,Robin

author: pettirosso
author_rank: Contributor
timestamp: April 30, 2023, 10:43:15 am
content: I tried an isolated AO3401A with the gate driven from a voltage divider with 1M resistors. The LED illuminated marginally at 1.4V and strongly by 1.2V and below, so presumably that should mean that the 1V at the gate (if true) obtained in the full circuit with the 2M2 resistors should be enough to drive the circuit. I am very happy to have switched my first mosfet in any case! It was brilliant to see when I removed the cable that it did have a sort of memory effect on the level of activation.I then made the minimum board with R1, R2, R3, Q1, Q2, and the reed switch, with an R220 resistor and LED at the site of the polarised capacitor (on the load off the drain) to visualise on/off. The AO3401A was not open in the basal state, and there was no effect on pulling the 2N7002 gate high or low using the equivalent of the GPIO19 line. Surprisingly closing the reed switch with a magnet resulted in a short circuit of the breadboard power supply (I don't think this is expected, the current must then be coming through the R2 100K resistor or the AO3401A source to gate).I will have to leave it for now but can make a plan and try again next weekend. I think it might be worth considering making a couple of new/different designs perhaps with different components.Thanks again for all of your help, I wouldn't have known at all where to start trying to troubleshoot this one.Best,Robin

author: Peabody
author_rank: Super Contributor
timestamp: April 30, 2023, 01:20:57 pm
content: You're getting some odd results that indicate you may not have things connected the way you think. So you might want to go over the connections again, particularly the mosfet pinouts.Isn't the switch a magnetic switch? Can you rotate it to make it open when the magnet is near? You want it to close when there's no magnet (the door is open). I don't know if that's "normally" open or closed, but perhaps what you have can be made to work.

author: pettirosso
author_rank: Contributor
timestamp: April 30, 2023, 01:55:03 pm
content: Unbelievable, I think you are right - it looks like I still haven't connected the AO3401A correctly on the boardAll fingers crossed this is the issue!I have triple checked the 2N7002, I think this is correct.

author: pettirosso
author_rank: Contributor
timestamp: April 30, 2023, 03:16:12 pm
content: Problem solved, the circuit works perfectly now! Starts with the magnet placed then runs until the 2N7002 gate is brought low either manually or by GPIO19.Thank you so much for your help, and sorry for being a complete numpty!Best,Robin

author: Peabody
author_rank: Super Contributor
timestamp: April 30, 2023, 06:58:13 pm
content: I'm glad you got it working. But I still not clear where you stand with the switch. If the switch is next to the magnet when the door is closed, then you need a switch which is closed when the magnet is far away, and open when near the magnet. Is that what you have?I would still remove R2 when you connect the ESP32. When you take the GPIO pin low to shut off the power, the drain voltage on the P-channel will drop gradually. At some point, the ESP32 will shut down, and the GPIO pin will go tristate. It the drain voltage is still high enough at that point, R2 could then turn on the N-channel, and power would come back up.

author: james_s
author_rank: Super Contributor
timestamp: April 30, 2023, 07:12:29 pm
content: Doesn't the ESP32 support deep sleep? You should be able to have it wake up by a state change of the reed switch and then go back to sleep after it finishes sending the message. I've only ever used ESP parts for line powered devices but I've used AVRs in battery powered applications that run for years.

author: pettirosso
author_rank: Contributor
timestamp: April 30, 2023, 09:14:10 pm
content: I agree the normally open reed switch does not appear to be suitable for the intended purpose in this circuit, I think it will need to be normally closed so that the circuit is held off when closed/next to the magnet and then activates the circuit when opened, unless anyone has any suggestions/clever tricks. I have ordered the normally closed switches from Aliexpress and will give them a try as they are 10x less than the ones on DigiKey, but will add a few to my next order for comparison. The lower cost ones would keep the total on track at about 8 EUR per board. I say it is working perfectly assuming the change to the normally closed switch will work as expected!Quote from: Peabody on April 30, 2023, 06:58:13 pmI have kept R2 off and it is working with the ESP32 (the GIF doesn't seem to play unfortunately), so will leave it off, thanks for this!I will try the deep sleep method for comparison, there could be an advantage to waking every so often on a timer when the door/window is open. I thought keeping the power completely off as long as possible would be the best option for battery life (using fairly low capacity 500 mAh 14500 LiFePO4). I'm hoping that the ESP32 will be ok powered directly off the LiFePO4 battery charged to 3.6V, I don't have any voltage regulation. It could be worth adding one as it would be good to be able to track the battery voltage via a voltage divider to the adc against a reference (I assume this is a reasonable way of doing this), otherwise will just have to get a feel for how long they last and charge them every so often.Currently charging a LiFePO4 battery to test, so will have to see how it goes. But the latch circuit at least is working brilliantly so thanks again to you both for your patient guidance, absolutely saved the day!

author: tooki
author_rank: Super Contributor
timestamp: May 01, 2023, 08:25:32 am
content: Quote from: james_s on April 30, 2023, 07:12:29 pmYes it does, but in a strange way: it basically reboots when waking from deep sleep. So while you can use deep sleep to essentially configure what pins or timers will wake it, your code doesn’t pick up where it left off unless you expressly save the state to Flash or an external EEPROM and then restore the state from that.My boss lashed together an ESP32 with deep sleep and some kind of sensor to monitor a door that shouldn’t be opened, but where someone was going to smoke, resulting in smoke and dank basement air being sucked into the air handler for the entire building. IIRC it’s still on the same charge (of a tiny LiPo) as when it was installed months ago.(The ESP32 light sleep mode retains state.)