1

Most of the sensors available in the market requires 3.3V-5V. The GPIO of the Raspberry Pi is 3.3V max only and not 5V tolerant. I have seen tutorials around the internet that they supply the sensors from 5V from the Raspberry Pi then connecting sensor's output pin to GPIO. Is that proper way to do it? I thought it will break the GPIO of the Pi.

Just to clarify, some of the sensors I've seen are DHT11, LM35, MQX Gas sensors etc (analog sensors are connected to ADC and that ADC is supplied using 5V from the Pi). Also for using I2C, UART and SPI I have seen they connect the supply from 5V of the Pi. Is that okay?

2 Answers 2

3

There is only one thing to bear in mind. Only feed between 0 and 3.3V to a Pi GPIO. Anything outside that range will eventually damage the GPIO and then the Pi.

You have to consider each device you wish to connect on a case by case basis.

Generally if a device is powered from 3V3 its outputs will be a Pi safe 3V3.

Generally if a device is powered from more than 3V3 (e.g. 5V) its outputs will be a not Pi safe more than 3V3 (e.g. 5V).

There are a variety of ways to drop an output pin voltage to a Pi safe 3V3. The simplest and probably the commonest is to use a voltage divider which just requires a pair of resistors.

You need to develop the skill of reading product data sheets and take particular note of their input voltage requirements and the voltage on their output pins.

1
  • You need to differentiate between a 3V3 sensor chip with its 5V0 powered module/breakout, which usually has a step down regulator, converting 5V power 3V3 output for the chip. The input/output of the 3V3 chip is still 3V logic level, though. The 5V0 input is for the convenience of user who almost always has an USB connector as 5V power. The 3V3 MPU6050 gyro/accelero chip is an example. The 5V powered GY521 module contains the 3V3 MPU6050. You might like to read the GYU521 schematic to check out the confusing details: raspberrypi.org/forums/viewtopic.php?f=37&t=234304#p1440912
    – tlfong01
    Commented Mar 16, 2019 at 13:48
1

@joan's answer contains excellent advice. My answer simply expands on that one to provide some other options, and address your question re I2C usage:

Interfacing Raspberry Pi's 3.3V GPIO with the "outside world" often adds a hardware hurdle. This is partly due to the fact that much of the outside world operates at 5.0V. Interfaces to the outside world bring additional risk of hardware damage for RPi users due to the fragile, unprotected GPIO hardware. And unfortunately, the RPi Foundation's documentation on GPIO fails to make it clear that you can destroy your Pi if you connect a 5V signal to a GPIO pin.

Interfacing your RPi's GPIO pins with devices whose output voltages exceed 3.3V will require reducing/shifting that voltage to 3.3V. @joan's suggestion to use a voltage divider is a good one. It's a simple solution, and may work perfectly well for your application. However, you should be aware that there are other methods to "skin this cat", each with its pros and cons. We'll look at two alternatives to the voltage divider, and then an approach for interfacing I2C, SPI and UART:

Zener Diode:

A Zener diode in series with a current-limiting resistor makes an effective voltage level shifter. At the circuit level, this is perhaps the simplest option. It requires only two components as shown in the following schematic:

schematic

simulate this circuit – Schematic created using CircuitLab

The advantage of the Zener diode is that if the sensor output increases above 5V, the voltage at the GPIO pin will be clamped at the Zener's reference voltage (3.0 V for the 1N4619). This may provide some measure of added safety for the RPi, as compared to a voltage divider.

Note also that the Zener diode and the voltage divider suffer from a limitation: They are only capable of taking a higher voltage as an input; they cannot take a 3.3V output from the RPi GPIO, and increase it to a higher level. In other words, this interface works only in one direction: from the sensor/peripheral to the RPi.

Logic Level Shifter

There is a class of devices called logic level shifters that have been around for some time. The original devices were unidirectional, meaning that one side of the shifter was always an input, the other always an output. Over time, design innovations resulted in level shifters that can sense direction of signal flow, and automatically configure themselves. An example of such a device is the TXB0108 from Texas Instruments. This particular device also offers ESD protection, which greatly reduces the risk of interfacing your RPi with the outside world. This device is also available in a small breakout board from Adafruit that lends itself to use with a Raspberry Pi.

The figure below illustrates the basic functionality of the TXB0108 level shifter. Technical details including a spec sheet, application notes and a usage guide are available from both TI and Adafruit.

TXB0108 Block Diagram

The TXB0108 is a much more complicated device than the voltage divider and the Zener diode. It will take more effort to integrate it into your circuit. You will need to decide if this additional complexity and effort are worthwhile. The big advantages of this device are the ESD protection, and the ability to implement bidirectional signaling with a peripheral device. The bidirectionality may be of no benefit in interfacing a one-way sensor such as a temperature or humidity sensor, but if you're interfacing with (for example) an Arduino, bidirectional signaling may be quite useful.

I2C, UART & SPI

I2C level shifting can be accomplished with the TXB0108 described above. Adafruit's website discusses this briefly, and TI claims I2C applications for it. However, due to the limited drive current capability of the TXB0108, and the vagaries in I2C pullup specifications, this device may not be the ideal choice. However, the people at Adafruit have another solution that is optimized for I2C: a 4-channel I2C-safe bidirectional level shifter that employs the BSS138 FET. There is also an informative App Note for the BSS138 that explains the level shifting technique used. Note that Adafruit also claims this device will support limited applications of SPI level shifting. It should also support level shifting for Raspberry Pi's UART, but not RS-232 to UART

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.