ENع
→Digital input
الدرس 09 · Digital pins: inputs and outputs

Digital input and logic levels

لم يُترجم بعد؛ يُعرض باللغة الإنجليزية

10 دقائق8 خطوات5 أسئلةBeginner
عن هذا الدرس

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”.

ستتعلّم

حدّد ما تشعر بأنك أتقنته. هذا التقييم الذاتي لا يؤثر في درجة الدرس.

شكل 9 · تفاعلي
01 / 08

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
جارٍ تحميل الرسم…
أو ← →
اقرأ الدرس خطوة بخطوة
  1. 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
  2. A 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 2
  3. Logic 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 promise
  4. 4.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         1
  5. 0.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         0
  6. 2.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)
  7. 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)
  8. 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)

اختبار يتاح بعد الخطوة الأخيرة.