loading

2 cent TTL Level Shifter - connect a 3.3V Pi to 5V TTL serial device or Arduino and more...

Picture of 2 cent TTL Level Shifter - connect a 3.3V Pi to 5V TTL serial device or Arduino and more...
level shifter.JPG
Serial comm.jpg
I will start out by noting that this instructable pertains only to TTL serial, and not RS232.
It should serve well for any 2 devices that you need to have serial communications between that have TTL serial capability.
Almost all the popular prototyping boards have TTL serial, but unfortunately, they don't all run at the same voltage.
The 5V device is safe, and can receive data from the 3.3V device without issue in almost all cases.
The problem comes when you have to send data to the 3.3V device from the 5V device.

This is also useful for connecting the Pi to a 5V USB TTL serial cable - and may become the most popular use for it!

If you have seen my 8X8X8 LED Cube instructable, you may have noticed there was one concern about hooking up the Raspberry Pi to the cube.
That problem is TTL Level shifting.
The ATmega chips can take 3.3V TTL serial, because 3.3V is still a "Logic 1" in the 5V world.
But if we want to send data back to the Raspberry Pi, we need to level shift that 5V signal or we'll
blow the input on the Raspberry Pi.

So I started looking at buying a TTL Level Shifter.
2 problems. Ones like this one cost $15 after shipping, and have way more than what I need.
Almost all the TTL level shifters I looked at were 8 bits wide, and many were bi-directional.
I only need 1 bit, and it only has to be uni-directional
So then I looked at my parts bin and thought "I should be able to just build what I need without spending anything"
At most, this will cost you the price of 2 to 3 resistors. Using the formula I put up here, you should be able to scrounge resistors from almost anywhere that will do - you don't have to use the values I use as examples. I just use 100 ohm resistors because I have over 4500 of them.

This instructable should actually work for most 5V to 3.3V TTL Serial communication between devices.
For instance, I included the pins for the Arduino, so you can see this would be a great way to communicate from Arduino to Pi.

CAUTION: ATTEMPTING ANY OF THIS IS ALWAYS DONE AT YOUR OWN RISK. I say this mostly because if you get the RX and TX backwards or mis-wire something and send 5V into a 3.3V input, you can damage the 3.3V device.
Double check everything, and then check it again before powering up. That said, let's continue!

If you saw my instructable on the Simplest iPhone/iPod/iPad charger circuit, you know I like to break things down to the simplest of terms. Well, here it is. 3.3 is 2/3 of 5. So if we have 5V, then we need to make that 2/3 of that.
In this circuit, R1 is tied to the 5V signal. R2 is either 2 R1 resistors, or a resistor 2X the value of R1.
So if you have a bunch of 100 ohm resistors (or just about any other value, as long as they are the same) put one on the 5V signal, then put the other 2 in series from that point to ground. The junction of R1 and R2 will be 2/3 of the 5V, or 3.3V. Simple.
romilly3 months ago

You might find this interesting:

http://www-ee.eng.hawaii.edu/~tep/EE491E/Notes/HC1...

It's a scan of an early Mototrola SPI spec. There's no formal SPI standard specification, so I'd be happy to accept this as authoritative.

SuperTech-IT (author)  romilly3 months ago

You mean where it says in 6.1 that 3 wire COMMUNICATION is possible, but in 6.2 it describes the FOUR SIGNALS of the protocol? GOOD. Glad you understood the document instead of just scanning it for just enough information to think you were right. It takes a big man to provide the PROOF that he was wrong. Thank you for being that man.

SuperTech-IT (author)  SuperTech-IT3 months ago

Please note that I did concede that you were actually correct on the OTHER points you made - just not about it being a 3 wire PROTOCOL.

SuperTech-IT (author)  SuperTech-IT3 months ago

Also please note that even if you only use 3 wires for your communication, you CANNOT IGNORE the fourth signal. Whether you tie it to +V or tie it to GND to make it work, YOU STILL MUST USE THE FOURTH SIGNAL !!!!

