2
\$\begingroup\$

I tried looking for an answer for several days, but didn't have any success, so I thought I would ask here. I apologize in advance if this was already answered, and that I was not able to find it in this stack exchange.

I'm trying to use a SP3T switch to connect GND to 2 GPIO of a MCU, from which I will then try to ready LOW (the MCU would setup the pin in read mode, with a pullup resistor), to determine in which "mode" the device is in / would boot into (Normal or Configuration).

I also want to piggy back on these position selection on the switch to connect the MCU to GND to complete the circuit and power the MCU (already connected to Vcc).

To summarize :

  • 1 position connected to GPIO and GND on the MCU (Normal mode - MCU powered on)
  • 1 position connected to nothing (No mode - MCU powered off)
  • 1 position connected to another GPIO and GND on the MCU (Config mode - MCU powered on)

If I connect wires without any other component, both GPIO end up connected to GND, making the MCU think it's in both modes (which is wrong and unwanted) :

enter image description here

So this version doesn't work, I tried to come up with something else based on transistor, and then P-channel MOSFET (only because the project will be powered by a battery, and I wanted to avoid unnecessary power loss with the resistor on the transistor's base, which is avoided with MOSFET if I read correctly) with the following design :

enter image description here

with now the wires connected to the GPIO driving the gate of their corresponding MOSFET, it should allow the MCU's ground pin to then be connected to GND, completing the circuit.

Is this design correct or am I making a mistake somewhere? Maybe there's another way? a simpler way? I'm a bit out of my depth here so any recommendation or advice would be appreciated.

\$\endgroup\$
5
  • \$\begingroup\$ First image doesn't work you're right. You should have put pull-up resistors not pull-down Have you tried simulating your circuit ? The second one also won't work. To clarify on first image you shouldn't have the direct ground wire \$\endgroup\$
    – Mat
    Commented Feb 14, 2022 at 14:02
  • \$\begingroup\$ This would be much easier with a double pole switch. I'm assuming it's an on off on switch? But the concerns about other parts also grounding the mcu is still valid. \$\endgroup\$
    – Passerby
    Commented Feb 14, 2022 at 15:14
  • \$\begingroup\$ Normal or Configuration only requires a SPST switch connected to one data pin ... a movable jumper would also work \$\endgroup\$
    – jsotola
    Commented Feb 14, 2022 at 16:15
  • \$\begingroup\$ I want the switch to handle the power state of the device AND its mode. A simple SPST switch would only handle the mode. \$\endgroup\$
    – Haerezis
    Commented Feb 14, 2022 at 18:25
  • \$\begingroup\$ Do you need the MCU to actually powered off, or would simply holding it in reset be sufficient? I'm not sure what the power consumption is when held in reset. \$\endgroup\$ Commented Feb 14, 2022 at 22:37

4 Answers 4

1
\$\begingroup\$

The only way to do it using a single pole switch breaking the MCU ground is to have some sort of electronic switching like you were trying to do with the MOSFETs or use diodes and their associated voltage drop in the ground path.

Would it be possible to trigger one of the sleep modes instead of breaking the MCU ground?

Another alternative is a double pole switch. You wouldn't need it to be triple throw though, as you are only trying to detect 2 states so you should only need one I/O pin for that. A center off DPDT switch where one pole grounds the MCU in both positions and the other pole grounds one of the GPIO pins in only one position should do it.

Update: I'm looking at a data sheet for the ESP-12F and it appears that there are two boot modes, download and run. The difference between them is for download mode GPIO0 is held low. I'm not sure if that's what you mean by config mode, if it is you should be using that pin instead of GPIO4&5.

\$\endgroup\$
4
  • \$\begingroup\$ Thank you for your comment. I was trying to avoid diode to avoid useless power dissipation (the project being powered by a battery). Also with my initial idea that would have used diodes (and not trying to connect GND, but +3.3v), the diodes would have been between 3.3v and the Vcc pin, resulting in a voltage drop across the diode that would have made the voltage too low for what I was trying to do. \$\endgroup\$
    – Haerezis
    Commented Feb 14, 2022 at 18:10
  • \$\begingroup\$ When I'm talking about mode, I'm talking about custom mode, for the internal logic of the MCU program. About using double pole instead of triple, I can't do that, the goal is to have the device powered off on one the position, and powered ON on the other (and booting/executing in different mode) \$\endgroup\$
    – Haerezis
    Commented Feb 14, 2022 at 18:10
  • \$\begingroup\$ @Haerezis I think we may be using different terminology for the switch type. You originally proposed a SP3T which has one common connection (the pole) that connects to one of 3 connections (the throws). Since you don't need any connection in the off position you can use a double throw 'center off' switch, it has three positions but only two of them make a connection, these would be cheaper and easier to source than a triple throw. By making it double pole as well you have two separate switches to work with in one package. \$\endgroup\$
    – GodJihyo
    Commented Feb 14, 2022 at 19:54
  • \$\begingroup\$ @GodJihyo thank god for your comment. So it seems like I'm just a big dummy, and I was trying to make my own life more difficult for no good reason. I never really understand switches naming convention, but your comment made me take a step back and take another look. It seems like a DPDT with an off center would work like a charm. So I'll just use that. I'm still curious if a design like this imgur.com/a/ypvttPS would work (it seems to work on paper when I look at it, but I'm no expert). THANKS YOU for making me open my eyes... \$\endgroup\$
    – Haerezis
    Commented Feb 14, 2022 at 20:50
