Friday, 16 January 2015

Raspberry Pi GPIO: mixing voltage levels

"Don't put metal objects into your microwave" and "Don't connect 5 Volts to your Raspberry Pi GPIO"

I consider these to be two pieces of well meaning advice for non-technical people. The idea is to help avoid mis-haps by offering generalisations, without going into long explanations.

But we are not non-technical people, are we?

The background

The Raspberry Pi GPIO (general purpose inputs/outputs) operate at 3.3 Volt, which can be a bit awkward. I say awkward because many devices (such as sensors, logic and functional chips) run on 5 Volts.

The popular advice, as mentioned above, attempts to be black-and-white, with lots of Raspberry Pi forum buffs passing on the same message, and recommending the use of potential dividers and level shifters when interfacing 5 Volt devices with the Raspberry Pi

I started thinking about this issue once again as a result of re-working the controller circuit from last years birdbox.

My take on this

The RaspberryPi was developed primarily for school age children to handle, connect and program. The board is clearly not overly susceptible to electro-static damage. If it were, the advice on the packaging would be for users to wear anti-static wrist straps to avoid damaging the circuitry.

The need for personal anti-static measures for certain equipment is due to the fact that we generate electrical charge as we move around. If I were to slide off this sofa and shuffle across our polypropylene carpet in my tartan slippers, by the time I reach the Blu-Ray player, the potential difference between the tip of my index finger and the eject button is likely to be hundreds, possibly thousands of volts. Admittedly this would be a low energy charge, and if I completed the circuit, it would be via a high resistance.

So, as the Raspberry Pi GPIO can be safely handled, it is reasonable to suggest that it can therefore be safely connected to a voltage greater than 3.3 Volts, just as long as it is connected via a suitably high resistance to limit input current.
I've seen diagrams on the web which allegedly represent the Raspberry Pi input protection circuitry, where input diodes conduct current into the supply rails if the input voltage exceeds recommended values. (I say "allegedly" because the design details are not in the public domain).

This is an old but effective way to protect sensitive input circuitry. When the input voltage exceeds the supply voltage + diode forward voltage, the diodes conduct current into the supply rails, the voltage across the input resistor rises, and the sensitive input circuit is protected from this higher input voltage.

This is a great illustration, but is it accurate?

If the Raspberry Pi includes this simple diode clamp protection, it should be possible to determine this by configuring a pin as an input, and measuring the input current as we increase the input voltage.

So I did a series of tests with different input resistors connected to 5 Volts. At each stage I calculated input voltage and current by measuring the voltage drop across the input resistor.

So, here are my conclusions from this first set of measurements:-
  • With an input voltage near to the ideal 3.3V, the input current is approx 61uA. Increasing the input voltage to 4.7V only increases the input current by approximately 3uA. Therefore I conclude that the circuit does not include a diode clamp operating in the range 3.3 to 4.7V.
  • Also, the small change in input current indicates that nothing bad is happening (i.e. nothing is conducting much more at 4.7V than it was at 3.3V, and the chip is not going to heat-up due to the extra 3uA).

If the input circuit does have input protection, it must be operating at a higher level (certainly above 4.7V) so maybe the input has been designed to be "5V safe".

Let's push it a bit further!

I decided to extend these tests by replacing the 5Volt source with 12Volts. I started testing with 12V connected via 100k to GPIO physical pin 12, while running a simple test program to ensure that this input was still functioning.

Initially I just kept swapping resistors and recording results. As the 12V supply was not especially stable, I measured both the voltage across the input resistor, and the voltage at the GPIO input pin.

Once I'd got a feel for how the tests were going, I repeated the tests using a resistor chain. This gave me better control, and enabled me to target input voltages in sensible 0.1V steps.

So, plotting the input current (i.e. calculated from Vr/r) against input voltage, I get this:-

The input current appears to rise steadily as the input voltage rises, up to around 5.8Volts. Above 5.8V the input appears to climb more quickly, so clearly something is happening.

The only conclusion I can come up with is that the Raspberry Pi GPIO inputs are 5 Volt safe, and that there is some kind of protection which kicks in around 5.8 Volts.

...but I'd be very interested to hear your interpretation!

The case for sticking to the guidelines

It is probably a safe bet that the majority of Raspberry Pi users, and also a very large majority of Raspberry Pi forum members, do not have a good working knowledge of electronics.

And you may be expecting that GPIO pin to operate as an input, but find that the impressive code example that you downloaded, periodically switches it to an output!

So the simple advice: "do not connect a voltage greater than 3.3Volts to GPIO pins" may be sensible. Otherwise the manufacturers could be inundated with returns, where users have connected GPIO pins to a high voltage, low impedance source and damaged their precious board!

But it follows that those Raspberry Pi users with a good working knowledge of electronics, just don't need that advice. They just need to approach the issue in the same way they would for any other electronics project, relying on their own ability as designers.

My design guide

While it is true that the GPIO runs from a 3.3 Volt supply, this does not mean that it is not tolerant of voltages greater than 3.3 Volts. Even Liz Upton agrees that voltages up to 4.3V are not a problem. But I'd need to see the data or hear the science behind the assertion that "5V won’t fry the chip, but it will decrease its lifetime noticeably".

Once again, the problem is that we don't have access to good quality, accurate design details and data.

For GPIO configured as inputs (which is probably the most interesting case) I certainly don't have a problem with connecting "open-collector" devices using a 5Volt supply, and a suitable value pull-up resistor. In fact I've been doing this for the last 18 months.

After conducting these tests, I'd be happy to use devices with open-collector outputs operating from supplies greater than 5 Volts. Its just a case of limiting the gpio input current by suitable selection of load resistor.

But probably of more interest is that I'm now happy to interface 5 Volt devices (e.g. Picaxe) directly to Raspberry Pi gpio pins. When a gpio pin is set as an input, I know the 5V output from a Picaxe won't damage it. When a GPIO pin is set as an output, the 3.3V level will still be enough to be recognised by the Picaxe as a high input.

However, for gpio outputs, it is almost always advisable to include a driver when connecting to any device requiring more than just a few mA. You need to look closely at both the rated output current for a GPIO pin, and the total GPIO current.

I doubt that you will try to connect a GPIO output to a load connected to (say) a 12V supply. But if the load resistance is high enough to limit the output current to a safe value, then the circuit will be safe. However it will only change the voltage across the load by 3.3V when the output switches!

If you don't understand what I'm talking about, then you should stick to the mantra in the first line of this post.
And here is another (more appropriate) chant you might like to use: "Current Kills, Current Kills!"

Further reading: Dr. Arno Wagne


  1. OK, this is a good analysis of what is probably going on behind the Pi's GPIO pins. I've been interfacing microcontrollers to Pi's since the Pi has been on the market, and I've been using resistor voltage dividers to get 5V talking to 3.3V. I just believed the mantra like everyone else!
    Things may change in my designs now that I've read your analysis. Thanks.

  2. The sudden rise in current at about 5.8V suggests that there is an overvoltage protection diode, but that it is tied to 5V instead of 3.3V.

    1. I agree. A silicon diode with a low current flowing through it often shows a volt drop of 800-900mV. So that fits with diode protection between gpio inputs and the 5V rail.
      It is now over 3 years since I started "abusing" the Pi gpio, and I have not damaged any of my boards (I currently have 13).

      BTW I like your article on rebuilding a valve (vacuum tube) amp:

      I need to push the rebuild of my 1950's Bush radio further up my job list!