ENع
→Pin current limits
الدرس 08 · Digital pins: inputs and outputs

Pin current limits and driving loads with a transistor

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

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

A microcontroller pin can only give a small current. On an Arduino Uno R3 the ATmega328P datasheet allows at most 40 mA per pin and 200 mA through its VCC and GND pins, and only tests outputs to 20 mA. A red LED with a 330 Ω resistor takes about 9.4 mA and is fine. A 12 V, 3 W LED strip needs 250 mA at 12 V, so the pin hands the job to an IRLZ44N MOSFET. The lesson “Digital output: driving an LED” covers the LED circuit itself, the Hardware lesson “Ohm's law” covers I = V / R, and the Hardware lesson “Transistors: the switch that changed everything” explains how a MOSFET works inside.

ستتعلّم

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

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

A pin is a tiny switch

Inside the chip, each output pin is a pair of tiny transistors. HIGH connects the pin to 5 V, LOW connects it to 0 V. Any current a part draws from the pin flows through them.

output pin   two tiny transistors inside the chip
  HIGH       pin -> 5 V   (it sources current, out of the pin)
  LOW        pin -> 0 V   (it sinks current, into the pin)
whatever you connect draws its current through them
جارٍ تحميل الرسم…
أو ← →
اقرأ الدرس خطوة بخطوة
  1. A pin is a tiny switch

    Inside the chip, each output pin is a pair of tiny transistors. HIGH connects the pin to 5 V, LOW connects it to 0 V. Any current a part draws from the pin flows through them.

    output pin   two tiny transistors inside the chip
      HIGH       pin -> 5 V   (it sources current, out of the pin)
      LOW        pin -> 0 V   (it sinks current, into the pin)
    whatever you connect draws its current through them
  2. An LED: about 9 mA

    digitalWrite(8, HIGH) puts 5 V on pin 8. The red LED uses about 1.9 V, leaving 3.1 V across the 330 Ω resistor. Ohm's law: 3.1 ÷ 330 ≈ 9.4 mA. The gauge shows that current leaving the pin.

    line 1     digitalWrite(8, HIGH);
    pin 8      5 V
    LED        about 1.9 V  (red, at 10 mA)
    resistor   5 - 1.9 = 3.1 V
    current    I = V / R = 3.1 / 330 = 9.4 mA
    limit      40 mA   ->  fine
  3. The datasheet's limits

    The ATmega328P datasheet sets an absolute maximum of 40 mA per pin and 200 mA through its power pins. Past that the chip can be damaged. Outputs are only tested up to 20 mA, so plan to stay below that.

    absolute maximum   40 mA per I/O pin
                       200 mA through the VCC and GND pins
                       beyond: permanent damage possible
    tested             20 mA at 5 V: the pin still gives at least 4.2 V
    our LED            9.4 mA   ->  comfortable
  4. A load that wants 250 mA

    Now a 12 V LED strip rated 3 W. It needs 3 ÷ 12 = 0.25 A, or 250 mA. That is over six times the 40 mA limit, and the pin gives only 5 V. Wired directly, it could damage the pin.

    strip      12 V, 3 W
    current    I = P / V = 3 / 12 = 0.25 A = 250 mA
    vs limit   250 / 40 = 6.25 times too much
    voltage    the pin gives 5 V, the strip needs 12 V
    so never wire it straight to a pin
  5. Let a transistor switch it

    The fix: an N-channel MOSFET, a transistor used as a switch. Ours is an IRLZ44N. A voltage on its gate (G) opens a path from drain (D) to source (S). The Hardware lesson on transistors shows how.

    MOSFET     IRLZ44N, N-channel, "logic level"
      G gate     the control: a voltage here opens the switch
      D drain    where the load current comes in
      S source   where it leaves, to ground
    rated      up to 55 V across D-S, gate up to 16 V
  6. Two separate loops

    The strip sits between the 12 V supply and the drain; the source goes to ground. Its 250 mA loops supply, strip, transistor, ground, and never passes through the pin. All grounds are joined, so both sides agree on 0 V.

    big loop     +12 V -> strip -> D -> S -> ground -> supply
    small loop   pin 9 -> 220 Ω -> gate
    shared       ground: the board, the MOSFET and the supply
    the pin only drives the gate
  7. Pin 9 HIGH charges the gate

    digitalWrite(9, HIGH) puts 5 V on the gate through a 220 Ω resistor. For a few microseconds current flows to charge the gate, at most 5 ÷ 220 ≈ 23 mA. Then it falls to under 0.0001 mA.

    line 2     digitalWrite(9, HIGH);
    peak       at most 5 / 220 = 22.7 mA, for microseconds
               (the gate holds at most 48 nC: at least 2.1 µs)
    steady     gate leakage under 100 nA = 0.0001 mA
    the figure slows the spike down about a million times
  8. 250 mA flows, the strip lights

    5 V is well above the gate's 1 to 2 V threshold, so the channel opens, to at most 0.025 Ω. 250 mA flows and the strip lights. The transistor drops only about 6 mV and turns under 2 mW into heat.

    gate       5 V   (threshold 1 to 2 V)
    channel    at most 0.025 Ω  (at VGS = 5 V)
    current    250 mA through the strip and D-S
    drop       0.25 A x 0.025 Ω = 6.25 mV
    heat       I² x R = 1.5625 mW
    pin 9      still under 0.0001 mA
  9. Pin 9 LOW: strip off

    digitalWrite(9, LOW) drains the gate back to 0 V through the resistor. The channel closes, the 250 mA stops and the strip goes dark. The pin switched a 3 W load while giving almost no steady current.

    line 3     digitalWrite(9, LOW);
    gate       5 V -> 0 V  (a short spike back into the pin)
    channel    closed
    strip      250 mA -> 0 mA, dark
    
    // Arduino Uno R3: LED + 330 ohm on pin 8,
    // IRLZ44N gate (220 ohm) on pin 9, 12 V strip on its drain
    const int LED_PIN = 8;
    const int STRIP_PIN = 9;
    
    void setup() {
      pinMode(LED_PIN, OUTPUT);
      pinMode(STRIP_PIN, OUTPUT);
      digitalWrite(LED_PIN, HIGH);    // about 9 mA: fine for a pin
    }
    
    void loop() {
      digitalWrite(STRIP_PIN, HIGH);  // gate to 5 V: strip on
      delay(1000);
      digitalWrite(STRIP_PIN, LOW);   // gate to 0 V: strip off
      delay(1000);
    }

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