Powering from the Raspberry Pi
A common question is “Will the Raspberry Pi power this?,” to which the answerer is always “What do you mean by power?” Basically, there are three ways a Pi can power something and each has a separate answer.
Sourcing the power from the GPIO pin
The GPIO pins are quite fragile on the Raspberry Pi, compared with other boards like the Arduino. Each GPIO pin can source (provide the current) or sink (suck the current into the pin to switch something to ground) about 16mA. The total amount of current sourced or sunk from all pins added together should be restricted to about 50mA, which spreads out to about 3mA each for the non-plus models.
The only thing you should be directly powering from a GPIO pin is an LED, and only with a low current at that. Everything else needs to go through some sort of driver — normally a transistor or FET. Also, the output voltage is only 3V3, so many devices need more voltage as well as more current.
Sourcing the power from the Pi
This is where the signal to control something is derived from the GPIO pins, but the actual power to drive it is from the 3V3 or 5V internal power lines of the Raspberry Pi.
Using the 3V3 supply is tricky because that line is going through the onboard voltage regulators and there is a limited amount of spare current available before you get overheating of the regulators. Don’t take any more than 50mA from this power rail. If you want more, consider using a voltage regulator on the 5V line.
Much more promising is the 5V line because it’s derived from the same power source that is powering the whole Pi. Because the Pi takes up to 800mA at peak, if you power the Pi with a 2A supply, you could take about 1.2A from the 5V lines. Parallel up the two 5V pins on the GPIO connector and at least two grounds to reduce the resistance introduced by the connector. But at the upper end of this current draw, expect a bit of voltage droop — you might get a volt or so lost and end up with only about 4V.
Sourcing the power from an external supply
There are times when you need a voltage higher than 3V3 or 5V to control a device. If this is the case, you need a separate power supply. This means that the ground or –ve terminal of the external power supply must be connected to the ground of the Raspberry Pi. A common fear here is that this will in some way damage the Pi, but as long as it’s only the ground that’s connected, there is nothing to worry about.
For example, a typical application might be to drive a 12V relay.
The way a transistor works ensures that the 12V does not leak back into the Raspberry Pi.
The relay contacts are completely separate from the Pi; this is said to be isolated. Any sort of coil or inductor must have a diode to protect against back EMF; this is a large reverse voltage spike that occurs when the relay coil’s current is removed and the magnetic field collapses. The diode shorts this out and prevents it from causing any damage.
The 1K resistor in the base of the transistor ensures that you don’t draw too much current from the GPIO pin. This circuit is applicable to other things as well — for example, instead of the relay coil, you can have a motor.