The problem
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.
How a print travels
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.
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.
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.
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.
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.
Finding the bug
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.
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.
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.
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.
What happens
;; step 1/10 · bug
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);
}
The card shows the lines that matter. Line 1 runs once in setup(), next to pinMode(13, OUTPUT); lines 2 to 6 are the body of loop(), which ends with delay(500). The full sketch is in the step notes. Serial.begin(9600) uses the default format, SERIAL_8N1: 8 data bits, no parity bit, 1 stop bit, so every byte costs 10 bits on the wire. When nothing is being sent the TX line rests at 1, and the start bit's drop to 0 tells the receiver a byte is coming.