Serial Communication Cheatsheet
Serial Communication Cheatsheet
Serial.begin, print, read ߀” talk to your computer from the board.
Setup & Output
| Serial.begin(9600) | Initialise serial at 9600 baud ߀” call in setup(), match baud in Serial Monitor |
| Serial.print(value) | Send value as human-readable text ߀” no newline appended |
| Serial.println(value) | Send value + newline ߀” each call appears on its own line in the monitor |
| Serial.print(x, HEX) | Print integer in hexadecimal (also BIN for binary, OCT for octal) |
| Serial.write(byte) | Send a raw byte (not text representation) ߀” use for device protocols |
Input
| Serial.available() | Number of bytes waiting in the receive buffer ߀” always check before reading |
| Serial.read() | Read and remove one byte from the buffer ߀” returns -1 if empty |
| Serial.readStringUntil('\n') | Read a full line typed in the Serial Monitor (terminated by Enter) |
| Serial.parseInt() | Parse the next integer from the buffer, skipping non-numeric characters |
| Serial.peek() | Look at the next byte without removing it from the buffer |
Useful Tips
| Serial.flush() | Wait until all outgoing serial data has been transmitted |
| if (!Serial) { } | On boards with native USB (Leonardo, Micro) ߀” wait for Serial Monitor to open |
| Pins 0 (RX) and 1 (TX) | Shared with USB ߀” avoid connecting hardware here while using Serial Monitor |
| Serial.setTimeout(ms) | Set timeout for readString/parseInt functions (default 1000 ms) |