Digital input and logic levels
لم يُترجم بعد؛ يُعرض باللغة الإنجليزية
عن هذا الدرس
A digital input pin listens to a voltage and turns it into one bit. Wire a potentiometer to pin 2 of an Arduino Uno R3, set the pin up with pinMode(2, INPUT), and watch digitalRead(2) decide: at 5 V the ATmega328P datasheet promises that 1.5 V or less reads LOW and 3.0 V or more reads HIGH, with no promise in between. Then see why real outputs leave room for noise, and why a pin with nothing connected reads at random. The potentiometer is the voltage divider from the Circuits lesson “Voltage and current dividers”.
Make pin 2 an input
A GPIO pin (general-purpose input/output) can drive or listen. pinMode(2, INPUT) makes pin 2 listen. An input only senses a voltage: it lets less than 1 µA flow, so it hardly disturbs what it measures.
line 2 pinMode(2, INPUT);
pin 2 input, high impedance ("Hi-Z")
current under 1 µA leaks in or out
INPUT also turns the internal pull-up offاقرأ الدرس خطوة بخطوة
Make pin 2 an input
A GPIO pin (general-purpose input/output) can drive or listen. pinMode(2, INPUT) makes pin 2 listen. An input only senses a voltage: it lets less than 1 µA flow, so it hardly disturbs what it measures.
line 2 pinMode(2, INPUT); pin 2 input, high impedance ("Hi-Z") current under 1 µA leaks in or out INPUT also turns the internal pull-up offA knob sets the voltage
A potentiometer is a voltage divider you can turn. Its middle pin gives any voltage from 0 to 5 V. We wire that pin to pin 2, so turning the knob changes the voltage the chip sees.
pot 10 kΩ between 5 V and GND wiper V = 5 V × (fraction of the turn) 80% -> 4.0 V 16% -> 0.8 V 44% -> 2.2 V wiper -> pin 2Logic levels: 1.5 V, 3.0 V
A digital pin turns a voltage into one bit. At 5 V the datasheet promises: 1.5 V or less reads LOW, 3.0 V or more reads HIGH. These limits are called logic levels.
VCC = 5 V (ATmega328P, Table 30-1) VIL max 0.3 × VCC = 1.5 V at or below: ensured LOW VIH min 0.6 × VCC = 3.0 V at or above: ensured HIGH 1.5 V .. 3.0 V no promise4.0 V reads HIGH
Turn the knob to 4.0 V. That is above 3.0 V, so digitalRead(2) returns HIGH, which is 1. The exact voltage is thrown away: 3.2 V and 5 V give the same 1.
line 5 int v = digitalRead(2); pin 2 4.0 V (80% of the turn) compare 4.0 V >= 3.0 V -> HIGH v 10.8 V reads LOW
Turn it down to 0.8 V. That is below 1.5 V, so digitalRead(2) returns LOW, which is 0. Any voltage from 0 V up to 1.5 V gives the same 0.
line 5 int v = digitalRead(2); pin 2 0.8 V (16% of the turn) compare 0.8 V <= 1.5 V -> LOW v 02.2 V: no promise
Now 2.2 V, between the two lines. The datasheet promises nothing here: one chip may read 1, another 0, and small noise can flip it back and forth. Good designs never leave an input in this zone.
pin 2 2.2 V 1.5 V < 2.2 V < 3.0 V -> not guaranteed v 0 or 1, and it can change between reads a Schmitt trigger adds a little hysteresis (typical only)Room for noise
The knob now plays another 5 V chip's output. Its HIGH is at least 4.2 V: noise can knock off 1.2 V and the pin still reads HIGH. Its LOW is at most 0.9 V: 0.6 V of room. That room is the noise margin.
output HIGH >= 4.2 V input needs >= 3.0 V margin 1.2 V output LOW <= 0.9 V input needs <= 1.5 V margin 0.6 V (output figures at 20 mA, VCC = 5 V)Nothing connected: floating
Unplug the wire and pin 2 floats: nothing sets its voltage. It picks up electrical noise, and digitalRead returns HIGH or LOW at random. The lesson "Pull-up and pull-down resistors" shows the fix.
pin 2 nothing connected -> floating voltage wanders with noise from nearby wires and your hand v HIGH or LOW at random fix a resistor to 5 V or GND (next: pull-ups)
أسئلة سريعة: 5
اختر إجابة واحدة لكل سؤال. يمكنك تغيير اختياراتك قبل التحقق منها.
01An ATmega328P runs on 5 V. Pin 2 is an input at 4.0 V. What does digitalRead(2) return?
4.0 V is above VIH = 0.6 × 5 V = 3.0 V, so the datasheet ensures it reads HIGH, which is 1. digitalRead only ever gives HIGH or LOW, never the voltage.
02The same pin sits at 2.2 V. What can you say about digitalRead(2)?
2.2 V is between VIL (1.5 V) and VIH (3.0 V). The datasheet makes no promise there, so a good design never leaves an input in that zone.
03A 5 V output gives at least 4.2 V for HIGH. The input needs at least 3.0 V. What is the HIGH noise margin?
4.2 V − 3.0 V = 1.2 V: noise can pull the line down that much and it still reads HIGH.
04A pin is set to INPUT with nothing connected to it. What happens when you read it?
With nothing setting its voltage, the pin picks up electrical noise, and the Arduino reference warns digitalRead can return either value and change randomly. A pull-up or pull-down resistor fixes it.
05On an ESP32 at 3.3 V, VIH is 0.75 × VDD. Which voltage is ensured to read HIGH?
0.75 × 3.3 V = 2.475 V, so 2.6 V reads HIGH. 5 V is above the 3.6 V an ESP32 pin may take, so it needs level shifting.
المسرد
- GPIO
- General-purpose input/output: a chip pin your code can set up to drive a voltage (output) or to sense one (input).
- Digital input
- A pin that reads a voltage as one bit: HIGH (1) or LOW (0).
- Logic levels
- The voltage limits that decide HIGH and LOW. On a 5 V ATmega328P: 1.5 V or less is LOW, 3.0 V or more is HIGH.
- VIL / VIH
- Input Low and Input High voltage: the highest voltage ensured to read LOW and the lowest ensured to read HIGH.
- High impedance (Hi-Z)
- An input barely takes any current (under 1 µA here), so it hardly changes the voltage it measures.
- Noise margin
- How far a real output's HIGH or LOW can be pushed by noise before the input stops being sure of it.
- Floating
- A pin with nothing setting its voltage. It picks up noise and reads HIGH or LOW at random.
- Potentiometer
- A resistor with a sliding middle contact: a voltage divider you can turn by hand.
الملاحظات والمصادر
- The limits come from the ATmega328P datasheet (DS40002061B, Table 30-1) for VCC from 2.4 V to 5.5 V: VIL at most 0.3 × VCC and VIH at least 0.6 × VCC. The datasheet says "Max" is the highest voltage ensured to read low and "Min." the lowest ensured to read high.
- Each input passes through a Schmitt trigger (datasheet §14.2.5, Figure 14-2): once it switches, the voltage must move back a little before it switches again. This gap is called hysteresis. The datasheet shows it only as typical curves and guarantees no minimum for ordinary pins. Where a chip switches inside the no-promise zone also varies from chip to chip. The figure's simulated chip switches at 2.2 V with 0.25 V of hysteresis; both are made-up example values, not datasheet figures.
- The output figures are for a pin carrying 20 mA at VCC = 5 V and 85 °C: VOH at least 4.2 V and VOL at most 0.9 V. With a light load, such as one input, the output sits much closer to 5 V and 0 V, so the real margin is larger.
- A 3.3 V chip has its own lines. The ESP32 datasheet gives VIH at least 0.75 × VDD (2.475 V at 3.3 V) and VIL at most 0.25 × VDD (0.825 V), and no more than VDD + 0.3 V (3.6 V) on a pin. So 5 V must never go straight into an ESP32 pin: see "3.3 V and 5 V: level shifting".
- A voltage that sits near the middle for a long time also wastes a little power in the input stage, which is why the datasheet asks you to avoid floating inputs and analog levels near VCC/2 on digital pins. To measure a voltage, not just HIGH or LOW, use an analog pin and analogRead().
اختبار يتاح بعد الخطوة الأخيرة.