Most mysterious resets, glitches and fried boards come down to power. Getting it right is not complicated, but it is easy to overlook. Here's how to choose and connect a power source so your projects run reliably.
Know how much current you need
Add up what everything draws: the board itself, LEDs, sensors, displays and especially motors and servos. Then choose a supply that can provide comfortably more. A small microcontroller board draws tens of milliamps, while a single hobby servo can draw several hundred milliamps when it moves, and much more if it stalls.
Running on batteries? The battery life calculator estimates how long they'll last.
USB power
USB is the easiest source while you're developing. By the USB specifications, a computer's USB 2.0 port is only meant to supply 500 mA, and a USB 3 port 900 mA. Many ports supply more in practice, but you shouldn't rely on it. A good USB phone charger can usually supply 1 to 3 A, which suits most projects.
Regulators: getting the right voltage
Most boards need a steady 5 V or 3.3 V. If your supply is higher, a regulator brings it down.
- Linear regulators are simple and quiet, but they turn the excess voltage into heat. The power wasted is (input voltage − output voltage) × current. Dropping 12 V to 5 V at 500 mA wastes 3.5 W, which needs a heatsink.
- Switching regulators (buck converters) are far more efficient, often 85 to 95%, so they stay cool even when dropping a large voltage. Ready-made modules are cheap and the usual choice for anything above a few hundred milliamps.
Your board's built-in regulator is usually small. Use it for the board and a few sensors, not for motors or long LED strips.
Motors and servos need their own supply
Motors draw big, sudden currents and create electrical noise. Powered from your microcontroller's 5 V pin, they can cause resets or damage the board. Instead:
- Power motors and servos from a separate supply that can handle their current.
- Control them through a transistor or a motor driver board.
- Connect the grounds together. The motor supply's negative must join the microcontroller's GND, so control signals have a common reference.
- Add a flyback diode across DC motors and relays, and a large capacitor (a few hundred µF) across the motor supply to absorb spikes.
Batteries
- AA and AAA alkaline cells are safe and easy. Three in series give about 4.5 V when fresh, dropping as they discharge.
- Lithium-ion and LiPo cells are 3.7 V nominal and 4.2 V when full. They hold a lot of energy and can catch fire if they're punctured, short-circuited, overcharged or over-discharged. Use cells or packs with protection circuits, charge them only with a proper lithium charger, and don't run them down below about 3.0 V per cell.
- 9 V batteries have a small capacity and can only supply low currents, so they suit very low power circuits, not motors or LED strips.
Protect your circuit
- A fuse in the positive supply line limits the damage if something shorts. Choose one rated a little above your normal maximum current.
- Reverse polarity protection: a diode in series, or a MOSFET circuit, stops damage if the battery goes in backwards.
- Check before you connect. Use a multimeter to confirm the supply voltage and polarity, and check for shorts between power and ground with continuity mode.
Going deeper
How hot will a linear regulator get?
A linear regulator wastes (Vin − Vout) × I as heat. Its temperature rise is that power multiplied by the thermal resistance from chip to air, θJA, given in °C/W in the datasheet. For example, with a θJA of 50 °C/W:
def regulator_temp(vin, vout, amps, theta_ja, ambient=25):
watts = (vin - vout) * amps
return watts, ambient + watts * theta_ja
for amps in (0.1, 0.3, 0.5):
w, t = regulator_temp(12, 5, amps, theta_ja=50)
print(f"{amps*1000:.0f} mA: {w:.1f} W, about {t:.0f} °C")
100 mA: 0.7 W, about 60 °C
300 mA: 2.1 W, about 130 °C
500 mA: 3.5 W, about 200 °C
Most chips should stay below a junction temperature of around 125 to 150 °C, and most regulators shut down to protect themselves before then. So this example only works up to roughly 200 mA without a heatsink. Thermal resistance depends heavily on the package and the copper around it, so use your part's datasheet figure.
Dropout voltage
A linear regulator needs its input to stay a little above its output. This "dropout" is around 2 V for older types like the 7805, and as low as a few hundred millivolts for low-dropout (LDO) regulators. Running a 3.3 V board from a single lithium cell (3.0 to 4.2 V) needs an LDO with very low dropout, or a buck-boost converter.
Switching regulator efficiency
A buck converter's input current is roughly (Vout × Iout) / (Vin × efficiency). At 90% efficiency, 5 V at 2 A from 12 V draws about 0.93 A from the supply, instead of the full 2 A a linear regulator would draw. Switching regulators do create electrical noise, so keep their wiring short, follow the datasheet layout, and add filtering for sensitive analogue circuits.
Wires, connectors and fuses
- Thin wires and cheap connectors have resistance, so they drop voltage and heat up at high current. Check the current rating of the wire and connectors you use.
- Resettable fuses (polyfuses) trip when overloaded and recover once cool, which suits small projects. Standard fuses act more predictably for higher currents.
- Capacitors charging at switch-on cause an inrush current spike, which can trip fuses or damage switches in large systems.
Battery ratings
Lithium cells are rated for a maximum discharge current, often given as a C-rate: 1C for a 2,000 mAh cell is 2 A, and 2C is 4 A. Exceeding the rating causes heating and voltage sag. Protection circuits cut off the cell on over-discharge, over-charge and short circuit. Treat any puffy, damaged or hot cell as unsafe.
Sources
The main references behind this guide. Numbers we worked out ourselves, such as the code outputs, come from running the code.