Powering devices via gpio pins? - Page 1
Forum section: electronics / general

author: ColCon
author_rank: Contributor
timestamp: September 30, 2019, 07:23:39 am
content: This is about powering 3v3 sensors from 3v3 mcu gpio pins. Specifically, I'm thinking of an encoder and a current sensor. Both will run on 3v and both draw under 5 mA.In the past I've allways run 3v3 and grd lines to sensors. But it seems to me it could be more flexible to be able to power them from gpio pins, in that the same board layout can be used for various sensors.This works but I'm concerned about best practices. Is this a case of "This is very much frowned upon" or is it "This is done all the time"?Thanks everyone,Colin

author: Siwastaja
author_rank: Super Contributor
timestamp: September 30, 2019, 08:21:54 am
content: Note that the equivalent IO pin resistance typically varies between, say, 5 and 50 ohms depending on chip, temperature, etc.If you can guarantee your sensor doesn't exceed the continuous maximum rating, and if you are OK with the voltage drop (50 ohms * 5mA = 0.25V or 8% of 3.3V!), there's nothing to be "frowned upon".Decoupling capacitors will cause the current to shortly exceed the maximum ratings, but this is rarely a problem. If you think it is, there are tricks:* Reduce the decoupling caps to minimum required. For a small IC, 10nF is often enough, instead of 100nF.* Add a bit more extra series resistance* Partially precharge by turning the weak pull-up resistor on before enabling the output. It's likely you'll get to somewhere around 1V before the 3V3 IC starts consuming current (and preventing further precharging through the weak pullup). This way, you have precharged 1/3 through, and you only have around 2V differential left, over the IO structure.

author: voltsandjolts
author_rank: Unknown
timestamp: September 30, 2019, 11:34:53 am
content: It's not good design practice to do this and it is so easy/cheap to add a power switch, why would you risk it?Also, you don't say whether your sensor is 10mm from the MCU or 10m.

author: Siwastaja
author_rank: Super Contributor
timestamp: September 30, 2019, 01:25:51 pm
content: Quote from: voltsandjolts on September 30, 2019, 11:34:53 amSuch "good design practices" totally suck. It's cargo cult engineering.QuoteDone properly, there's no risk.Separate power switch done improperly, there is a risk.There is no power switch solution which works reliably without actual design analysis.Small devices with very low consumption in all operating states and no strict requirement for voltage regulation -> nothing wrong doing it like that. A perfectly OK "design practice".

author: mikerj
author_rank: Super Contributor
timestamp: September 30, 2019, 01:37:15 pm
content: Quote from: voltsandjolts on September 30, 2019, 11:34:53 amExplain why. There is nothing wrong with powering low current devices from a port pin provided you understand the limitations and consequences of exceeding them.

author: voltsandjolts
author_rank: Unknown
timestamp: September 30, 2019, 02:14:09 pm
content: The OP has asked a valid question, and I welcome them to the forum.Quote from: mikerj on September 30, 2019, 01:37:15 pmIndeed.The question suggests they are a relative newcomer to electronics and therefore there are a few 'traps for young players' that might catch them out when powering sensors from MCU port pins. It's wrong to suggest 'its fine' without pointing those out and I'm surprised more experienced folks haven't done that.To the OP, some things to consider:(1) Your MCU power pins will have stated current limit which is less than the sum of rated output currrent for all pins. So, be careful your design doesn't exceed that.(2) You talk about using 'various sensors'. Current requirements may well change when changing sensor, maybe a port pin won't have the current capability. Unlikely it would be a problem if you had an external power switch. Keeps your options open.(3) If the sensors are off-board, connected by long wires (i.e. an aerial) you may see unintended behaviour of the micro (due to RF/noise pickup), more so than using an external power switch. Also, there may be a voltage drop over those wires, so you might want to power at 5V and have a local 3V regulator at the sensor. An external power switch makes that possible.(4) An external power switch can easily provide soft switch-on charateristic if required.(5) If you are using a low pin count MCU, you might be sharing ISP pins and IO pins. An external power switch means ISP could be less problematic.

