I was wondering since a micro controller can sink 20 mA on each pin, if I can make 4 pin ports low by software, and just connect the positive side of the load to Vcc, is this a reliable methodology for driving larger loads? Would the only thing to worry and pray about is that the machine cycle is fast enough to handle the fluctuations of setting all the pins at the same time? Am I on the right track?
If not buying a "module" is your issue, consider spending 6 cents (or less) on a transistor that will happily sink 200 or 500 mA and only occupy one pin for its base control input. I think 6 cents is what I paid for the last batch of 2N3904/6 and 2N4401/2 in lots of 100. Not much point in buying less than that, but if you like paying 4X as much for one at a time, be my guest.
These are very far from cutting edge parts, but they do a particular sort of job well and they are pretty low cost.
It should work just fine, but You need to keep in mind a few things.
- Max VCC or Max GND current. There is a maximum current that the microcontroller can support through its VCC or GND pin.
- Max Port current. An individual port has a maximum current as well.
- Pin ESR. A GPIO Output will have a voltage drop (or rise) based on how much current is going through it. Typically, the recommended max current through the pin is for a given output voltage compared to VCC or GND.
If your load is within these specs, which your 80mA load example should be for ATMega micro controllers, then the other concern is preventing shorts.
You need to ensure that you change the entire Port at once, instead of one pin at a time. If you change only one pin at a time, from Low to High or High to Low, then you can create a dead short between the pins, blowing one or more of them. Depending on the default state of the pins when starting up, before your code can set them, this may not be avoidable without extra parts. Also try to keep it isolated to a single port, as it will be difficult to change multiple ports at the same time, and voltage differences between ports may hurt.
Typically, having series resistors on the pins will help. These resistors will end up in parallel with each pin as well, which helps regulate the current between them.
Assuming you want the 80 mA load equally on 4 pins, then that's 4 extra parts, as protection. At that point, you may as well just go with a single transistor + resistor pair.
With careful coding, you should not need these, and parallel gpio will work fine as current sinks.
Open Collector outputs, which can only pull low, do not have issues with shorts, but should still be changed at the same time. Not changing them at the same time can lead to the current though just a single output, which will cause issues if there is no current limiter. A typical microcontroller GPIO is not Open Collector, but you can mimic it by only switching between Output Low and High-Impedance Input mode.
Don't ever connect a digital port to ground if its push pull or Vcc or ground if its open drain. Check the datasheet, I think the rasp pi doesn't have a datasheet but the info is out there. Sinking is different than sourcing. If you have an open drain output, you can sometimes sink more current than a push pull. You might be able to get away with a little more with a paralleled open drain configuration, because you aren't dissipating a lot of power in the chip but you still are limited by the wire width, if you go beyond this it will heat up and burn away.
With push pull this is generally a bad idea, you can parallel pins on your processor to give you more current, but you don't get more current through the Vcc Io or the I/O voltage supply pin. In some cases digital devices will give you a current rating on the Vccio and the pin itself. Don't exceed these ratings.
Don't guess, don't pray, don't test, just follow the datasheet, and treat it as your God.
Whilst I agree it can be a bit esoteric, each pin or category of pins will have recommended maximum currents (either sourcing &/or sinking). There's also likely to be maximums for the whole port of 8/16/32 pins, and/or a maximum source/sink for the entire chip's i/o pins. Read the datasheet, do the math, get the answer. Just to complicate matters even further, some MCUs allow you to program how much maximum-sink/source-current they'll operate at (e.g. the i/o on a BeagleBone's TI MCU). All the platforms/MCUs you mention are different.
There is danger in simply connecting several i/o pins together and using their combined current sourcing or sinking capability, and it's not the per-port or per-chip maximums I mention above; MCUs aren't meant to be power shunt devices, that's why they're called microcontrollers. It's:
(a) what happens to those i/o's when the chip's in RESET? Most will go high-impedance input, which is probably OK, but sometimes a pin will have some special purpose that may come into play during or immediately after a reset, in which case, what's your circuit going to do in that time, & how much current through that pin?
(b) the technique by which you set/clear those i/o pins: did you use only the pins from 1 port, in which case you can write a byte/word into the i/o register and set/clear them all at once, so they'll all start conducting all at once - great. But if you spread the load across pins from multiple i/o ports (i.e. Port A & Port B), then that requires multiple register writes, which happens sequentially, which means the first pins/port that is written conducts first and carries the total current, then it won't be until the next/last port is written that the current is shared roughly equally across all the pins you've connected to. If you're doing this i/o writing a single pin at a time (e.g. Arduino's "didtalWrite(Pin,State)", then that first pin will conduct the FULL current until the 2nd & subsequent pins are written. This is all BAD and likely to kill the MCU, if not immediately, then well within its expected life-time.
The solution is simple. Get a NPN transistor or N-ch MOSFET of suitable spec, connect its base/gate to the MCU's i/o pin (and calculate an appropriate series resistor between the two), connect its Collector/Drain to whatever it is you want to switch on/off, and connect its Emitter/Source to Ground. 1 i/o pin, 1 transistor, good for anything from tens of mA & beyond. There'll be other questions on SE on how to do this in detail.
Several concerns spring to mind.
- What if you make a mistake in the code and drive one pin high while another is low? You now have a power to ground short through a pair of IO pins which is not good.
- Are there any restrictions on total maximum current for the IO pins. If so are you within those limits
- How well balanced are the drive transistors. I.E. How close to equally will they share current initially.
- What is the temperature coefficient of the output transistors. As imbalanced current causes unequal heating of the output transistors will that make the current balance better or worse.
Concern 1 is mostly a case of how much condidence you have in your own code. Concern 2 is something datasheets can answer but I don't think i've ever seen a datasheet that answers concerns 3 and 4.
As such I would not recommend this configuration. Use an external transistor to switch the load.