ENع
→Digital output
الدرس 07 · Digital pins: inputs and outputs

Digital output: driving an LED

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

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

A digital output pin is a switch inside the microcontroller: it joins the pin to 5 V (HIGH) or to 0 V (LOW). Follow pin 8 of an Arduino Uno R3 as pinMode sets its direction bit, digitalWrite sets its output bit, and current flows through a 330 Ω resistor and a red LED. Then use Ohm's law to see why the LED gets 9.4 mA and why the resistor is never optional. The Hardware lessons “Ohm's law”, “Diodes: one-way streets” and “Transistors: the switch that changed everything” explain the parts; here we drive them from firmware.

ستتعلّم

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

شكل 7 · تفاعلي
01 / 07

A pin is a switch

Pin 8 on the Uno's ATmega328P chip can work as an output. Inside, it acts like a switch that joins the pin to 5 volts or to 0 volts, called ground. At power on it joins neither: every pin starts as an input.

board   Arduino Uno R3, ATmega328P, 5 V
pin 8   port B, bit 0  (PB0)
output  a switch: pin -> 5 V (HIGH) or pin -> 0 V (LOW)
reset   the switch joins neither: the pin is an input
جارٍ تحميل الرسم…
أو ← →
اقرأ الدرس خطوة بخطوة
  1. A pin is a switch

    Pin 8 on the Uno's ATmega328P chip can work as an output. Inside, it acts like a switch that joins the pin to 5 volts or to 0 volts, called ground. At power on it joins neither: every pin starts as an input.

    board   Arduino Uno R3, ATmega328P, 5 V
    pin 8   port B, bit 0  (PB0)
    output  a switch: pin -> 5 V (HIGH) or pin -> 0 V (LOW)
    reset   the switch joins neither: the pin is an input
  2. The LED circuit

    We wire pin 8 to a 330 Ω resistor, then a red LED, then ground. An LED, a light-emitting diode, glows only when current flows through it the right way: in at the long leg (anode), out at the short leg (cathode).

    pin 8 -> 330 Ω -> ammeter -> LED anode (long leg)
          LED cathode (short leg) -> GND
    LED   lights only for current from anode to cathode
    now   the pin is an input: no current, LED dark
  3. pinMode: make it an output

    pinMode(8, OUTPUT) writes a 1 into DDRB0, a bit in the direction register. A register is a tiny memory cell wired to hardware. The switch now works. PORTB0, the output bit, is still 0, so the pin is held at 0 V.

    line 2   pinMode(8, OUTPUT);
    DDRB0    0 -> 1   (1 = output)
    PORTB0   0        (its value after reset)
    switch   pin -> 0 V
  4. HIGH: 5 volts, light on

    digitalWrite(8, HIGH) writes 1 into PORTB0. The switch joins pin 8 to 5 V. With 5 V at one end of the circuit and 0 V at the other, current flows through the resistor and the LED into ground, and the LED glows.

    line 3   digitalWrite(8, HIGH);
    PORTB0   0 -> 1
    switch   pin -> 5 V
    current  pin -> 330 Ω -> LED -> GND
    LED      on
  5. How much current flows?

    The LED takes about 1.9 V, its forward voltage, leaving 5 − 1.9 = 3.1 V for the resistor. Ohm's law: I = 3.1 V ÷ 330 Ω ≈ 9.4 mA. The ammeter (a current meter) reads 9.4 mA, below the pin's 20 mA rating.

    LED       1.9 V  (forward voltage, WP7113ID, typ at 10 mA)
    resistor  5 - 1.9 = 3.1 V
    Ohm's law I = V / R = 3.1 / 330 = 9.39 mA
    rated     pin 20 mA, LED 30 mA max
  6. Why the resistor matters

    To choose R, divide the spare volts by the current wanted: 3.1 V ÷ 10 mA = 310 Ω, so use the next standard size, 330 Ω. With no resistor, only the driver limits the current, which can pass 40 mA and damage the pin.

    want     about 10 mA
    R        3.1 V / 10 mA = 310 Ω  ->  330 Ω (standard E12 value)
    pin      40 mA absolute maximum, 20 mA rated
    LED      30 mA absolute maximum
    no R     nothing but the driver limits the current
  7. LOW: 0 volts, light off

    delay(5000) waits five seconds. Then digitalWrite(8, LOW) writes 0 into PORTB0, and the switch joins pin 8 to 0 V. Both ends of the circuit are now at 0 V, so no current flows and the LED goes dark.

    line 4   delay(5000);   five seconds, LED on
    line 5   digitalWrite(8, LOW);
    PORTB0   1 -> 0
    switch   pin -> 0 V
    current  0 mA, LED off

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