author: rstofer
author_rank: Super Contributor
timestamp: September 30, 2019, 02:57:31 pm
content: Quote from: mikerj on September 30, 2019, 01:37:15 pmI wouldn't do this kind of thing but from an MCU perspective, we drive LEDs all the time. Usually 10 mA, sometimes as much as 20 mA for conventional red LEDs.I would be more worried about the pin output voltage dropping below an acceptable value for the sensors. Still, it isn't very hard to do the experiment on a breadboard.

author: T3sl4co1l
author_rank: Super Contributor
timestamp: September 30, 2019, 03:29:06 pm
content: In and of itself, no, nothing wrong with that. See above caveats.When else would GPIOs be bad for power? When that power goes down a cable, for example. It's an EMC hazard:- The pin switches quickly, making the cable an effective radiator. Not that you'll be switching power on and off very often, but an edge is an edge.- The pin is sensitive to EMI and ESD. GPIO pins aren't terribly robust.- You can't put much bypass capacitance on the end. More generally, you have the Rds(on) of the high side output transistor, and you shouldn't load it with much more than that, for very long (10s us?). You can often get away with heavy loading or even momentary shorting of GPIOs... but it's not recommended. So, without much time to put a heavy load on there, is another way of saying you can't have much capacitance at the end, and consequently, too low |Z| at the load end.So, if you're doing teensy ~mA sensors, and have some protection diodes and series resistance to protect the GPIO pin, and some RC or LC filtering to keep edges quiet, and maybe a 0.01uF bypass at the far end -- sure, that's a good way to do it, and saves parts over a purpose-made protected load switch.Tim

author: SiliconWizard
author_rank: Super Contributor
timestamp: September 30, 2019, 03:37:08 pm
content: Exactly, we do this with LEDs all the time. I don't see a fundamental problem with it for powering low-power devices.As to the max current GPIOs can provide, look that up in the datasheet, and keep a reasonable margin. If you're powering a sensor that draws a couple hundred microamps peak, seriously, you're never going to have a problem - at least as long as the device in question doesn't FAIL.The only downside I see with this, is with the probability of the device you're powering like this to fail (as a short, for instance). That's a risk analysis to do. You may consider that a typical LED is much more robust than any typical IC, and thus is less risky to power from a GPIO. Key word: risk analysis.Of course simply using an external switch to power external devices won't prevent potential failures. They may just kill your power supply or switch (if it's not well protected) instead of your MCU. Choose what's worst in your case. You may add some current limiting to prevent a complete system failure.But, unless this is a safety-critical, or at least a lifetime-guaranteed product, seriously think twice about even bothering. Just a thought.

author: T3sl4co1l
author_rank: Super Contributor
timestamp: September 30, 2019, 03:48:53 pm
content: Incidentally, depending on how the GPIO is structured -- you may be able to detect a short. One case is simply reading the pin input value periodically: if it's low when it's supposed to be high, something's wrong. If the structure doesn't allow that (if the pin-read operation is internally overridden by the pin-output state), then momentarily switch it to weak-pullup or high-Z mode, read the pin maybe a microsecond later, then set it back. Bypass capacitance will keep the pin high for some time, but a fault will discharge it very quickly.A generalization of this method can be used to measure capacitors or leakage currents, enabling applications from capacitive touch to testing of devices to communication with LEDs alone, and more.Tim

