Sub topic 12
Logic levels: why 5V and 3.3V matter
This chapter assumes you know voltage, current, and resistance, can read a simple breadboard diagram, and are comfortable with setup() and loop(). You have already seen the power pins and the digital pins. Here you will learn how the board turns voltages into the ones and zeros your sketch sees, and how to avoid connecting parts that use different voltage standards.
What a logic level is
A digital pin does not measure a number. It decides whether the voltage on it counts as HIGH or LOW. Those two words are logic levels. A logic level is a range of voltage that a chip treats as one binary state.
The range depends on the voltage the chip runs from, called its operating voltage. The Uno's ATmega328P runs at 5V. A 3.3V board such as an Arduino Due or a Nano 33 IoT runs its chip at 3.3V. The two families use different voltage ranges for HIGH and LOW, so they cannot always be wired together directly.
Input thresholds and output levels
A chip's datasheet gives four values for each pin:
- VIL: the highest voltage still read as LOW.
- VIH: the lowest voltage read as HIGH.
- VOL: the highest voltage the pin outputs when it drives LOW.
- VOH: the lowest voltage the pin outputs when it drives HIGH.
For the ATmega328P at 5V supply, the datasheet gives roughly these figures:
| Value | Approximate limit at VCC = 5V | Meaning |
|---|---|---|
| VIL | 0.3 × VCC, about 1.5V | Input at or below this reads LOW |
| VIH | 0.6 × VCC, about 3.0V | Input at or above this reads HIGH |
| VOL | up to 0.9V at 20 mA | Output driving LOW |
| VOH | at least 4.2V at 20 mA | Output driving HIGH |
Check the datasheet for the exact numbers on your chip. The key point is the gap between VIL and VIH. A voltage from about 1.5V to 3.0V sits in an undefined zone. The pin may read HIGH, LOW, or flip between them. This is why a floating input gives random readings, and why a weak or noisy signal can cause errors.
Because VIH on a 5V Uno is about 3.0V, a 3.3V output usually reads as HIGH on the Uno. The margin is small, only about 0.3V, so noise or a long wire can break it. A 5V output going into a 3.3V chip is the dangerous direction, covered below.
Why the mismatch is a problem
There are two directions to think about.
A 3.3V device driving a 5V input
A 3.3V output is above the 5V chip's VIH of about 3.0V, so the Uno will normally read it as HIGH. This often works. It is not guaranteed across all parts and temperatures, so check the numbers for your devices before you rely on it. If the margin is too thin, use a level shifter, described below.
A 5V device driving a 3.3V input
This is the risky direction. Most 3.3V chips list an absolute maximum input voltage of about VCC + 0.3V, or about 3.6V. A 5V signal exceeds that. Current then flows through the chip's internal protection diodes, and it can overheat or damage the input. Some chips are "5V tolerant" on certain pins, but you must confirm that in the datasheet. Do not assume it.
Ways to match levels
You have three common options.
- Resistor voltage divider. Two resistors split a 5V signal down to about 3.3V. It works for one-way signals going from 5V to 3.3V and is cheap. It is slow on fast signals because of the resistance and the pin's capacitance, and it does not work in the other direction.
- Level shifter module. A small board with transistors or a dedicated chip converts levels in both directions. Bidirectional shifters suit buses such as I2C. This is the most reliable choice for most projects.
- Choose compatible parts. Many modules include their own regulator and level conversion. Read the product page to see which logic voltage the signal pins expect.
Here is the divider arithmetic. With R1 = 1 kΩ from the 5V signal to the junction and R2 = 2 kΩ from the junction to GND, the junction voltage is:
v_in = 5.0
r1 = 1000.0
r2 = 2000.0
v_out = v_in * r2 / (r1 + r2)
print(f"Divider output: {v_out:.2f} V")The output is 3.33V, which is safe for a 3.3V input. Use this only for a signal that flows one way, from the 5V side to the 3.3V side.
Reading your own logic level in a sketch
You can see the effect of the thresholds with the analog input. This sketch reads a pin both ways: as a digital input and as an analog value. Wire a 3.3V source (for example the Uno's 3.3V pin through a 1 kΩ resistor) to pin 2 and to A0 to compare.
const int signalPin = 2;
void setup() {
Serial.begin(9600);
pinMode(signalPin, INPUT);
}
void loop() {
int digital = digitalRead(signalPin);
int raw = analogRead(A0);
float volts = raw * (5.0 / 1023.0);
Serial.print("Digital: ");
Serial.print(digital);
Serial.print(" Analog: ");
Serial.print(volts, 2);
Serial.println(" V");
delay(500);
}With 3.3V applied, the analog reading should show about 3.3V and the digital reading should be 1, because 3.3V is above VIH. Lower the voltage with a potentiometer and watch the digital value change somewhere in the 1.5V to 3.0V zone. The exact switching point varies from chip to chip, which shows why that range is called undefined.
Operating voltage and the IOREF pin
The board's operating voltage is the voltage the microcontroller runs at. The Uno exposes it on the IOREF pin. IOREF lets a shield read the board's logic voltage and adapt to it. On a 5V Uno, IOREF is 5V. On a 3.3V board it is 3.3V. A well-designed shield uses this to choose its own signal levels.
Your sketch's pin outputs swing between 0V and the operating voltage. On the Uno, a HIGH output is close to 5V. The 3.3V pin is a separate regulated supply for other parts. It does not change the logic level of the pins.
Common mistakes
- Powering a 3.3V sensor from the 5V pin because the module "looks like" it will work.
- Connecting a 5V Uno's TX pin directly to a 3.3V module's RX pin. This is a frequent way to damage a serial module.
- Assuming all of a 3.3V board's pins are 5V tolerant. On many boards none of them are.
- Using
analogReadconversion with5.0on a 3.3V board. The conversion factor must match the reference voltage, or readings come out wrong. - Forgetting that a sensor's supply voltage and its signal voltage can differ. Check both.
Summary of rules
- A pin's HIGH and LOW depend on its operating voltage, with an undefined zone between VIL and VIH.
- A 3.3V output into a 5V Uno input usually works, with a small margin.
- A 5V output into a 3.3V input needs a divider, a level shifter, or confirmed 5V tolerance.
- Read VIL, VIH, VOL, VOH, and absolute maximum input voltage from the datasheet, not from guesses.
Exercise
A 3.3V sensor outputs a digital signal that you want to read on a 5V Uno, and the Uno also sends commands back to the sensor on a separate wire. Which direction is risky, and what would you use on that wire?
Show answer
The Uno-to-sensor wire is risky, because the Uno's 5V HIGH exceeds the sensor's roughly 3.6V absolute maximum input. The sensor-to-Uno wire usually works, since 3.3V is above the Uno's VIH of about 3.0V, though the margin is small. On the Uno-to-sensor wire, use a resistor divider (for example 1 kΩ and 2 kΩ giving about 3.33V) or a level shifter. A level shifter also gives a safer margin on the return wire.
Exercise
A 5V Uno reads a pin voltage of 2.2V on a digital input. What will digitalRead return, and why is relying on it a bad idea?
Show answer
The result is not guaranteed. 2.2V lies between VIL (about 1.5V) and VIH (about 3.0V), the undefined zone, so the pin may read HIGH, LOW, or change between them with noise or temperature. Design the circuit so the signal is clearly below 1.5V or above 3.0V.