Using a Logic-level Shifter with Raspberry Pi

When and why to use a logic-level shifter?

Written By: Cherie Tan

Dash icon
Difficulty
Easy
Steps icon
Steps
11
Many sensors provide a voltage level which informs the Raspberry Pi or a microcontroller such as the Arduino about their change of state. Now recall that the Raspberry Pi's GPIO pins are designed for use with 3.3V inputs and outputs. Some sensors operate on 5V, so feeding its output directly to the Raspberry Pi's GPIO pins runs the risk of damaging either the sensor or the Raspberry Pi. Imagine not knowing about this and wiring it up erroneously, you'd hope to see spectacular blow-ups and magic smoke! However, we usually see nothing, no fun at all - it just eventually stops working. Which can be frustrating when it comes to troubleshooting. Other times though, you need a voltage adjustment in both directions so that devices can communicate in both directions across the same wire. 

In this guide, you'll learn about logic-level shifters, specifically the bi-directional logic level shifter.

Complete this guide to set up a Raspberry Pi with a Little Bird Uno R3, so they can communicate bi-directionally via the I2C protocol!

Step 1 Overview

Many sensors provide a voltage level which informs the Raspberry Pi or a microcontroller such as the Arduino about their change of state. Now recall that the Raspberry Pi's GPIO pins are designed for use with 3.3V inputs and outputs.

Some sensors operate on 5V, so feeding its output directly to the Raspberry Pi's GPIO pins runs the risk of damaging either the sensor or the Raspberry Pi. Imagine not knowing about this and wiring it up erroneously, you'd hope to see spectacular blow-ups and magic smoke! However, we usually see nothing, no fun at all - it just eventually stops working. Which can be frustrating when it comes to troubleshooting. 
On the other hand, sometimes you want to increase the 3.3V output from the Raspberry Pi so that it can drive another circuit that requires 5V input.

One example of this can be seen in the use of the TrinityPixel LED Strips with Raspberry Pi, where a 74AHCT125 logic level shifter was used. That said, if you're only powering a few TrinityPixels like in the case of building a mini Nanoleaf replica, you could do without a level-shifter or external power supply, and power them directly from the Raspberry Pi's 5V pin.
Other times though, you need a voltage adjustment in both directions so that devices can communicate in both directions across the same wire. For example, say we wanted to connect a Raspberry Pi to a Little Bird Uno R3. To do that, you'll need a bi-directional logic-level shifter.  In this guide, we'll show you how to do just that by using the I2C communication protocol
A logic level is a specific voltage used to determine whether a signal is HIGH or LOW.  In the past, 5V used to be the standard logic level for most systems, recognised as a typical HIGH signal. These days, devices are getting smaller and more power-efficient, so many work on 3.3V to reduce power consumption or wastage. 

Step 2 Meet the I2C Bi-directional logic-level shifter

Take a look and you should see a total of twelve pins on the bi-directional logic-level shifter. On the first row, you'd see the pins: HV1, HV2, HV, GND, HV3 and HV4. On the second row, you'd see the pins: LV1, LV2, LV, GND, LV3, and LV4.

What do all these mean? Here's a hint: This logic-level shifter is a 4-way level conversion module which has the capability of converting 4 pins on the high side to 4 pins on the low side, with two inputs and two outputs provided for each side. 
The board needs to be powered from two voltage sources, a high voltage and a low voltage, that your system is using. In this case, the high voltage source would be 5V and the low voltage source would be 3.3V. 
So, The high voltage source is to be connected to the 'HV' pin, while the low voltage source connected to 'LV', and ground from the system to 'GND' pins.
Now click on the second then third image. Here, we've drawn imaginary lines to separate the module into columns. Each column represents a channel, so there are four separate channels here and the number at the end designates the channel of the pin. So for example, LV1 and HV1 are on channel 1. For example, say we wanted to shift the logic level of the signal sent in LV1, up to a higher voltage, this would be shifted up and sent out to HV1.

If we wanted the signal sent in HV2 to be shifted down, this would be sent out of LV2. You may use as many channels as required, although it is not required to use every single one. 
Note: These logic level shifters will work on any pin that requires sending and receiving of digital data. 

Step 3 Connect bi-directional level-shifter to breadboard

Insert the bi-directional level-shifter into the breadboard as shown. Here, we have inserted them so that the first row of pins are connected from F7 to F12. The second row of pins should be connected to D7 to D12.

Step 4 Connect 3.3V of Raspberry Pi to LV pin

Insert a F-M jumper wire from 3.3V on the Raspberry Pi 4 to LV on the bi-directional logic-level shifter.

Step 5 Connect 5V of Little Bird Uno R3 to HV pin

Next, insert a F-M jumper wire from 5V on the Little Bird Uno R3 to HV on the bi-directional logic-level shifter.

Step 6 Connect GND to GND

Then, connect a F-M jumper wire from GND on the Little Bird Uno R3, to GND (row 1).
Next, connect another F-M jumper wire from GND on the Raspberry Pi 4 to GND (row 2)

Step 7 Connect GPIO2 (SDA) to LV1

Now connect a F-M jumper wire from GPIO2 (SDA) to LV1.

Step 8 Connect GPIO3 (SCL) to LV2

Connect a F-M jumper wire from GPIO3 (SCL) to LV2.

Step 9 Connect A4 (SDA) to HV1

Then, connect a F-M jumper wire from A4 (SDA) pin of the Little Bird Uno R3 to HV1.

Step 10 Connect A5 (SCL) to HV2

Finally, connect a F-M jumper wire from A5 (SCL) pin of the Little Bird Uno R3 to HV2.

Step 11 Conclusion

As you might realise now, connecting up a logic-level shifter to the Raspberry Pi and other components is simple! In this guide, you've learned about bi-directional logic-level shifters, when to use them and why.