Friday, 15 June 2012

Raspberry Pi Power Supply and Powered USB Hub

When I first powered up my Raspberry Pi, I was using a 1A charger from an HTC Desire phone. This ought to provide enough current to power a USB mouse and keyboard, plugged into each of the Pi's USB ports. However, I was suffering from random key repeats and missed keypresses. Besides, I wanted to connect a USB wifi adaptor too, and there was no way this was going to work with an unpowered USB hub: the Pi can only supply a total of 140mA.

Being a cheapskate, I baulked at the idea of spending almost as much again as I'd paid for the Pi itself to buy a powered USB hub; besides, I already had a meaty 4A mains adaptor from an old label printer. What's more, a powered USB hub would require a separate mains adaptor, and it would be much neater to power everything, Pi and peripherals, from one plug.

The solution: buy a cheap unpowered USB hub from a pound shop and butcher it about a bit with a soldering iron and a craft knife. Here is what I came up with (click to make bigger):

As you can see from the picture, there are two protruding leads: the silvery one is the original lead from the hub to the computer's USB port; the black one, terminated in a micro USB plug, is the one I have added which goes to the Pi's power socket. You can also make out the power socket I have added for the input power from the PSU I'm going to be using. It's clearer in this picture:

If you want to make one of these yourself, it's pretty simple. You will need an existing power supply that can deliver 5V and enough current to power your Pi plus whatever else you want to plug into the hub. I would recommend at least 1.5A. (Current is given in amps (A) or milliamps (mA) on your charger or power adaptor, 1.5A being the same as 1500mA.) Bear in mind that some power supplies will drop the voltage as they deliver a higher current, so a supply that looks on paper as if it ought to be borderline good enough might still not work. Some people have reported getting Pis to work with supplies rated at 6V, but I'm guessing the voltage maybe fell when loaded. The label printer PSU which I am now using delivers 5.15V unloaded. If you stay below 5.25V, you should be OK. Your mileage may vary, as they say.

You will also need a socket that pairs with the existing plug on the power supply you're going to use. (That's the part sticking through the clumsy cut-out in the plastic casing in the photo above.) If you don't have one, you could simply chop off the plug and wire the power supply directly into the hub, so it has three leads permanently going into it. This strikes me as getting a little ungainly; besides, it means you won't be able to use the power supply for anything else in the future. Up to you. If you do do this, obviously you need to adapt the instructions below where it talks about fitting the power supply socket: just solder your two wires from the PSU direct to the board instead.

Finally, you need to live near one of those ubiquitous and wonderful pound shops that sell HDMI leads, toothpaste and 72-packs of no-name batteries for a pound a punt. Most of them have a small computing/AV/electrical section and if you're lucky it will supply you not only with a nice little unpowered USB hub like the one pictured, but also an encapsulated micro USB plug (the black lead in the photo). Mine was sold as a USB Blackberry charger. Chop off the full-size USB plug from the one end and you're left with a Raspberry Pi power lead. Total cost to you: two quid.

If you prise open the two halves of the clamshell that comprise the hub (which it is mercifully possible to do without breaking it permanently) you'll find the silvery USB lead (yours may not be silvery; the hub itself may be blue or pink; use a bit of imagination here) has four inner wires soldered on to the board. The red and black ones are the positive and negative power supply. The green and white ones are the data lines. (At least, this was the colour-coding for the hub I bought.) The essence of what we're going to do is divert the power wires to our replacement external supply, but leave the data ones where they are. This means your USB peripherals will draw their power directly from the PSU, not from the Pi; while the data lines will still go to the Pi's USB socket. Here is a photo of the underside of the board:

As you can see, I've simply snipped the red and black wires from the USB lead, leaving only the green and white data lines connected. Next, I found a convenient place on the edge of the board to mount the input jack for my PSU plug. (It's not really mounted; it kind of hangs over the edge and is held in place by the fat wires and goodwill.) I chose that specific corner for it because, for some reason, there was already a round hole cut out of that corner in the moulded plastic, previously unused, and easily big enough to poke a cable through. I felt it was meant to be when I first noticed this.

You need to connect the input jack to the positive and negative power tracks on the PCB from where you've just snipped off the existing power supply from the USB lead. My replacement PSU has a coaxial plug which supplies negative to the centre pin and positive to the barrel of the plug, so I have connected a red wire to the foremost tag and a black wire to the rear tag on the power socket. Then I soldered the other end of the red wire to the PCB where the red wire from the USB lead was previously connected, and the black wire to the PCB where the black wire from the USB lead was previously connected. In fact you'll notice I haven't done exactly that: I've traced the tracks on the PCB to find a more convenient place to solder them on; this is fine and makes more room in the rather cramped plastic housing. The two ends of the four terminals where each USB socket is mounted are always the power supply (and the two middle ones always data). Just make sure you get them the right way round.

So now we have a powered USB hub, but no power yet for the Pi itself. To remedy this, solder the lead from your chopped-off pound-shop encapsulated micro USB lead on to the board as I've done. Again, you can use any convenient terminals which you identify as being electrically connected to the positive and negative supply. In this case you can see I've actually soldered them to the exact spot (but on the other side of the board) where I snipped off the original power leads. Slightly confusingly, this time I found the power lines were colour-coded red and white (not black). I'm afraid you'll have to work with what you find.

Finally, I blobbed some glue liberally on top of where I'd soldered, to mitigate the effects of jiggling the wires round (since there's no room for proper strain relief) and snapping them off over time. That would be it, except that with the addition of the coaxial socket for the power input, the case now won't snap back together. This is where the craft knife comes in. My best effort looked like this:

You can probably do better.

Before you carefully feed the wires through the holes and snap the whole lot back together, I would strongly advise you to get your multimeter or continuity tester and check you have not inadvertently made a short circuit between the positive and negative supply terminals. In fact there should be no continuity between any of the four USB terminals. A minor precaution against frying your Pi and blowing up your PSU when you plug it in. (Though I emphasize that your carrying out any of the above, and the consequences you may realize by doing so, are entirely your own responsibility, not mine. I might have made a mistake in my instructions. You might make a mistake carrying them out. Weigh up the risk yourself and be judge of your own competence. If it doesn't work, or causes actual damage, it's your responsibility!)

If you've gone ahead, done all that and shouldered the responsibility as requested, you're ready to go. Insert the USB plug (on the silvery lead in my case) into one of the Pi's USB sockets, and insert the micro USB plug (black in my case) into the Pi's micro USB power socket. Finally, insert the plug from your power supply into the socket you've added to the hub. Everything should spring into life (assuming you remembered to connect the display and insert the SD card). You now have five USB ports to play with: one left on the Pi itself (which should supply 140mA; call it 100mA for safety) and four on your new powered hub, which should be good for however high the rating on your PSU is (minus whatever the Pi itself is drawing). And the whole lot runs off a single mains plug.