author: SiliconWizard
author_rank: Super Contributor
timestamp: September 30, 2019, 04:19:23 pm
content: Quote from: T3sl4co1l on September 30, 2019, 03:48:53 pmNeat tip.Many MCUs have GPIOs from which you can read the actual state even when they are configured as outputs (they just don't disable the input stage).This is pretty handy here. If you monitor the state frequently enough (best case ever would be if you could configure the GPIO as output AND still be able to enable an interrupt on state change, but if not, you can do this by polling), the risk of damage would probably be pretty low.

author: drussell
author_rank: Super Contributor
timestamp: September 30, 2019, 04:26:43 pm
content: Quote from: SiliconWizard on September 30, 2019, 03:37:08 pmHow many microcontrollers have you fried by just shorting an output pin to ground?

author: Dielectric
author_rank: Regular Contributor
timestamp: September 30, 2019, 04:27:28 pm
content: Quote from: Siwastaja on September 30, 2019, 08:21:54 amI don't think this got enough attention. Because the power is now coming through a somewhat resistive element, the output voltage from your GPIO will vary depending on how much current the sensor draws. It's basically an unregulated voltage source at that point. Some sensors have internal LDOs because they only use 1.8V internally, so it wouldn't be a huge deal there, but others may be using the power rail as a ratiometric reference so your output may wiggle unintentionally.Also, take a quick total of your current draws through the GPIO and check them against the MCU datasheet. There may be a per-pin limit and a per-device limit, so you can't actually pull full current on all of the GPIOs at once.I have never run an actual sensor via GPIO power, but I've gated them via MOSFETs. More modern ones (Bosch motion sensors, for instance) actually do their own sleep management so I let them take care of themselves.I did power an FRAM via GPIO just because I could, but there's an internal core regulator so it didn't really matter. FRAM also draws an exceedingly low amount of current even during an erase or program cycle.

author: ColCon
author_rank: Contributor
timestamp: September 30, 2019, 04:29:41 pm
content: Quote from: T3sl4co1l on September 30, 2019, 03:48:53 pmWhat a great tip. Even if you were to only do it at startup.

author: ejeffrey
author_rank: Super Contributor
timestamp: September 30, 2019, 05:24:56 pm
content: Powering a sensor from a GPIO pin is a perfectly fine and normal thing to do as long as you pay attention to the caveats listed above.One other thing to look for is that some microcontrollers have GPIO options that make this easier. Some microncontrollers have configurable "high drive strength" output modes, usually only available on certain pins. This is usually intended for driving high brightness LEDs, but can also be used to power sensors where the DCR of a normal GPIO pin might too much. The second is adjustable slew rate /speed -- some microcontrollers can be configured to use slower edges to reduce EMI.One thing to watch out for whenever you power gate peripherals is to avoid latchup. If you drive an IO signal high before Vdd is enabled you can cause the target device to latch-up. This is a concern whether you power directly from the GPIO or through an external FET switch, although in this case it will short circuit your GPIO pin. That is helpful if you can test for it as described by T3slco1l, but bad if you blow out the pin.

author: voltsandjolts
author_rank: Unknown
timestamp: September 30, 2019, 09:03:32 pm
content: Looks like we have scared off the OP now anyway

author: ColCon
author_rank: Contributor
timestamp: September 30, 2019, 09:18:23 pm
content: Quote from: voltsandjolts on September 30, 2019, 09:03:32 pmNo no I'm here. This is fantastic.

author: PCB.Wiz
author_rank: Super Contributor
timestamp: September 30, 2019, 11:09:57 pm
content: Quote from: ColCon on September 30, 2019, 07:23:39 amSomewhere between "This is very much frowned upon" & "This is done all the time"If the current is low enough, and the sensor tolerant enough, there is no real reason to not use a port pin. ]LEDs are 'powered from' port pins all the time.

author: westfw
author_rank: Super Contributor
timestamp: September 30, 2019, 11:20:26 pm
content: I'll point out that many 3.3V microcontrollers are not specified to be able to provide "5mA" on GPIO pins, and others may need to have "high drive strength" or something explicitly configured to "allow" higher currents. I check SAMD21, and normal drive strength is 2mA by default and 7mA with the "DRVSTR" attribute configured. IIRC, the early Luminary chips had settable 2/4/8mA strengths.I used a GPIO pin to power the "target" board from a (5V) GPIO pin on an Arduino in the "OptiLoader" sketch. It seemed to work fine, though I started getting nervous about the amount of "stuff" that some target boards were starting to include...

author: Psi
author_rank: Super Contributor
timestamp: September 30, 2019, 11:24:16 pm
content: Make sure you put some capacitors on the IO pin to GND to buffer any high current pulses that the chip might draw.Something like a 10uF and a 100nf in parallel.

