Serial Communication on Arduino
Serial Communication on Arduino
Print debug output, read user input, and talk to your computer.
What is Serial Communication?
Serial communication sends data one bit at a time over a single wire. The Arduino Uno has one hardware serial port on pins 0 (RX) and 1 (TX), also connected to the USB chip ߀” so you can send and receive messages between the board and your computer through the USB cable.
This is the primary way to:
- Debug your sketch (print values to the Serial Monitor)
- Receive commands from a PC or another device
- Talk to other devices like GPS modules, Bluetooth adapters, and displays
Setting Up Serial ߀” Serial.begin()
Call this in setup() with the baud rate (bits per second). Always use a rate that matches your Serial Monitor setting.
void setup() {
Serial.begin(9600); // 9600 baud is the most common for beginners
}
Common baud rates: 9600, 19200, 38400, 57600, 115200.
Sending Data ߀” Print Functions
| Function | Behaviour |
|---|---|
Serial.print(x) |
Print value, no newline |
Serial.println(x) |
Print value + newline |
Serial.print(x, HEX) |
Print integer in hexadecimal |
Serial.print(x, BIN) |
Print integer in binary |
void loop() {
int sensor = analogRead(A0);
Serial.print("Raw value: ");
Serial.println(sensor); // e.g., "Raw value: 512"
delay(500);
}
Opening the Serial Monitor
In the Arduino IDE 2, click the magnifying glass icon in the top-right toolbar, or press Ctrl + Shift + M. Set the baud rate at the bottom of the monitor to match Serial.begin().
ß? ?? Pins 0 and 1 are shared with USB serial. Avoid connecting anything to these pins while uploading or monitoring.
Reading Data ߀” Serial.read() and Serial.available()
if (Serial.available() > 0) { // Data waiting in the buffer?
char c = Serial.read(); // Read one byte
Serial.print("Got: ");
Serial.println(c);
}
Serial.available() returns the number of bytes waiting to be read. Always check it before calling Serial.read().
Reading a Full Line
void loop() {
if (Serial.available() > 0) {
String line = Serial.readStringUntil('\n'); // Read until Enter key
line.trim(); // Remove \r\n
Serial.print("You typed: ");
Serial.println(line);
if (line == "on") {
digitalWrite(LED_BUILTIN, HIGH);
} else if (line == "off") {
digitalWrite(LED_BUILTIN, LOW);
}
}
}
Serial.write() vs Serial.print()
| Function | Sends |
|---|---|
Serial.print(65) |
The characters 6 and 5 (the text "65") |
Serial.write(65) |
The byte 0x41 (which is the ASCII character 'A') |
Use write() when talking to another device expecting raw bytes; use print() for human-readable output.
Timing and Non-Blocking Code
delay() pauses everything, including serial reading. For responsive serial input without blocking, track time manually:
unsigned long previousMillis = 0;
const long interval = 1000;
void loop() {
unsigned long now = millis();
if (now - previousMillis >= interval) {
previousMillis = now;
Serial.println(analogRead(A0)); // Print every 1 s without blocking
}
// Other code here runs freely without being delayed
}