ENع
→Serial print
الدرس 06 · Getting started with a microcontroller

Debugging with serial print

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

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

A microcontroller has no screen, so when your firmware misbehaves you cannot see why. Serial print fixes that: the chip sends text over its UART, through the board's USB chip, to the Serial Monitor on your computer. Follow one Serial.println() on an Arduino Uno R3 from a number to characters, from characters to 10-bit frames on the TX pin, and onto your screen. Then use two prints to find a real bug: a voltage that reads 0.00 at almost every knob position because of whole-number division. The Hardware lesson “Serial buses: UART, I2C and SPI” explains the wires in depth; here we use them to debug. Whole-number division itself is C++ material, covered in the C++ lesson “Variables and types”.

ستتعلّم

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

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

A bug you cannot see

Our sketch should light the LED when the knob gives more than 2.0 volts. We turn it past half way, and nothing. The code compiles and runs, but the chip has no screen. We cannot see its variables.

board   Arduino Uno R3  (ATmega328P)
want    knob voltage > 2.0 V  ->  LED on pin 13 on
got     LED stays off unless the knob is turned all the way
problem the values live inside the chip, out of sight

// Arduino Uno R3: a knob (potentiometer) on A0, an LED on pin 13
void setup() {
  pinMode(13, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  int raw = analogRead(A0);
  float v = raw / 1023 * 5.0;   // the bug
  Serial.println(raw);
  Serial.println(v);
  digitalWrite(13, v > 2.0);
  delay(500);
}
جارٍ تحميل الرسم…
أو ← →
اقرأ الدرس خطوة بخطوة
  1. A bug you cannot see

    Our sketch should light the LED when the knob gives more than 2.0 volts. We turn it past half way, and nothing. The code compiles and runs, but the chip has no screen. We cannot see its variables.

    board   Arduino Uno R3  (ATmega328P)
    want    knob voltage > 2.0 V  ->  LED on pin 13 on
    got     LED stays off unless the knob is turned all the way
    problem the values live inside the chip, out of sight
    
    // Arduino Uno R3: a knob (potentiometer) on A0, an LED on pin 13
    void setup() {
      pinMode(13, OUTPUT);
      Serial.begin(9600);
    }
    
    void loop() {
      int raw = analogRead(A0);
      float v = raw / 1023 * 5.0;   // the bug
      Serial.println(raw);
      Serial.println(v);
      digitalWrite(13, v > 2.0);
      delay(500);
    }
  2. Open a serial port

    Serial.begin(9600) switches on the chip's UART, a part that sends bytes one bit at a time. 9600 is the baud rate: bits per second. They leave on pin 1 (TX), and the USB chip passes them to the Serial Monitor on your computer.

    line 1   Serial.begin(9600);    runs once, in setup()
    UART     universal asynchronous receiver-transmitter
    baud     9600 bits per second
    pins     0 = RX (in), 1 = TX (out)
    path     ATmega328P -> ATmega16U2 -> USB cable -> computer
  3. Read the knob: 512

    With the knob near half way, A0 sees about 2.50 volts. analogRead turns 0 to 5 volts into a whole number from 0 to 1023, so raw becomes 512. Is that right? Let us print it.

    line 2   int raw = analogRead(A0);
    knob     about 2.50 V on A0
    ADC      0..5 V  ->  0..1023   (10 bits)
    raw      512
  4. Number to characters

    Serial.println(raw) does not send the number 512 itself. It writes the characters '5', '1', '2', then CR and LF to end the line: 5 bytes. They wait in a 64-byte transmit buffer, and println returns at once.

    line 4   Serial.println(raw);
      5   0x35  (53)
      1   0x31  (49)
      2   0x32  (50)
      CR  0x0D  (13)
      LF  0x0A  (10)
    5 bytes into the 64-byte transmit buffer
    CR = carriage return, LF = line feed: "start a new line"
  5. Each byte becomes 10 bits

    The UART sends each byte as a frame: a start bit (0), the 8 data bits lowest first, and a stop bit (1). '5' is 0x35. At 9600 baud each bit lasts 104 µs, so one byte takes about 1.04 ms.

    byte     '5' = 0x35 = 00110101
    frame    0 1 0 1 0 1 1 0 0 1
             S 0 1 2 3 4 5 6 7 E
             S = start, 0..7 = d0..d7 (lowest bit first), E = end (stop bit)
    bit      1 / 9600 s = 104.2 µs
    byte     10 bits = 1.04 ms   (960 bytes per second)
  6. The monitor shows 512

    The USB chip hands the bytes to your computer, and the Serial Monitor shows them as text: 512. It must be set to 9600 baud too, or the bits are cut up wrongly and you see garbage. 512 looks right.

    received 0x35 0x31 0x32 0x0D 0x0A
    shown    512   (CR LF starts the next line)
    monitor  set to 9600 baud, the same as Serial.begin()
    verdict  raw = 512 is what a half-turned knob should give
  7. But v prints 0.00

    Next, Serial.println(v) prints 0.00 at every knob position except the very end. That is wrong: it should be about 2.50. raw was right and v is wrong, so the bug sits on the line between them: line 3.

    line 5   Serial.println(v);
    shown    0.00     (floats print with 2 decimals)
    expected 512 * 5 / 1023 = 2.50
    raw OK, v wrong  ->  look at line 3
  8. Whole-number division

    raw / 1023 divides two whole numbers, so C++ does whole-number division and throws the fraction away: 512 / 1023 is 0, not 0.5. Then 0 × 5.0 is 0. Every raw below 1023 gives 0.

    line 3   float v = raw / 1023 * 5.0;
    step 1   raw / 1023  =  512 / 1023  ->  0   (int / int, fraction dropped)
    step 2   0 * 5.0  =  0.0
    so       v = 0.0 for raw 0..1022, and 5.0 only at 1023
    LED      0 > 2.0 is false: off unless the knob is turned all the way
  9. Fixed: v prints 2.50

    Multiply first: raw * 5.0 is a float, so the division keeps its fraction. 512 × 5.0 / 1023 = 2.50. Upload again: the monitor shows 2.50, and 2.50 > 2.0, so the LED lights.

    line 3   float v = raw * 5.0 / 1023;
    step 1   512 * 5.0 = 2560.0   (a float)
    step 2   2560.0 / 1023 = 2.5024
    shown    2.50
    LED      2.50 > 2.0  ->  pin 13 HIGH, LED on
  10. Printing takes time

    Each loop sends 11 bytes: about 11.5 ms at 9600 baud. println returns at once while the 64-byte buffer has room; when it fills, println waits. Print less, use 115200 baud, and remove prints when done.

    per loop 5 + 6 = 11 bytes = 110 bits
    9600     11.5 ms on the wire
    115200   0.95 ms on the wire
    buffer   64 bytes: println returns at once while there is room
    full     println waits until bytes have gone out
    figure   shows only the 5 bytes of raw; v adds 6 more

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