ENع
→Blink
الدرس 05 · Getting started with a microcontroller

Blink: your first firmware

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

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

Blink is the first program almost everyone runs on a new board: an LED that turns on for a second, then off for a second, forever. Follow the nine-line Arduino sketch on an Arduino Uno R3, from pressing Upload to the pin changing voltage. See how pinMode() and digitalWrite() set two bits inside the chip, DDRB5 and PORTB5, and how delay() waits a set time measured from the 16 MHz clock. Then change one number and make it blink faster. The lesson “setup() and loop(): the superloop” explains why loop() repeats; the C++ basics lessons cover the language.

ستتعلّم

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

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

Hello, world for chips

Blink is the first firmware most people write: an LED goes on for a second, off for a second, forever. If it blinks, your board, cable and tools all work. Ours is an Arduino Uno R3 with an LED on pin 13.

Blink  the "hello, world" of microcontrollers
board  Arduino Uno R3  (ATmega328P, 16 MHz)
LED    on pin 13, through a 220 ohm resistor
       (the board's own "L" LED is on pin 13 too)
it blinks  ->  board, cable and toolchain all work
جارٍ تحميل الرسم…
أو ← →
اقرأ الدرس خطوة بخطوة
  1. Hello, world for chips

    Blink is the first firmware most people write: an LED goes on for a second, off for a second, forever. If it blinks, your board, cable and tools all work. Ours is an Arduino Uno R3 with an LED on pin 13.

    Blink  the "hello, world" of microcontrollers
    board  Arduino Uno R3  (ATmega328P, 16 MHz)
    LED    on pin 13, through a 220 ohm resistor
           (the board's own "L" LED is on pin 13 too)
    it blinks  ->  board, cable and toolchain all work
  2. Nine lines of C++

    Here is the whole program, called a sketch, in Arduino C++. setup() runs once when the board starts. Then loop() runs again and again, as the lesson on setup() and loop() showed.

    sketch  an Arduino program
    setup() once at power on
    loop()  again and again, forever
    
    // Arduino Uno R3: LED + 220 ohm resistor on pin 13 (LED_BUILTIN)
    void setup() {
      pinMode(13, OUTPUT);
    }
    void loop() {
      digitalWrite(13, HIGH);
      delay(1000);
      digitalWrite(13, LOW);
      delay(1000);
    }
  3. Upload it to flash

    Press Upload. The IDE compiles the sketch into machine code and sends it down the USB cable. The bootloader, a small program already on the chip, writes it into flash. The lesson "From code to chip: compile, link and flash" shows each stage.

    Upload   compile -> machine code -> USB -> board
    USB      the ATmega16U2 on the board turns USB into serial
    loader   the bootloader (optiboot) writes it into flash
    flash    keeps the program with the power off
    reset    the chip restarts and runs setup()
    more     lesson: From code to chip: compile, link and flash
  4. Make pin 13 an output

    Line 2: pinMode(13, OUTPUT). After a reset every pin is an input. Inside the chip, pin 13 is bit 5 of port B. Writing 1 to its direction bit, DDRB5, makes it an output the chip can drive.

    line 2   pinMode(13, OUTPUT);
    pin 13   = port B, bit 5  (PB5)
    DDRB5    0 -> 1     1 = output, 0 = input
    at reset every pin starts as an input
  5. HIGH: the LED lights

    Line 5: digitalWrite(13, HIGH) sets the output bit PORTB5 to 1, and pin 13 goes to 5 volts. Current flows out of the pin, through the resistor and the LED, to ground. The LED lights.

    line 5   digitalWrite(13, HIGH);
    PORTB5   0 -> 1
    pin 13   5 V
    current  pin 13 -> 220 ohm -> LED -> ground
    LED      on
  6. Wait one second, lit

    Line 6: delay(1000) waits 1,000 milliseconds, one second. At 16 MHz, one second is 16 million clock ticks. The program does nothing else, and pin 13 stays at 5 volts, so the LED stays on.

    line 6   delay(1000);
    1,000 ms = 1 s
    16 MHz x 1 s = 16,000,000 ticks
    pin 13 stays 5 V: LED on
  7. LOW: the LED goes dark

    Line 7: digitalWrite(13, LOW) sets PORTB5 back to 0. Pin 13 drops to 0 volts, the same as ground, so no current flows and the LED goes dark.

    line 7   digitalWrite(13, LOW);
    PORTB5   1 -> 0
    pin 13   0 V, same as ground
    current  none
    LED      off
  8. Wait one second, dark

    Line 8: delay(1000) waits one more second with the LED off. Then loop() reaches its closing brace on line 9.

    line 8   delay(1000);
    another 16,000,000 ticks
    pin 13 stays 0 V: LED off
    line 9   }  end of loop()
  9. And again, forever

    loop() ends and starts again at line 5. One blink takes 2 seconds: 1 on, 1 off. That is 30 blinks a minute, 0.5 per second (0.5 hertz). The plot shows pin 13 over 4 seconds.

    line 9 -> line 5, again and again
    period   1000 ms on + 1000 ms off = 2 s
    rate     1 / 2 s = 0.5 Hz = 30 blinks a minute
    plot     pin 13: 5 V, 0 V, 5 V, 0 V ...
  10. Change it: blink faster

    Change both delays to 200 and upload again. Now one blink takes 0.4 seconds: 2.5 blinks a second. Edit, upload, watch the board: that is how all firmware work goes.

    delay(200);   on lines 6 and 8
    period   200 + 200 ms = 0.4 s
    rate     1 / 0.4 s = 2.5 Hz
    edit -> upload -> watch, again and again

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