Sub topic 6
Using the Arduino Serial API
This chapter assumes that you know the setup() and loop() functions, bytes, binary numbers, and digital HIGH and LOW levels. It also assumes that the UART wiring and baud rate have already been chosen correctly. The Arduino Serial object gives your sketch access to a UART port. You can use it to send debugging text, receive commands, or exchange bytes with another device.
Starting a serial port
Call Serial.begin() in setup(). Its first argument is the baud rate, or the number of symbols sent each second. Both devices must use the same baud rate and frame format.
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.println(F("Arduino is running"));
delay(1000);
}Open the Arduino Serial Monitor and select 9600 baud. The monitor must use the same line ending setting as your sketch expects. Serial.println() sends the text followed by a carriage return and newline. Serial.print() sends text without automatically adding a line ending.
The F() macro stores a constant string in program memory on boards where this saves RAM. It is useful for fixed messages in sketches with limited memory.
Serial.begin() can also specify the frame format:
Serial.begin(115200, SERIAL_8E1);This selects 115200 baud, eight data bits, even parity, and one stop bit. The usual default is SERIAL_8N1: eight data bits, no parity, and one stop bit. A connected device must use the same settings.
Sending data
The API provides several ways to transmit information.
| Call | Meaning |
|---|---|
| `Serial.print(value)` | Sends a value as readable text |
| `Serial.println(value)` | Sends readable text and a line ending |
| `Serial.write(byte)` | Sends one raw byte |
| `Serial.write(buffer, length)` | Sends a sequence of raw bytes |
| `Serial.availableForWrite()` | Reports transmit-buffer space |
print() converts numbers to characters. For example, Serial.print(25) sends the ASCII characters 2 and 5, not one binary byte with the value 25. Use write() when the receiver expects the original byte value.
void setup() {
Serial.begin(9600);
Serial.print(F("Text: "));
Serial.println(25);
Serial.print(F("Raw byte: "));
Serial.write((uint8_t)25);
Serial.println();
}
void loop() {
}The cast to uint8_t makes the intended byte type clear. The receiver must know whether a field is text or binary. Mixing the two without a defined format causes parsing errors.
Receiving data
Serial.available() returns the number of bytes currently waiting in the receive buffer. Serial.read() removes and returns one byte. Its return type is int, not char, because it can return -1 when no byte is available.
void setup() {
Serial.begin(9600);
}
void loop() {
while (Serial.available() > 0) {
int received = Serial.read();
Serial.write((uint8_t)received);
}
}This is an echo program. Every received byte is sent back. The while loop drains all bytes that are currently available, but it does not wait for a future byte. That makes the loop non-blocking: other work can run whenever the buffer is empty.
A blocking version would wait inside a loop until a byte arrived. That approach is simpler for a one-purpose sketch, but it can prevent timing-sensitive tasks, sensor updates, and safety checks from running.
Serial data flow
Choosing the right API call
Use text functions for human-readable logs and simple command interfaces. Use raw bytes for compact binary protocols or values that may contain any byte from 0 through 255. Define the format before writing the parser.
For example, a text command might look like LED ON\n. A binary command might contain a command byte followed by a brightness byte. The receiver must know how many fields to expect and how to identify the end of a message.
Checking transmit capacity
Arduino serial output normally uses a transmit buffer. A call to print() or write() can wait if the buffer does not have enough room. Large or frequent messages can therefore delay the rest of the sketch.
Serial.availableForWrite() lets a sketch check available space before sending a large block. This matters when output must not interrupt time-sensitive work. It does not guarantee that a complete message will be transmitted immediately.
Exercise
What is the difference between Serial.print(65) and Serial.write((uint8_t)65)?
Show answer
Serial.print(65) sends the text characters 6 and 5. Serial.write((uint8_t)65) sends one raw byte whose value is 65, which represents A in ASCII.