3
\$\begingroup\$

Consider this: -

enter image description here

When you have your switch in the middle position you could detect this in code and put your MCU into deep-sleep mode. That is more preferable than trying to turn off the power to the MCU via a ground pin because any other IO pin connected to ground is going to partially activate your MCU.

I urge you to think about dropping into a deep-sleep mode because, if you have IO connections to other chips in your circuit (that might also output 0 volts), then it's likely you'll never achieve what you want; the MCU might remain powered through those grounded IO lines. And, it might do daft things too.

The same applies if you try and disconnect the VCC line - external IO lines to/from other circuits may have these IO lines at Vcc voltage and they will likely still cause your MCU to keep operating.

\$\endgroup\$
5
  • 1
    \$\begingroup\$ That doesn't break the ground for the mcu so it will be powered in the middle/off mode still... \$\endgroup\$
    – Passerby
    Commented Feb 14, 2022 at 14:33
  • \$\begingroup\$ Oh, so that's what the OP was driving at with all that complication @Passerby - OK I shall have a rethink. \$\endgroup\$
    – Andy aka
    Commented Feb 14, 2022 at 14:42
  • \$\begingroup\$ Yeah sorry, I was a bit nervous when writing the post. I want the MCU off on the middle position, and on on all other, with the difference being that on pin 1 I want (for example) the device to boot up in normal mode, and on pin 4 to boot in config mode. \$\endgroup\$
    – Haerezis
    Commented Feb 14, 2022 at 17:54
  • \$\begingroup\$ @Haerezis that's no problem. No need to apologize but, do you see the problem I'm alluding to of switching either power rail AND, can you not going into some deep-sleep mode that keeps current very low? \$\endgroup\$
    – Andy aka
    Commented Feb 14, 2022 at 18:40
  • \$\begingroup\$ I'm already using the deep sleep mode in my "normal" mode. I wanted for the OFF position to really be an electrical off. I've had another idea with the someone else's comment, to use a single mosfet pulled up on the gate, and connected to GND via the middle position of the switch : imgur.com/a/ypvttPS what do you think? \$\endgroup\$
    – Haerezis
    Commented Feb 14, 2022 at 19:05
0
\$\begingroup\$

Neither image will work.

In first image you have already grounded both pins so the switch can't do anything, both pins will always read low.

The second image is even more wrong. You have passed the MCU ground pin via FET body diodes so the MCU will receive just less voltage as it is not connected to ground directly.

Just use the first image but don't forcibly pull GPIOs to ground. Have a pull-up resistor instead. Or use internal pull-up.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Yes, I'm aware the first one is wrong, I just wanted to try and demonstrate what was my naive "first" idea. I thought it could help people understand what I wanted to achieve. I guess I was wrong :/ The idea was having a position on the switch where the MCU would be off, and other positions the MCU would be ON, but would boot in different "mode" depending on the switch position (read on the gpios). The main problem with the first one is that enable one gpio would also enable the other because they are connected by GND, I was trying to not do that. \$\endgroup\$
    – Haerezis
    Commented Feb 14, 2022 at 18:01
0
\$\begingroup\$

enter image description here

It might be easier to switch the 3.3V supply instead.

When the switch is in the middle position the 1M resistor keeps the P-Channel MOSFET off without any current draw.

When the switch is in one of the other positions the gate is connected to ground through one of the diodes, turning the FET on.

You'll need a P-Channel MOSFET that can handle the current draw of your module at a ~2.5V Vgs.

\$\endgroup\$
3
  • \$\begingroup\$ That might not work. When switch is in the middle position, there will be current flowing via MCU protection diodes into MCU which will try to clamp current and keep voltage on IO pin low, which turns on the FET. Pins of an unpowered chip should never be pulled high. \$\endgroup\$
    – Justme
    Commented Feb 14, 2022 at 18:20
  • \$\begingroup\$ I like your idea, but it made me think about another version, let me think what are your thoughts. The idea is : schematics. On position 4 & 1 of the switch, the mosfet gate is connected to a pullup resistor activating the mosfet, thus the MCU is connected to GND (and the device is ON). On position 2 of the switch, GND is connected to the gate, making the mosfet closed thus disconnected GND for the MCU (thus the MCU is OFF). I'm just trying to avoid diode and unnecessary forward voltage drop power dissipations. What do you think? \$\endgroup\$
    – Haerezis
    Commented Feb 14, 2022 at 18:24
  • \$\begingroup\$ Haerezis That might work. You have to reverse the MOSFET though. Source to ground, Drain to the micro GND pin. \$\endgroup\$
    – Dutch2
    Commented Feb 14, 2022 at 20:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.