In the case of multiple slaves, this line must be controllable from the master.

romilly3 months ago

This is not a risk-free approach.

At power-up the Rx pin on the Arduino is held at +5v volts by a pull-up resistor, so the direct connection to the Pi will eventually damage the Pi's Tx input. A strong 5v signal is also applied to the pin when programming the Arduino, so you should never program the Arduino while it is connected to the Pi using this technique.

If you're going to use this approach you will need a series resistor between the Arduino x and Pi Tx pins.

Connecting via a level shifter (purchased or home-brew) is much safer.

The Serial protocol referred to in this article has nothing to do with SPI which is a three-wire serial protocol originally defined by Mororola: http://en.wikipedia.org/wiki/Serial_Peripheral_Int...

SuperTech-IT (author)  romilly3 months ago

Noted, and a good caution. Please note that if you damage your Pi doing this, you have been warned, and I will not be held liable. However, I have yet to damage my Pi doing this. Also, this danger is not present if you are programming your Arduino via the ICSP, and the risk while programming via the serial isn't quite as bad as it may seem, as there is actually a 1K resistance between the USB/serial chip and the ATmega328P, so the "STRONG" signal during programming actually isn't strong at all if you are using the USB interface to program it. In fact, it will easily be held to 3.3V or even 0V depending on the output state of the Pi's Tx line. Romilly does still however present a valid point, and it certainly won't hurt to do like the Arduino does, and put a series resistor in the line from the Arduino Rx to the Pi's Tx. The value isn't terribly important, but I'd probably use between 500 ohms and 1K.

As far as SPI goes - If you are simply asking if for instance this basic method could be used to connect a 5V SPI master to a 3.3V SPI slave, then yes, you should be able to treat the MISO line like we did here to the Arduino's Rx line (as discussed above, with serial resistance) and the rest of the lines level shifted as in the diagram on the Arduino's Tx line. I would also like to point out that SPI is a FOUR wire protocol.

I knew the three vs. four-wire SPI controversy would re-surface :)

The reason that I (and many articles) describe SPI as a three-wire protocol is that three wires (MISO, MOSI and SCLK) are needed to carry SPI data. Of course, most SPI devices require you to connect three other wires: Ground as a common reference, Chip select (CS), and power. From my perspective CS isn't really a signal wire. Many SPI application use a single SPI device and just tie CS to ground. (CS is usually an active-low signal).

SuperTech-IT (author)  romilly3 months ago

It's not a "controversy" just because you arbitrarily decide that "from your perspective" one of the wires doesn't really count - it is called a 4 wire protocol specifically to distinguish it from actual 3 wire protocols. In other words, just because you are colour blind, that doesn't mean there is a "red wire vs grey wire controversy". If the rest of the world calls the wire red, you can't just start calling it grey because that is how you happen to see it.

SuperTech-IT (author)  SuperTech-IT3 months ago

I'll go a step further and discount any and all articles that call it a 3 wire protocol the same way I discount any and all articles that refer to controlling a great deal of outputs using a small number of lines "multiplexing". These people are simply wrong. period. Just because some people don't know the difference between what DEmultiplexing and multiplexing is, and they decide to write an article about "multiplexing" the outputs on your Arduino, doesn't mean that multiplexing is the correct term. Just because many applications only use one slave device doesn't mean that CS or SS is suddenly no longer part of the protocol itself and therefore no longer part of the protocol definition.

sharkyenergy5 months ago
thanks it worked like a charm! used 3 470 ohm resistors! :)
SuperTech-IT (author)  sharkyenergy5 months ago

Glad it worked for you. Most people over complicate very simple issues and use all kinds of hardware to do something that is easy as Pi to do if you just think about it.

HomuraRun1 year ago

Does this work with SPI?

ngardiner1 year ago
Thanks, a very simple solution to the problem!