So you’ve got your microcontroller/development board ready to go, you’ve got your sensors and external components and you’re ready to build an IoT device to make the world your slave. But hang on a minute, the sensor you have only operates on 5V and you vaguely recall that using 5V with a 3.3V device could be bad. Well, you’re not wrong, that would be very bad. But never fear, today we’ll be looking at how to use nifty devices call logic level shifters (also known as converters) and how we can use those to interface devices with different logic levels. You see, whilst 5V used to be the standard for logic operating voltages, as devices get smaller and more power efficient, they use lower and lower voltages to reduce power consumption and wasted energy. And many devices now work on 3.3V or even lower, and most of those don’t like being used with higher voltages. In fact, you can damage an entire board just from a brief encounter with a voltage that it isn’t rated for. If you’re not sure what we mean talking about voltage levels and other such terms, check out our Analogue Electronics Crash Course to get a handle on how electricity and electronics components all work.
So what do we mean when we’re talking about logic levels? Simply put, a logic level is a voltage recognised by a system as the typical voltage of a high signal. This voltage level issued used by chips to determine whether the signal is high or low (binary logic) and then can make computations based off that. Traditionally, 5V was the default logic level for most systems, however as silicon chips have gotten smaller and more efficient, 3.3V is now the standard for most microprocessors (higher level processors like those found in modern desktops/laptops can be as low 1.2V).
The issue with this is that for a chip running at 3.3V if you connect 5V up to its pins unless it’s specifically tolerant to the higher voltage, you can risk damaging the pin, or the entire chip. Now that damage only goes one-way. If you connect 3.3V up to a 5V pin, you won’t damage it, however, 3.3V may not be high enough for it to register a high signal, so it may or may not work correctly. However, you may find that some 5V devices work just fine when being driven from a 3.3V signal. This is because a voltage doesn't have to be exactly on the high voltage limit (3.3V or 5V) to be considered a 'high' digital signal. There exists 'windows' or ranges for whether a voltage triggers a high or a low signal. This is because, many devices may not output exactly 5V; it may be 4.8V, or 5.1V, however, it will still be adequate for a high signal. The exact thresholds and levels differ across devices and voltages, however, let's take a look at a typical TTL (transistor-transistor logic) as used in many digital systems:
That isn't a complete guide to TTL voltage thresholds as it differs from input device to output device to eliminate noise, however, the concept is there. So the general rule of thumb is to match the logic level voltage of external components, shields, modules, or other boards to the board/chip you’re connecting to. Now how can we interface two things that operate on different voltages? A magic piece of hardware called a ‘Logic Level Shifter’ (or converter).
How Do They work?
One of our most popular logic level shifters is this one from Sparkfun. It’s a four channel, bi-directional shifter which is awesome because it means that it will convert from a high to a low voltage, and from a low to a high voltage which makes it so useful to have for going between 3.3V and 5V devices. Now don’t think that this board will step-up your 3.3V to 5V by itself. You can see on the board the HV and LV pads which mean you need to have 3.3V and 5V lines to connect to it, and then the onboard BSS138 FET shifts the signal up or down. You can use this board with voltages other than 5V and 3.3V (the BSS138 is rated for up to 50V) however it’s mostly used for shifting these logic level voltages. Now we know a bit about what logic level shifters are, let’s take a look at how to use them to interface 5V devices with our Raspberry Pi.
Logic level conversion is straight forward. You simply connect up the HV and LV pins to the high voltage and low voltage that you want to switch and then connect the pins up to any of the available channels. We’ve provided a couple of hardware examples on how to hook up various boards and platforms. Bear in mind though that because they’re such simple devices, logic level shifters will work on any pin sending/receiving digital data. What they won’t do is map analogue voltages. It’s either on or off, digital signals.
We’ve set up a Raspberry Pi, Arduino, and Particle board connected via I2C through a logic level shifter and because the Pi and Particle boards and 3.3V, and an Arduino Uno is 5V, we’ll need to convert them.
Arduino and Raspberry Pi:
Arduino and Particle Photon I2C:
As you can see, it’s incredibly simple, and we’ve attached ground wires along with voltage taps from each board, then connected the SDA and SCL wires from each board to the shifter which will take care of the voltages, and we can use the I2C bus to communicate bi-directionally without any danger of damaging either board.