3 ms·
That sounds really dangerous. You're depending on circuitry built to protect the microcontroller, to limit the current. Sounds like a good way to kill the LED,
by makoto12 5y ago
That sounds really dangerous. You're depending on circuitry built to protect the microcontroller, to limit the current. Sounds like a good way to kill the LED, but worse also kill the microcontroller. Also I'm quite skeptical at the number of microcontrollers that have this protective circuitry?
- qazxcvbnmlp 5y agoMany modern MCUs have GPIO that can be configured via software to have an internal resistance in line with the GPIO. This resistance will limit current to within the specs of the MCU just fine.
- MayeulC 5y agoTrue that: I know very few microcontrolers with overcurrent protection, you usually permanently disable a pin when draining too much current. I would still use a resistor with it, though having the microcontroller supply the current is usually fine. Protip: microcontrollers can usually absorb more current with pull-downs (due to n-mosfets, plus internal power supply routing) than they can supply. So have VCC -> res -> LED -> µC and turn the LED on by writing a 0 to the GPIO. Make sure the µC doesn't overheat, and you can drive a lot of LEDs (or other circuits) like this. Double check the datasheets for the max ratings, of course.
- tzs 5y ago> So have VCC -> res -> LED -> µC and turn the LED on by writing a 0 to the GPIO. For turning the LED off the obvious way is to write a 1 to the GPIO. But check the documentation for your processor. On many another way to turn the LED off is to change that GPIO from an output to an input. With the LED off you should have very little current through the GPIO but there usually is a little, and I believe that on some processors the "change it to an input" method will have a little less current. It probably won't be a big difference but when you are trying to squeeze as much runtime as you can out of a battery it can be worth it.
- MayeulC 5y agoGood point, I hadn't thought of that for this use! An input has a high impedance, which is to say a very large (giga-ohm) resistance to vcc/gnd: very little current will flow trough. Usually it's connected to the gate of a transistor, which can easily be damaged by high voltages (3V instead of 1.2 can be enough). You can achieve the same effect, but more resilient to high voltages, by switching the output pin to an open drain/open collector mode, which disable the pull-up (pmos) transistor. That gives you high impedance when the pull-down transistor is enabled (ie, 1 is written to the output). This can also be used to control high-voltage logic/LEDs with a low-voltage microcontroller. https://en.wikipedia.org/wiki/Open_collector https://en.wikipedia.org/wiki/Open_collector https://blog.stratifylabs.co/device/2013-10-21-Understanding-Microcontroller-Pin-Input-Output-Modes/ https://blog.stratifylabs.co/device/2013-10-21-Understanding...
- londons_explore 5y agoWhen trying to squeeze runtime out of a battery, you want all pins to be 0v or Vcc with no current flowing. That means that if you're connected to an LED, setting the pin to an input is a very bad plan. The voltage will sit around 1 volt, which will cause internal circuitry within the microcontroller to use substantially more current - in some cases multiple microamps (compared to nanoamps for some microcontrollers sleeping).
- evancox100 5y agoIt’s not protective circuitry, the current limit just comes from the fact that you don't have an infinitely large output MOSFET driving the IO. Usually the driving capability will be spec'd as minimum Iol @ a given Vol, and minimum Ioh at a given Voh (output low current, output low voltage, etc). Typically Iol/Ioh will be in the 1 mA to 5 mA range, sometimes selectable with different drive strengths. Some things to watch out for: Typically the spec'd Vol/Voh would be pretty close to VSS/VDD, for example 0.3 V above/below. If you were really driving the LED hard, you could get a lot more current if you drive the IO harder than that, though the increase isn't linear and eventually you will put the IO MOSFET into saturation, and the current will top out. Also, most specs I've dealt with have just a minimum drive current, so you don't know what the upper end of that could be. Rough rule of thumb: if the minimum is X, the typical might be 2x, and the max might be 4x. This is probably the biggest drawback of this idea, since there is such a huge range to what you'll get. (And note that this affects you even if you DO use an external current limiting resistor. To overcome this you need to actually regulate the current flowing through the LED via a feedback loop.) Also, there may be a total current limit for the micro that is less than the sum of the individual IO drive capabilities, since the micro can only dissipate so much heat internally, or the power rails can only handle so much combined DC current. So you might only be able to drive 10% of the micro's GPIO pins at maximum current. These are all things to watch out for. For a robust/production design, you should probably use another solution, but for tinkering it will probably be fine.
- londons_explore 5y ago> I'm quite skeptical at the number of microcontrollers that have this protective circuitry It isn't protective circuitry as-such... it's the default way to build an output pin circuit[1]. The MOSFETS in typical use act like switches, but they also have a 'saturation' region[2] where they act like constant current drivers, which is exactly what ought to be used to drive an LED. The current flow in the saturation region isn't awfully consistent from one microcontroller to another as it typically depends on temperature and various process parameters. [1]: https://open4tech.com/wp-content/uploads/2019/03/push_pull_schematic-1.jpg https://open4tech.com/wp-content/uploads/2019/03/push_pull_s... [2]: https://i.stack.imgur.com/GFMrw.png https://i.stack.imgur.com/GFMrw.png