The Raspberry Pi GPIO pins work with 3.3V logic levels and are not 5V tolerant. If you apply 5V to a GPIO pin you risk permanently damaging it. However, you can easily use 5V sensors or modules if you convert their 5V outputs to 3.3V using a level shifter. This ensures the GPIO pins on the Pi only see a maximum of 3.3V. The shifters are usually bi-directional so they can also be used to allow the Pi to trigger a 5V input with it’s 3.3V outputs.
Typical Level Shifter Module
A level shifter (also known as a logic level converter) will take 5V signals and convert them to 3.3V. This allows the Pi to read outputs from sensors that are only happy being powered from 5V. The modules are cheap and often provide 4 or 8 channels.
The channels consist of a pair of pins labelled HV1/LV1, HV2/LV2 etc. The “HV” stands for “high value” and “LV” stands for “low value”. Channel 1 consists of LV1 and HV1, Channel 2 is LV2 and HV2 etc. There are also two pins which must be provided with reference voltages so that the module understands what our high and low values are.
Example Level Shifter Circuit
In the example below a 5V sensor is connected to one channel on a 4-channel level shifter. The other 3 channels could be used for other sensor inputs.
The level shifter is connected to both the Pi’s GPIO and the sensor output :
Level Shifter | Pi GPIO | Sensor |
---|---|---|
HV | Pin 2 – 5V | |
LV | Pin 1 – 3.3V | |
GND | Pin 30 – GND | |
HV1 | OUT | |
LV1 | Pin 32 (GPIO 12) |
The sensor is powered from the Pi’s 5V supply with the output going to the first HV pin on the level shifter.
Sensor | Level Shifter | Pi GPIO |
---|---|---|
GND | Pin 34 – GND | |
OUT | HV1 | |
5V | Pin 4 – 5V |
Although I used GPIO12 you could use any of the Pi’s GPIO inputs. Similarly you can use alternative 3.3V and ground connections. Take a look at the Pi GPIO reference diagram to check what is available :
Multiple Channels
The level shifters provide multiple channels as they are often sold to be used with I2C or SPI devices where you would need to convert multiple signals per device.
Tips
When to use a level shifter :
- You want the Pi to read logic levels that are above 3.3V
- You are using a 5V module that doesn’t work properly using 3.3V and you have no choice but to use 5V
When you don’t need to use a level shifter :
- The device is powered from 3.3V as its logic outputs will be 3.3V
- The module is 5V but only takes inputs. The Pi’s 3.3V pins are usually fine for triggering devices that use 5V logic as they usually trigger with anything above 3V
Troubleshooting
In order for the circuit above to function correctly you need to make sure that :
- the high-level (“HV”) side of the module is only ever connected to the 5V power or signal pins
- the low-level (“LV”) side of the module is only ever connected to the 3.3V power or signal pins
- the HV, LV and GND pins are connected to the 5V, 3.3V and Ground pins on the Pi’s GPIO
Buy a Level Shifter
You can buy level shifter modules from The PiHut, eBay or Amazon for only a few £/$.
Look out for multiple packs. Given their low price you might as well buy 3-5 and keep some spares in your electronics box.
2 Comments
don’t we need to connect the HV side GND of the level shifter to the raspberry pi GND pin?
I think the two Gnd pins are the same so you only have to connect one of them.