author: splin
author_rank: Frequent Contributor
timestamp: October 02, 2019, 12:17:39 am
content: Quote from: Psi on September 30, 2019, 11:24:16 pm10uF seems rather large - it will short out the output pin when first turned on and potentially draw a large current until it charges. Similarly at turn off it could dump a lot of current into the output pin. I doubt that many MCU's would be bothered by this, especially if you don't turn the sensor on/off very often. If you do it frequently then caution is advised - excessive current in metallic (usually aluminium) internal traces in the MCU can cause electromigration where the trace metallization gradually thins, the metal being redeposited elsewhere on the die, eventually with fatal results.If you *really* want to use a largish capacitor many devices allow GPIO outputs to be configured with an internal pullup that may be up to 50K; an option is to use the pull up to partially charge the capacitor; then switch the output to push-pull (active pull up) mode to finish charging the capacitor. This will reduce the peak current spike loading on the I/O.This won't work well if the sensor draws too much current but you may find your sensors draw very little current until the supply voltage reaches a certain threshold and it starts to turn on.Some devices, like STM32Fxx, have GPIOs with programmable drive strengths to trade off power consumption and speed so another option is to start with the pull-up mode then switch to push pull mode stating with the weakest drive and periodically increasing the drive strength to maximum.Switching off: you could put the GPIO into input mode and let the sensor or a high value bleed resistor discharge the capacitor.Be sure that your sensors can tolerate slow rise and fall rates of the supply voltage - some devices don't like very slow rates and some explicitly specify minimum supply voltage ramp rates.

author: SiliconWizard
author_rank: Super Contributor
timestamp: October 02, 2019, 01:18:01 am
content: I second that, 10µF is very large, and would actually cause issues on some MCUs. I can give you examples. Don't try this on Cypress FX1/FX2 MCUs. Due to the way their GPIOs work, with a read-modify-write structure, if you place too large a capacitance on a given GPIO, and set it to an output, you will never be able to actually set it to High level if the capacitor is fully discharged, and conversely to Low level if it's still charged enough. Not good.

author: Psi
author_rank: Super Contributor
timestamp: October 02, 2019, 10:16:02 am
content: ok, fair enough. 1uF + 100n thenYou definitely want something else there that's bigger than the typical 100nf.

author: forrestc
author_rank: Unknown
timestamp: October 02, 2019, 10:48:24 am
content: Quote from: PCB.Wiz on September 30, 2019, 11:09:57 pmIn low power design, powering parts of circuits with GPIO's are done all the time. When the processor goes to sleep, it also often will want to turn off other devices or portions of the circuit. If what is being turned off is low enough power that the drive capabilities of a GPIO pin on a microcontroller can power it, it's kinda ridiculous to add additional circuitry just so it can be done what some people would say was the 'proper' way. Especially when the external circuitry often looks a lot like the drive circuitry of the GPIO - that is, a FET connecting the switched device to Vcc. Remember, though, that in low power design, you're often shutting off parts of the circuitry with very low power consumption to start with, just to drop the power consumption to the point that you can run for years from a non-rechargeable battery.Because of this, it's not uncommon to find a statement or sample schematic in a datasheet for a sensor to say that this is perfectly acceptable. For example, in the MCP9700 temperature sensor datasheet, one finds this section:QuoteBUT, and this is a big but, as others have said, this requires a fair bit of engineering including understanding the actual behavior of a GPIO pin under load, carefully reading the maximum loading datasheets, including things like maximum current in and out of each pin, blocks of pins, the entire chip, etc. And so on.

author: ColCon
author_rank: Contributor
timestamp: October 02, 2019, 11:23:16 am
content: But if we apply the same rigour to the case where the sensor is simply powered from the same 3v3 line as the mpu, might not additional circuitry be called for there too?

