Some devices have a different supply voltage than the Arduino which you might want to let communicate with them.
For example the NRF24L01+ wireless-module or the RC522 RFID-module. Both work with 3.3V (the Arduino Uno/Nano works with 5V). The reason why you can let them communicate with each other with a direct line is that these modules do have logic level shifters.
Such a level sifter sets the logic level of 5V to the logic level of 3.3V. What's now the logic level? Digital data are sent in bits. That's two possible states (0 & 1). Those states have, depending on the voltage, a range.
- 5V logic zero: 0V – 0.4V (0V – 0.8V)
- 5V logic one: 2.7V – 5V (2V – 5V)
- 3.3V logic zero: 0V – 0.5V (0V-0.8V)
- 3.3V logic one: 2.4V – 3.3V (2V – 3.3V)
The values inside the brackets are the maximum values.
If the 3.3V-module sends a massage to the 5V-module you don't have any problems, because 2.4V-3.3V are inside of logic one on the 5V module. The other direction isn't that simple, because the voltage of 5V would damage the 3.3V-module.
Some devices do not have logic level shifters. For example the ESP8266, which can communicate with the Arduino via the serial interface (RX, TX). In that case you have to find a solution on your own. Of course there are cheap level shifters you can buy, but I want to show how you can make them on your own. Basically you only need two resistors.
In the shematic plan you can see, that the ESP8266 can be supplied with 3.3V from the Arduino. The ESP's TX pin can be connected directly with the Arduino's RX pin. That way the ESP8266 sends only 3.3V to the Arduino - logic one is reached - everything's fine.
The other direction (Arduino-TX to ESP8266-RX) requires two resistors. The size of the resistor is choosen in a way, that the voltage is set in thirds. One third of the 5V voltage is going to be converted to heat in the R2 = 1kΩ resistor. That means that 2/3 of the Arduinos voltage will arrive on the ESP, which is roughly 3.3V. Now you just need a pull-down resistor, so you don't have any wrong signals due to voltage variations. The resistor now has to convert 2/3 of 5V into heat, therefore: R1 = 2kΩ.
The R3 resistor is just for the Wifi module, it's not necesarry for the level shifter.
This is actually the easiest way to build a logic level shifter. If you want to you devices that operate with 1.8V or 2.8V you have to find a different solution. The reason is that logic one can't be reached on the 5V device.
The following example shows the ESP8266 again, but now the level shifter is bi-directional. That way it doesn't only step down the 5V, but also step up the 3.3V. In that case you can also use above mentioned 1.8V or 2.8V devices combined with a 5V device.
How does this circuit work? First I will tell you the pinout by the shematic plan.
- Left: Drain
- Right: Source
- Down: Gate
The transistor is built as a p-channel, normally conducting. So it's conducting if both signal lines on the sides are LOW. If the source is LOW the gate will become positive (regarded to the source). Is the drain LOW the zener diode starts conducting and lowers the source voltage as long as the gate voltage is high enough to make the transistor conducting.
On the gate you always have to apply the line with the lowest voltage.
The 4 resistors (R1-R4) don't have to do anything with the level shifter. They are just used as pull-up resistors for signal interference suppression. The reistor must be big enough to handle the current of both sides.
Again, the R5 resistor is just here for the ESP module.
Who doesn't want to build a level shifter on his own: There are plenty of devices only. The following example shows a 4-channel shifter which costs about 2€ or less.
Behind such a deivce stand exaclty the above shown MOSFET technology, just smaller.
If the device with the lower voltage is already supplied with current (through a battery or something else) you don't need the bottom red line. But the Arduino and the external device always have to share the same ground. On the purchased level shifter this is done via the GND-pins. On the selfmade one you have to connect the ground lines directly via a cable.
Questions and comments: deloarts.wordpress.com