2
\$\begingroup\$

I want to create a circuit to switch loads (e.g.: led lights).

Following requirements have to be met:

  • the load should keep it's state when the microcontroller goes to sleep/powers off
  • the load could have a different power source (e.g.: 12V)
  • the circuit should draw as less power as possible because it is powered by battery

I came up with following idea:

  • use a 555 timer in bistable mode to keep the state in deep sleep
  • use a irlz44n mosfet to switch the load

This is how my circuit looks right now:

schematic

Explanation of pins:

  • VCC will be 3.3V of my microcontroller (esp32)
  • GND is shared between 3.3V and GND of Load
  • TRG is connected to microcontroller GPIO to switch power on
  • RST is connected to microcontroller GPIO to switch power off
  • Load is connected to Power Source and load-components (e.g. LEDs)

Questions:

  • does this circuit work as expected
  • do i need a gate resistor?
  • do i need a gate source resistor? when i tired the mosfet standalone the output toggled when small voltages where applied (e.g. touching the pins) but i switched it with the timer this did not happen anymore
  • are the components chosen wisely?
  • how could i modify this to replace the LOAD pin with VIN and VOUT which is then connected to the load components?
\$\endgroup\$
11
  • 2
    \$\begingroup\$ most microcontrollers have sleep modes where the pins keep their state, which solves half of the problem. If all you need is a latch for the power-off-case, a 555 is just a very complicated way to do this. I'd strongly recommend using something else. \$\endgroup\$ Dec 18, 2021 at 0:34
  • 1
    \$\begingroup\$ If you have basic logic gates lying around: a buffer (alternatively: an AND where you tied one input to VCC, or an OR or XNOR where you tied one input to GND) with its output fed back through a large resistor to its input, and the same input is connected to your microcontroller pin. This assumes your microcontroller lets its outputs float when off. Pay attention to not accidentally back-feed the microcontroller this way! \$\endgroup\$ Dec 18, 2021 at 10:58
  • 1
    \$\begingroup\$ You can, however, just buy latches Latches or Flipflops, depending on whether you want to have a clock input at which the output changes (ff) or not (latch). \$\endgroup\$ Dec 18, 2021 at 11:17
  • 1
    \$\begingroup\$ For 0.40€ apiece for an octal latch, your 555 solution is way more complex and expensive - not only do you need the 555, you also need two MCU outputs per output; with an octal D-latch, you can work with 1 MCU GPIO per output, and one "latch enable" for all outputs combined. Other options involve a serial-in, parallel-out shift register (which means you can have as many outputs you want with 3 GPIOs), or, honestly: \$\endgroup\$ Dec 18, 2021 at 11:20
  • 1
    \$\begingroup\$ simply never shutting off your microcontroller, as if you're switching any significant load, then the current consumption of an idling or sleeping (fix your sleep mode, your MCU can do that, "I haven't found the way to use it properly" is not a good excuse!) MCU will not matter at all. E.g. your LED uses 50 mA. Your ESP32 in "light sleep mode" preserves its output states and uses 0.8 mA, so 1.6% of the overall power. Forget about using external latching! You'll never notice the difference in battery lifetime! \$\endgroup\$ Dec 18, 2021 at 11:21

1 Answer 1

3
\$\begingroup\$

Your circuit isn't great. You need gate drive resistors, and mostly, the 555 is not the right chip if you want a latch, and it's painful when you need many.

So, instead, you could, if you have basic logic gates lying around, use a buffer¹ with its output fed back through a large resistor to its input, and the same input is connected to your microcontroller pin.

This assumes your microcontroller lets its outputs float when off. Pay attention to not accidentally back-feed the microcontroller this way!

You can, however, just buy Latches or Flipflops, depending on whether you want to have a clock input at which the output changes (ff) or not (latch).

For 0.40€ apiece for an octal latch, your 555 solution is way more complex and expensive - not only do you need the 555, you also need two MCU outputs per output; with an octal D-latch, you can work with 1 MCU GPIO per output, and one "latch enable" for all outputs combined. Other options involve a serial-in, parallel-out shift register (which means you can have as many outputs you want with 3 GPIOs), or, honestly:

