## Best way to shift 5V down to 3.3V for GPIO pin

henryhunt
Posts: 17
Joined: Sat Aug 27, 2016 11:31 am

### Best way to shift 5V down to 3.3V for GPIO pin

I can see this question has been asked multiple times around the internet (and here), however I have come across so many methods, solutions and component values, and I am not advanced enough with electronics to decide for my specific situation.

I have a sunshine duration sensor that simply outputs a 5V signal when it is sunny and a 0V signal when it is shady. I would like to read the value of this signal on a GPIO pin, once a second, to build up a tally of how long it has been sunny for. However, the GPIO pins are obviously 3.3V inputs only.

What would be the best way to get this 5V output signal down to a 3.3V signal that the GPIO pin can accept? I've read about voltage dividers, voltage regulators, level shifters, simple circuits and breakout boards of more complex components, but am not really sure what the best method or device/component would be to accomplish this.

bitbank
Posts: 252
Joined: Sat Nov 07, 2015 8:01 am
Location: Sarasota, Florida
Contact: Website

### Re: Best way to shift 5V down to 3.3V for GPIO pin

Your case is the simplest for adapting the signal. You're reading a signal that's higher voltage than your GPIO can handle. Simply use a voltage divider comprised of 2 resistors. One with twice the resistance of the other. You'll form a voltage divider which will output 2/3 of the voltage (3.3v on the point between the two resistors).

Choose R2 to be twice as large as R1 (e.g. 10K + 20K ohms).
The fastest code is none at all

scotty101
Posts: 3778
Joined: Fri Jun 08, 2012 6:03 pm

### Re: Best way to shift 5V down to 3.3V for GPIO pin

Best is very subjective and depends massively on what you are connecting.

You can rule out a voltage regulator - These are used for stepping down voltage for power supplies rather than signal voltages.

A Voltage divider is probably the cheapest way but you need to be confident that your input voltage won't rise to be too large since this could result in more than 3.3v being applied to the GPIO pins. A zener diode can help here.

A logic level shifter is a good way to do it as you will never get more than 3.3v (or whatever output voltage you want) from these devices. You can use a transistor in the same way but the "pre-packaged" modules or chips tend to have multiple inputs/outputs.

If you are confident that your input voltage will never be more than 5V, use a voltage divider.
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

PhatFil
Posts: 1478
Joined: Thu Apr 13, 2017 3:55 pm
Location: Oxford UK

### Re: Best way to shift 5V down to 3.3V for GPIO pin

if you have 1 or 2 simple on/off inputs to detect then basic vltage dividing circuits can be quick and easy. however when dealing with anything using a complex coms protocol 2 way coms and more than 1 or 2 inputs imho the time saved by simply employing a logic level shifter board makes them very useful bits of kit to have abuot. having a number of 2x 4x and 8x level shifter boards with the components kit makes a lot of sense.. I buy far east bundles of 10-20 pcs reducing the cost to pennies a board, not had a dud/deadboard yet!

Depending on your project this may or may not be an issue but diy voltage dividers on vero board or strip board tend to come in a bit bigger sized than the smd compnent mounted logic level shifter boards.

henryhunt
Posts: 17
Joined: Sat Aug 27, 2016 11:31 am

### Re: Best way to shift 5V down to 3.3V for GPIO pin

scotty101 - It seems like a voltage divider would be best here. However, I never understand what is meant when people talk about being sure voltages will not be higher than they expect. Surely if a device says it will output 5V, it will just do that? Why would it be any higher? Or is that what you mean, that I need to make sure that it is 5V that the sensor says it will output and not something else that I have mistakenly read?

FTrevorGowen
Forum Moderator
Posts: 5284
Joined: Mon Mar 04, 2013 6:12 pm
Location: Bristol, U.K.
Contact: Website

### Re: Best way to shift 5V down to 3.3V for GPIO pin

henryhunt wrote:
Tue Apr 10, 2018 11:57 am
scotty101 - It seems like a voltage divider would be best here. However, I never understand what is meant when people talk about being sure voltages will not be higher than they expect. Surely if a device says it will output 5V, it will just do that? Why would it be any higher? Or is that what you mean, that I need to make sure that it is 5V that the sensor says it will output and not something else that I have mistakenly read?
(With apologies to @scotty101 for "jumping in") It's not really that the sensor may output >5V, if it's powered from "5V", but if its supply is different/separate to the Pi's 5V (which should be close enough to 5V to be "O.K.") and is >5V then there is a (small) risk of >5V "spikes". Some sensors may work with higher than 5V supplies and already use some form of level-shifter/translator to provide a "5V" level, depending on what that is a simple divided may not produce the expected result. TBH, it's probably more likely to be a "lower" value than a "higher" one but the only way to be sure is to check it first. (Which, as a by-product, means you will have checked your "bread-board" wiring too - always "a good thing" to do. )
Trev.
Still running Raspbian Jessie or Stretch on some older Pi's (an A, B1, B2, B+, P2B, 3xP0, P0W, 2xP3A+, P3B+, P3B, B+, A+ and a B2) but Buster on the P4B's. See: https://www.cpmspectrepi.uk/raspberry_pi/raspiidx.htm

Return to “Interfacing (DSI, CSI, I2C, etc.)”