16 Replies Latest reply on Jul 18, 2017 9:32 AM by gpolder

    Logic Level Shifters for GPIO

    muthukmk

      Hi,

       

      I have accumulated drivers and sensors for Arduino over a period of time. IR proximity, ultrasonic sensor , 4-channel relay module, handful of L293N motor driver modules, A4988 stepper sticks and corresponding CNC shield etc.

       

      I have been successful in using them with Arduino (Mega and UNO). I have taken enough care in using those items which are power-hungry to be supplied with separate power sources and that approach worked well in Arduino context (eg. always power motors separately). Now, I want to use above sensors and actuators with Raspberry Pi 3 model B which I recently purchased. Now I need to find a proper way of dealing with logic-level related issues when I connect to RPi GPIO.

       

      I have some doubts after going through experiences on Google on various Instructables / DIY projects if its good to use level shifters particularly while interfacing above components with RPi GPIO.

      Many articles/discussions what I have read on internet seem to indicate that it should be ok to use them directly with RPi GPIO. But I wanted to confirm with those who would had been in similar situation. There may be more costly options (eg. a common adapter which lets such interfacing seamlessly possible without additional components )  which I want to avoid.

       

      So, specific questions

      a) Is it imperative to use logic level shifters to deal with 3.3v and 5v differences always for input as well as output ?

      b) Or its more of experimenting and finding what works best for each of standard modules like relay / motor drivers / ultrasonic sensors / PIR sensors etc?

       

      If I have missed any specific points to be added to the question, please let me know so that I can add the same

       

      Regards

       

      Muthu

        • Re: Logic Level Shifters for GPIO
          Roger Wolff

          For signals going into the raspberry pi, if the signal is really 5V then it is best to use some sort of level shifter. It depends a bit on how fast your signal is going to be changing on what approach is necessary. The simplest is to just put a big resistor in series with the signal. When the signal drives "5V" and the pi has an internal diode to 3.3V on the signal, a 100k resistor will allow a current of max 17 micro amps to flow into the raspberry pi gpio. Some manufaturers specify an allowed current of 10mA, staying below 1mA should be safe. The downside here is that capacitance on the GPIO pin might cause slower response on the raspberry pi side.

           

          Slightly more complex: I use a 1k resistor, together with a scottky diode to 3.3V. This allows quite quick signal changes and keeps the current into the broadcom CPU to a minimum.

           

          Even more complex would be to have a special level shifter chip and configure that for 5V on one side and 3.3 on the other.

           

          For signals going the other direction, a raspberry pi output going into some other chip, you again have several options. Do nothing is one. Many older chips are TTL compatible. They have a threshold voltage that is compatible with the 3.3V signal levels of the raspberry. (Threshold is: below this voltage a 0, or low will be seen, above that high, or one. )

           

          The L293 is an example, it will detect "high" above 2.3V, where the raspberry can deliver 3.3V, officially minus 10% or 3.0V without any issue.

           

          Modern chips will be 3.3V of themselves. The trickiest are the medium-old chips. (An example is: the AVR that is on your arduino).  Those are 5V CMOS. In practise they switch above VCC/2, or at 2.5V. But the manufacturers take some margin and specify "between 0.3VCC and 0.7VCC". This calculates as: The switch will happen between 1.5V and 3.5V. If you drive it with 3.3V, you are still not guaranteed to be detected as a "1" or "high". In practice, this almost never happens. For hobby projects, it is acceptable to hook it up and "see if it works". But not acceptable if you're designing the next-generation space shuttle...

          4 of 4 people found this helpful
          • Re: Logic Level Shifters for GPIO
            DAB

            The easiest way to do level shifting is to use a PSOC 4000 board (about 5 USD) as an interface to the RPi GPIO.

             

            The PSOC provides you with all kinds of easy level shifting for IO and provides a wealth of programmable Digital and IO blocks to simplify control and sampling.

             

            Take a look.

             

            DAB

            4 of 4 people found this helpful
              • Re: Logic Level Shifters for GPIO
                ntewinkel

                Very interesting way to do that, DAB! I would have never thought of that approach.

                It looks to be $4 USD: http://www.cypress.com/documentation/development-kitsboards/psoc-4-cy8ckit-049-4xxx-prototyping-kits

                Not sure what shipping would cost.

                  • Re: Logic Level Shifters for GPIO
                    DAB

                    Hi Nico,

                     

                    Remember the review I did a few years back.

                    I called them "Engineers Magic Wands" for a reason.

                     

                    These simple little boards can handle an amazing range of  interfacing tasks both digital and analog.

                     

                    If you do not have any, buy some.  They are just so versatile.

                     

                    DAB

                    1 of 1 people found this helpful
                  • Re: Logic Level Shifters for GPIO
                    koudelad

                    Hello @DAB,

                     

                    Just out of curiosity, could you, please, be a bit more specific about using the PSoC?

                     

                    I can imagine, it is perfect for performing a sensor hub in this case. It can even perform some real-time applications that the Raspberry Pi can't. The data from the PSoC can be sent to Pi upon request from the Pi. However, how to do the level shifting? The PSoC would have to be running at 3.3 V to comply with the Pi. How to interface the sensors that run at 5 V? Is it possible to do the level shifting internally?

                    According to the datasheet, maximum voltage at GPIO is -0.5 V to Vdd + 0.5 V and the maximum GPIO injection current for input voltage higherer that Vddd (or lower that Vss) is -0.5 to 0.5 mA.

                     

                    Thanks, David

                      • Re: Logic Level Shifters for GPIO
                        Roger Wolff

                        I stepped away from the PSOC devices.

                        Cypress promised me configurable analog circuitry. Sounds interesting... So I bought a device....

                         

                        * Bootloader support means that you can buy a $90 programmer, write your own bootloader and put that in the device.

                        * Analog configurability is only avialable on the 40xx devices with xx > 20, while I had bought the 00 or 10. The analog configurability was advertized as "for the 4xxx series".

                        * analog configurability means not much more than that you can select which input pin goes to the ADC. A standard feature on all microcontrollers. Nothing special.

                        * They promised to send me a 4020 stick when it became available, but didn't.

                         

                        So... the development sticks are really cheap, but not as useful as the nucleo boards from ST. Those have the programmer on board, and "bootloader" means you can actually program an otherwise empty chip.

                         

                        For someone getting into electronics, using an embedded processor to do level shifting is a bit too difficult to make it easy. For others who have the hardware and lots of experience in working with the chip, it'll be a 5 minute job, but not when you're new.

                          • Re: Logic Level Shifters for GPIO
                            koudelad

                            You made some good points, although my experience is better.

                             

                            I tried to list the new "USB stick" kits here: https://www.element14.com/community/docs/DOC-54414/l/cypress-linecard#comment-103831

                             

                            The cheapest kits don't have a dedicated programmer, but cost about $ 4 / € 4 (excluding VAT). I got one of the kits with a 42xx device on Embedded World exhibition a few years ago. I use this kit quite often and the only inconvenience is the bootloader. On the other hand, no other programmer is required (I programmed it hundreds of times), the device comes pre-programmed with the bootloader. There is basically no analog configurability, just the ADC.

                             

                            Cypress heard the community and the newer versions of these kits with PSoC 4 M / S / L devices feature a KitProg programmer (that can be snapped away and used separately) and cost about $ 10.

                             

                            I think you might like the PSoC Analog Coprocessor or PSoC 5 LP devices, which feature the analog parts you were missing: programmable op-amps, comparators, ADCs, DAC, analog multiplexer, filter block.

                             

                            I am just an amateur who previously programmed 8-bit PICs, but I have to say, there are a few things that the competition can't offer. On the other hand, my lack of digital and especially analog circuitry design is what prevents me from using the full potential of these devices. (Huge downside of not being an EE.) That is why I asked, whether there is some obvious way to do this level shifting using a PSoC, to learn something new However, using a level shifter with one the TLE291 etc. is the simplest way to go.

                             

                            EDIT: Crossed out "USB stick" like 4200 L kit, the only available is CY8CKIT-046 PSoC® 4 L-Series Pioneer Kit.

                            1 of 1 people found this helpful
                              • Re: Logic Level Shifters for GPIO
                                John Beetem

                                David Koudela wrote:

                                 

                                Cypress heard the community and the newer versions of these kits with PSoC 4 M / S / L devices feature a KitProg programmer (that can be snapped away and used separately) and cost about $ 10.

                                As far as I know, Cypress doesn't have a cheap "prototyping board" for the PSoC 4200L.  You have to get a PSoC 4200L Pioneer board for around US$50.  I like the 4200L specs, especially since it has USB and more programmable logic than the 4200.

                                1 of 1 people found this helpful
                                  • Re: Logic Level Shifters for GPIO
                                    koudelad

                                    You are right, I corrected my previous reply. But who knows, maybe there will be one in the future.

                                    BTW Cypress promised this "USB stick" like kit for the upcoming PSoC6, but this will take a while. The "big" kit is supposed to be available by the end of September and the smaller has only been confirmed as planned.

                                    • Re: Logic Level Shifters for GPIO
                                      Jan Cumps

                                      True. I'm a fan of the Cypress PSoC family.

                                      At the same time I also throw my hands in the air when someone (hey, Don) promotes them as logic level shifter, replacement for all FPGA or replacement for all ARM controllers.

                                       

                                      I like them, but they are just what they are: ARM microcontrollers with a little FPGA and analog configurable fabric. No magic. Just that.

                                        • Re: Logic Level Shifters for GPIO
                                          DAB

                                          Hi Jan,

                                           

                                          I agree, for a simple level shifter, the PSOC is maximum overkill.

                                           

                                          I am just trying to promote the device to people and make them aware that it is a truly versatile little board that can be paired with an RPi or other computer to handle many I/O issues with just a bit of wiring and software.

                                           

                                          As for FPGA, now there you have misunderstood.  A complex project that requires fast response and a lot of logic will always need a FPGA.

                                          The PSOC can help provide many simple digital and analog blocks with FPGA like capability, but there are just too few of them on each board to consider using them for a complex project.

                                           

                                          Magic is in the eyes of the viewer.  It may not be magic, but at times, it certainly looks close enough.

                                           

                                          DAB

                                          2 of 2 people found this helpful
                                      • Re: Logic Level Shifters for GPIO
                                        Jan Cumps

                                        I'm using strategy 1 and 3 of this blog post.

                                         

                                         

                                        5V to 3.3V Logic Level Shifting Stragety Notes | James Reuben Knowles

                                         

                                         

                                        #1 works well if you need to drop a few data lines.

                                        For #3, you *have* to order the right IC because not all manufacturers have a chip with this desired (side) effect of being a level shifter.

                                        The chip I use (and also Gadgetfactory, who use it as a I/0 buffer for their Papillio boards) is the SN74CBT16245DGGR .

                                         

                                        I've done a few videos on the subject:

                                         

                                         

                                         

                                        1 of 1 people found this helpful
                                • Re: Logic Level Shifters for GPIO
                                  ntewinkel

                                  eBay has a lot of inexpensive options available - a quick search for "logic level converter" or "logic level shifter" shows plenty of tiny boards with 4 or 8 channels for only $1 USD (free shipping too)... but you need patience, as shipping usually takes the scenic route!

                                   

                                  -Nico

                                  • Re: Logic Level Shifters for GPIO
                                    muthukmk

                                    Thank you Roger, DAB and Nico. I get a perspective of options available. I have some level shifters which Nico pointed out. I will spend some time exploring these options and post my observations.

                                     

                                    Regards

                                    Muthu

                                    • Re: Logic Level Shifters for GPIO
                                      gpolder

                                      the Sparkfun Level converter (https://www.sparkfun.com/products/12009) worked well for me ([Upcycle It] Nixie Display #9 - Hardware hacking ).

                                      There are other suppliers using the bss138 as well, Adafruit, ...