author: Siwastaja
author_rank: Super Contributor
timestamp: October 02, 2019, 01:54:15 pm
content: Quote from: ColCon on October 02, 2019, 11:23:16 amYes, to a lesser degree though.The noise coupling to the IO structure is not well defined. Noise may be low, or may not, and this may depend on the actual IO port and pin - how it happens to route inside the chip.Note that the ripple comes from the internal logic (CPU, memory cells, etc.) switching. This high-current loop is shared by the supply that goes to the IO pin, so:+Vcc -- RL -- IO pin -- RL -- coreis worse than+Vcc -- RL -- IO pin+Vcc -- RL -- core(where RL denotes the R+Lparasitics of the internal wiring.), because the core current pulses affect the IO pin voltage in the first case, but not in the latter. (Assuming there is a good bypass capacitor at the +Vcc pin.)Same happens in a poorly designed PCB, and people still usually get away with these!But if you design the PCB properly, it's like the latter case:+3V3 --- RL --- (bypass cap to GND), MCU Vcc+3V3 --- RL --- (bypass cap to GND), Sensor Vcc(Global bypass cap at +3V3 node.)Yes, if the analog performance is of utmost importance, I wouldn't consider powering the device from the GPIO pin. Not only due to noise, but for precision work, you likely want stable, locally regulated voltage as well, because most precision devices don't have infinitely good Power Supply Rejection Ratio, and precision analog devices tend to be rated for a certain recommended operating voltage.But do note that the typical 100nF capacitorexactlytakes care about this high-freq noise resulting from gates switching. The IO port MOS resistance actually forms an RC filter in this combination.And the typical 100nF cap doesn't cause too much loading for the port during switching. The peak current exceeds the MCU IO port ratings, sure, but it's a very short pulse and total energy dissipated shouldn't be enough to cause localized die heating, or electromigration. I would be far more careful with 10uF, though.

author: Nominal Animal
author_rank: Super Contributor
timestamp: October 03, 2019, 03:12:25 am
content: Wouldn't a P-channel MOSFET (one with low threshold voltage, say VGS(th)≤ 1V), gate controlled via a current-limiting resistor from the GPIO pin, with source to the a filtered or bypassed +Vcc, and drain to the load, work here? Say, something like DMP2305U, in SOT-23-3, so one could even dead-bug it, solder directly to wires without a board?This would also allow one to control the voltage to a sensor that uses +5V but communicates at 3.3V levels for 3.3V MCUs, or provide a voltage reference (selectable between a couple of different ones) to a sensitive sensor.I know that the switching rate may be a problem (it isn't instant on or off, depending on the current-limiting resistor, but I would expect the switch time to be milliseconds or less). Any other caveats a newbie like me should be aware of?

author: Siwastaja
author_rank: Super Contributor
timestamp: October 03, 2019, 06:28:18 am
content: Quote from: Nominal Animal on October 03, 2019, 03:12:25 amOf course, a P-channel MOSFET is a cheap and small solution, so powering via GPIO pin is a kind of micro-optimization for extremely lowest cost or smallest solution (sometimes you won't have room for even the tiniest SMD MOSFET package).Quote from: Nominal Animal on October 03, 2019, 03:12:25 amA simple P-channel MOSFET, though, won't necessarily solve the 5V/3V3 problem (which the OP didn't have (a reminder to avoid confusion)), because the gate voltage is referenced to the source pin, which sits at the sensor's supply voltage. If your sensor is at 5V, and your IO pin sits at 3.3V, you already have -1.7V at the gate in the supposed "off" state. Some microcontrollers have some 5V tolerant pins that supportopen drainmode, then this works by adding a pull-up to 5V. If such a pin is not available, then an extra NFET (and a pull-up resistor) is required. A dedicated power switch IC may be a smaller solution, then.

author: voltsandjolts
author_rank: Unknown
timestamp: October 03, 2019, 06:01:09 pm
content: Quote from: Siwastaja on October 03, 2019, 06:28:18 amThere are numerous dual NFET / PFET options in small packages to help with this, plus one external resistor and your done.

author: MarkR42
author_rank: Regular Contributor
timestamp: October 03, 2019, 06:07:45 pm
content: This thread is quite interesting, I'm a bit of a novice, but I thought I'd throw in one idea which may not have been mentioned yet:* You could use your MCU as a low-side switch, instead of high-sideI'm not sure what advantage that would have, but it means that the pin should never be high, always high-z or low.