ENع
→Pull-ups
الدرس 10 · Digital pins: inputs and outputs

Pull-up and pull-down resistors

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

12 دقيقة9 خطوات5 أسئلةBeginner
عن هذا الدرس

A button wired to an input pin seems simple, until the pin floats: joined to nothing, it reads 0 or 1 at random. On an Arduino Uno R3, follow pin 2 as a 10 kΩ pull-up holds it HIGH, a press pulls it LOW while 0.5 mA flows, and pinMode(2, INPUT_PULLUP) swaps in the chip's own 20 to 50 kΩ resistor. Then flip it into a pull-down. This builds on the lesson “Digital input and logic levels”; the Circuits lesson “Voltage and current dividers” explains why the pressed pin sits at 0 V, and the Hardware lesson “Transistors: the switch that changed everything” shows what sits behind an input pin.

ستتعلّم

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

شكل 10 · تفاعلي
01 / 09

A pin joined to nothing

A button joins pin 2 to ground (GND, 0 volts). With the button open, the pin is connected to nothing at all. We call it floating, and digitalRead returns 0 or 1 at random.

board     Arduino Uno R3 (ATmega328P, 5 V)
wiring    pin 2 -> button -> GND
line 1    pinMode(2, INPUT);
open      pin joined to nothing: floating
read      0, 1, 1, 0, ... at random
جارٍ تحميل الرسم…
أو ← →
اقرأ الدرس خطوة بخطوة
  1. A pin joined to nothing

    A button joins pin 2 to ground (GND, 0 volts). With the button open, the pin is connected to nothing at all. We call it floating, and digitalRead returns 0 or 1 at random.

    board     Arduino Uno R3 (ATmega328P, 5 V)
    wiring    pin 2 -> button -> GND
    line 1    pinMode(2, INPUT);
    open      pin joined to nothing: floating
    read      0, 1, 1, 0, ... at random
  2. Why a floating pin drifts

    An input pin draws almost no current: at most 1 µA, a millionth of an amp. Engineers call this high impedance. So stray charge from a nearby wire or your finger is enough to swing its voltage up and down.

    leakage   at most 1 µA into the pin
    nothing holds the voltage: it drifts
    at or below 1.5 V   reads LOW (0)
    at or above 3 V   reads HIGH (1)
    in between   not guaranteed either way
  3. Add a pull-up resistor

    Add a 10 kΩ resistor from the pin to 5 V: a pull-up. With the button open almost no current flows, so the resistor drops almost no voltage. The pin sits at 5 V and reads HIGH, 1.

    pull-up   10 kΩ from pin 2 to 5 V
    open      current: only the pin's leak
    drop      1 µA x 10 kΩ = 0.01 V at most
    pin       about 5 V  ->  HIGH (1)
  4. Press: the pin goes LOW

    Press the button. The pin is now joined straight to ground, so it sits at 0 V and reads LOW, 0. Current flows from 5 V through the resistor and button to ground: 5 V ÷ 10 kΩ = 0.5 mA.

    pressed   pin joined to GND: 0 V
    line 3    int v = digitalRead(2);  ->  v = LOW (0)
    current   5 V / 10 kΩ = 0.5 mA
    the resistor takes all 5 V
  5. Pressed reads LOW

    With a pull-up the logic is upside down: open reads HIGH, pressed reads LOW. Engineers call this negative logic. So the code checks for LOW: line 4 lights the LED on pin 13 while v is LOW.

    open      v = HIGH (1)   LED off
    pressed   v = LOW  (0)   LED on
    line 4    digitalWrite(13, v == LOW);
    negative logic: "on" is the LOW level
  6. Why not a plain wire?

    Why not tie the pin to 5 V with a plain wire? Then pressing the button joins 5 V straight to ground: a short circuit, with a huge current. With a 10 kΩ resistor instead, a press draws a safe 0.5 mA.

    plain wire + press   5 V straight to GND
                         a short circuit
    10 kΩ + press       0.5 mA, safe
    open: both hold the pin HIGH
  7. Choosing the value

    Too small wastes current: 1 kΩ draws 5 mA on every press. Too big is weak: with 1 MΩ, the pin's own 1 µA leak could drop 1 V, and noise gets in. 10 kΩ is the usual choice.

    1 kΩ    pressed 5 mA     open drop up to 0.001 V
    10 kΩ   pressed 0.5 mA   open drop up to 0.01 V
    1 MΩ    pressed 5 µA     open drop up to 1 V
    usual     10 kΩ for a button
  8. The pull-up inside the chip

    The ATmega328P has a pull-up built into every pin. pinMode(2, INPUT_PULLUP) switches it on, so the outside resistor can go. It is weaker, 20 to 50 kΩ, so a press draws only 0.1 to 0.25 mA.

    line 2    pinMode(2, INPUT_PULLUP);
    inside    20-50 kΩ from pin 2 to 5 V
    pressed   5 V / 50 kΩ = 0.1 mA  ...  5 V / 20 kΩ = 0.25 mA
    INPUT (line 1) switches it off again
    
    // Arduino Uno R3: a push button from pin 2 to GND
    void setup() {
      pinMode(2, INPUT_PULLUP);   // 20-50 kΩ to 5 V, inside
      pinMode(13, OUTPUT);        // the on-board LED
    }
    
    void loop() {
      int v = digitalRead(2);     // open: HIGH, pressed: LOW
      digitalWrite(13, v == LOW); // LED on while pressed
    }
  9. Pull-down: the mirror image

    Swap them: button to 5 V, resistor to ground. Now the resistor pulls the pin down to 0 V, and a press reads HIGH. The ATmega328P has no pull-downs inside, but an ESP32 does: INPUT_PULLDOWN.

    pull-down 10 kΩ from pin 2 to GND
    open      0 V   ->  LOW (0)
    pressed   5 V  ->  HIGH (1), 0.5 mA flows
    positive logic: "on" is the HIGH level
    ESP32: pinMode(pin, INPUT_PULLDOWN);

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