Pin current limits and driving loads with a transistor
لم يُترجم بعد؛ يُعرض باللغة الإنجليزية
عن هذا الدرس
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.
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
اقرأ الدرس خطوة بخطوة
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 themAn 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 -> fineThe 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 -> comfortableA 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 pinLet 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 VTwo 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 gatePin 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 times250 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 mAPin 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); }
أسئلة سريعة: 5
اختر إجابة واحدة لكل سؤال. يمكنك تغيير اختياراتك قبل التحقق منها.
01What is the absolute maximum DC current for one I/O pin of the ATmega328P?
The datasheet's absolute maximum ratings list 40 mA per I/O pin (and 200 mA through the VCC and GND pins). That is a damage limit, not a target: outputs are only tested to 20 mA.
02A 5 V pin drives a red LED (about 1.9 V) through 330 Ω. About how much current flows?
The resistor has 5 − 1.9 = 3.1 V across it, and 3.1 ÷ 330 ≈ 9.4 mA: comfortably below the limit.
03A 12 V LED strip is rated 3 W. Why can't a pin power it directly?
I = P ÷ V = 3 ÷ 12 = 0.25 A = 250 mA, over six times the 40 mA limit, and at a voltage the pin cannot give.
04In the MOSFET circuit, which path does the strip's 250 mA take?
The load current loops supply → strip → drain → source → ground. The pin only drives the gate, which is a separate small loop; the shared ground lets both agree on 0 V.
05After digitalWrite(9, HIGH) the gate is charged. How much current does pin 9 keep giving?
The gate is insulated. Charging it takes a short spike (at most 5 ÷ 220 ≈ 23 mA for a few microseconds); after that only leakage flows, at most 100 nA = 0.0001 mA.
المسرد
- Source and sink
- A pin sources current when it pushes current out (HIGH) and sinks current when current flows into it (LOW).
- Absolute maximum rating
- A datasheet limit that can damage the part if exceeded. It is not a safe working point.
- Load
- Whatever a circuit powers: an LED, a strip, a motor or a relay.
- MOSFET
- A transistor switched by the voltage on its gate. An N-channel MOSFET connects drain to source when its gate is high enough above the source.
- Gate threshold voltage
- The gate voltage at which a MOSFET just starts to conduct: 1 to 2 V for the IRLZ44N. To switch fully on, the gate must be well above it.
- Logic-level MOSFET
- A MOSFET whose datasheet promises low on-resistance with a 5 V (or lower) gate, so a microcontroller pin can switch it fully on.
- On-resistance (RDS(on))
- The small resistance between drain and source when the MOSFET is on: at most 0.025 Ω for the IRLZ44N at a 5 V gate.
- Common ground
- Joining the grounds of the microcontroller and the load's supply, so both use the same 0 V reference.
الملاحظات والمصادر
- The LED is a Kingbright WP7113ID-style 5 mm red LED: 1.9 V typical (2.3 V at most) at 10 mA. The pin's own voltage also sags a little under load (at least 4.2 V at 20 mA), so the real current is slightly under 9.4 mA.
- Absolute maximum ratings are damage limits, not working points. The datasheet also caps groups of pins: the current out of one port group should stay under 150 mA and into one group under 100 mA. Arduino's own guide suggests a 470 Ω or 1 kΩ series resistor on outputs.
- The gate is like a tiny capacitor. The IRLZ44N needs at most 48 nC to charge, so at 22.7 mA or less it takes at least 2.1 µs; in practice a few microseconds, because the current shrinks as the gate fills. The steady gate leakage is at most 100 nA.
- A "logic-level" MOSFET is needed: its datasheet gives the on-resistance at a 5 V gate (0.025 Ω at most at 25 °C). Many standard MOSFETs are only specified at a 10 V gate. A 10 kΩ resistor from gate to ground is often added so the strip stays off while the pin is still an input after reset.
- The strip is an example load, 12 V and 3 W, treated as steady. Motors, relays and solenoids are coils: they also need a flyback diode, which the lesson “Driving a servo and a one-way DC motor with a MOSFET” covers.
- Microchip ATmega48A/PA/88A/PA/168A/PA/328/P datasheet (DS40002061B): Absolute Maximum Ratings and DC Characteristics
- Arduino Docs: Digital Pins
- Arduino Docs source: digital-pins.md (arduino/docs-content)
- Infineon (International Rectifier) IRLZ44NPbF datasheet
- Kingbright WP7113ID red LED datasheet
- J. W. Valvano, Embedded Systems: Shape the World, ch. 8 Switches and LEDs (UT Austin)
اختبار يتاح بعد الخطوة الأخيرة.