I've introduced some of my friends into the wonderful world of Arduino and making and a lot of them have a hard time understanding working with different logic levels, so I thought it would be a good idea to make a simple guide about it. So if you ever struggled with logic level shifting this is the guide for you!
I'll show two really simple ways of doing logic level shifting and explain why or when you might need to do it.
Checkout the two minute video where I go through it and check out my YouTube channel for other videos on ESP8266 projects and tips.
Lets get to it!
Step 1: What Are Logic Levels?
So what are logic levels?
Logic levels normally refers to the HIGH or LOW state of your arduino pins, but not all logic levels are the same.
More traditional Arduino boards such as an UNO or a Nano have a 5V logic level, which means when you set a pin HIGH, it will measure as 5V.
But other common boards such the ESP8266 have a 3.3V logic level, which, as you can probably guess, means that when you set a pin HIGH it will measure as 3.3V.
5v and 3.3V are by far the most common logic levels that you will come across in hobby electronics.
It's also important to note that sensors often have these logic levels too. An Ultrasonic sensor is a 5V sensor, while most screens run at 3.3V logic levels
Step 2: Ok, But Why or When Do We Need to Shift Them?
If you are using a micro controller with a different logic level than a sensor you may have to consider logic level shifting, but not always.
3.3V signals going to a 5V device:
Normally if you have a 3.3V device sending signals to a 5V device you will not have any problems, so even though the 3.3V device will only measure at 3.3V when set to HIGH, this is normally over the threshold of what a 5V device considered HIGH. The first picture shows a visual representation of this.
There will be some exceptions to this, such as sometimes you will see Neopixels require logic level shifting as the 3.3V signal doesn't always reliably work with Neopixels running at 5V (My Google Maps Arduino Commute Tracker project didn't work with the 3.3v signal of my ESP8266 for example)
5V signals going to a 3.3V device:
This is where you will run into problems! Normally the max voltage a 3.3V device can accept on its pins is around 3.6V, so a 5V signal could potentially damage your device.
A good real world example of the above is connecting to an Ultrasonic sensor (5V) with an ESP8266 (3.3V). How an ultrasonic sensor works is when it receives a HIGH signal on its trigger pin it will emit an ultrasonic signal and when that signal bounces back it will send a HIGH signal on the echo pin and the microcontroller calculates the distance of the object based on how long there was between the trigger pin and the echo. The 3.3V signal on the trigger pin will work fine, but the 5V response on the echo pin will need to be shifted.
Step 3: Method 1: a Voltage Divider
The simplest and most common method of logic level shifting is a voltage divider.
In the circuit shown as in the first picture, the output of the divider is the line coming out from the middle. There is a total of 5V dropped across the circuit, but we can can calculate how much voltage is dropped across each resistor. Meaning with specific resistor values, we can get the divider to output a signal that is safe for our 3.3V device.
The equation to calculate the voltage drop across a resistor is
(R2 / (R1 + R2) ) * V
So if we use 10K Ohm resistor for R1, and a 22K Ohm resistor for R2 you would get 3.4V on the output of the divider, which would be a safe value to use on our ESP8266.
If you look at the last image it might show a clearer image of how you would wire this up.
Additional Tip (can skip if you want!):
Someone asked before could they build it with a very limited values of resistors and the answer is yes! If you have 3 of any type of resistor, if you place 3 of them in series and take the output of the circuit between R1 and R2 you will get a perfect 3.3V signal! It is recommended you use higher value of resistors so your circuit uses less current. (at least 1K i'd say)
The equation for calculating the voltage drop across the last two resistors would be
(R2 + R3 / (R1 + R2 + R3) ) * V
But if all the resistor values are the same it cancels down to
2/3 * V
Step 4: Method 2: a Bi-Directional Logic Level Shifter
This is my preferred method of doing Logic Level shifting, mainly because i'm terrible at organizing my resistors and find it hard to find the values I'm looking for! I also like them as you often need to shift several pins at once, so this is a good option for that.
It is a device called a Bi-Directional Logic Level Shifter, and they can be bought for about 30c delivered.
Basically how you use them is you connect your higher voltage (normally 5V) to the HV pin and then your lower voltage (normally 3.3V) to the LV pin, and then any signal received on any of the pins will be output on the equivalently numbered pin on the other side at the appropriate voltage.
So an example of this would be if a 5V signal was received on HV1, it would be output at 3.3V on LV1. And then also if a 3.3V signal was received on LV4, it would be output on HV4 at 5V
You can buy the Level shifters from Aliexpress for $1.20 for 5*
Step 5: Conclusion
So hopefully you now understand a little bit more about level shifting, why and when you need to do it and how to do it.
If you have any questions please let me know I'd be happy to try help!
Thanks a lot!