Overview
The USB cable is also a serial link. Serial.print() sends text to the IDE's
Serial Monitor, and Serial.read() receives what you type. It is the main way to see inside a
running sketch.
You will need
- Arduino Uno R3 and USB cable
Wiring
| Uno pin | Connects to | Notes |
|---|---|---|
| 0, 1 | USB serial (internal) | Leave free while using Serial |
Common problems
- Garbled characters: the Monitor's baud rate (bottom corner) must match
Serial.begin(). - Every command "runs twice": the Monitor sends a newline after your text. This sketch ignores it; parsing code must too, or set line ending to "No line ending".
- Sketch restarts when you open the Monitor: normal on the Uno; opening the port resets the board.
- Upload fails while another program holds the port: close other serial tools first.
The Serial Plotter (Tools → Serial Plotter) graphs any line of numbers. Print several values separated by spaces or commas to plot them together.
The sketch
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(9600);
Serial.println("Type 1 for LED on, 0 for off, ? for status");
}
void loop() {
if (Serial.available() > 0) {
char command = Serial.read();
if (command == '1') {
digitalWrite(LED_BUILTIN, HIGH);
Serial.println("LED on");
} else if (command == '0') {
digitalWrite(LED_BUILTIN, LOW);
Serial.println("LED off");
} else if (command == '?') {
Serial.print("LED is ");
Serial.println(digitalRead(LED_BUILTIN) == HIGH ? "on" : "off");
Serial.print("Uptime (ms): ");
Serial.println(millis());
}
// Other characters, such as line endings, are ignored
}
}How the code works
Serial.println() adds a line break; Serial.print() does not. For fixed text,
Serial.println(F("text")) keeps the string in flash memory instead of the Uno's 2 KB of RAM.
What you should observe
- The Monitor shows the instruction line. Typing 1 and Send turns on the L LED and replies "LED on".
- Typing ? reports the LED state and uptime.