Don't use an external latch at all. Your microcontroller has that integrated.

Simply never shut off your microcontroller, as if you're switching any significant load, then the current consumption of an idling or sleeping (fix your sleep mode, your MCU can do that, "I haven't found the way to use it properly" is not a good excuse!) MCU will not matter at all. E.g. your LED uses 50 mA. Your ESP32 in "light sleep mode" preserves its output states and uses 0.8 mA (datasheet, Table 6 on page 31), so 1.6% of the overall power. Forget about using external latching! You'll never notice the difference in battery lifetime!

You can even go to ESP32's deep sleep mode (espressif idf's gpio_hold_en(YOUR_GPIO); gpio_deep_sleep_hold_en(); allows you to do retain state, according to documentation), and then you're down in the microampere range – i.e. you could operate from a coin cell for years. So, seriously, you need none of this, but to figure out your software!

Overall recommendation

  1. Add the ability to go into deep sleep mode to your firmware. The ESP framework by espressif does make that possible, and it allows the outputs to hold state while the microcontroller consumes < 100 µA.
  2. Use logic-level MOSFETs. Your choice of MOSFET isn't great, you need something that fully turns on at 3.3 V.
  3. Connect the gate of your MOSFET directly to your ESP32, through a gate resistor (quite possibly in the kΩ range)
  4. Add a linear regulator to 3.3V (and Schottky diodes to avoid backfeeding, to both your battery and the regulators output) from your LED power source. So, as long as your LEDs have power, you don't need to worry about the MCU not having power to remain in a light or deep sleep mode.
  5. Since you say this is battery-powered, review your overall power system design. Chances are you really only need one power source – for your LEDs, since they are the dominant consumer of power. If you've got a battery that powers these, chances are you have a switch-mode power supply already; make sure you're not doing something "interesting" like stepping up voltages (e.g. to 5V) to then linearly waste a lot of power again – the ESP32 needs no more than 3.3 V of VCC; every voltage above that just gets converted to heat internally.
    Maybe you can even just completely shut down everything while that power source is not available – after all, nothing to light up without! In that case, store the output state you have in nonvolatile memory (the ESP32 comes with some) and restore on power-on; the user will never even know the ESP32 reset in between!

¹ alternatively: an AND where you tied one input to VCC, or an OR or XNOR where you tied one input to GND

\$\endgroup\$
6
  • \$\begingroup\$ Use logic-level MOSFETs: do you have any recommendations for me? \$\endgroup\$
    – warch
    Dec 18, 2021 at 12:12
  • 1
    \$\begingroup\$ farnell.com -> your region -> Semiconductors -> discretes -> FETs -> single (or dual) MOSFETs -> Select devices with a "Threshold Voltage Vgs" below 2.8V; sort by price. Check the datasheet for the V_DS at the current you want to operate at. This is something you should be doing anyway, you need to buy these in the end, nobody can really take component selection off your shoulders. \$\endgroup\$ Dec 18, 2021 at 12:15
  • \$\begingroup\$ Generally, the filters at component distributors are very helpful. For example, set a sufficiently high continuous drain currrent. \$\endgroup\$ Dec 18, 2021 at 12:20
  • \$\begingroup\$ But i dont really get it... The mosfet i chose (IRLZ44N) has a Vgs threshold of 2V. What is wrong then with my choice? at.farnell.com/infineon/irlz44npbf/mosfet-n-55v-41a-to-220/dp/… \$\endgroup\$
    – warch
    Dec 18, 2021 at 12:22
  • \$\begingroup\$ Regarding power system design: my goal is to extend a simple cheap led strip with wifi. I have some led strips at home witch are powered by 3x AA Batteries. I added a MCU which is powered separately (currently esp8266, a tp4056 module, a 18650 li-ion battery, a mcp1700 ldo) and i want to improve battery life and make a custom pcb to keep the footprint small. I wan to keep the pcb independent from the switched circuit so that i can use it for other devices as well (extend simple devices with wifi in order to control it over home assistant) \$\endgroup\$
    – warch
    Dec 18, 2021 at 12:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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