Skip to content

Arduino - Communication

In the world of microcontrollers, communication is essential. Your Arduino needs to talk to your computer, sensors, and other chips. While there are many communication protocols, the most fundamental one is serial communication. This chapter focuses on UART, the protocol used by Arduino’s built-in Serial object.

Parallel communication sends multiple bits of data at once over multiple wires. It’s very fast but requires many I/O pins, which are a precious resource on a microcontroller. Serial communication sends data one bit at a time over a single wire (plus a common ground). It’s slower but far more efficient in its use of pins, making it the dominant method for most microcontroller tasks.

Arduino’s Serial communication is asynchronous, meaning it doesn’t use a shared clock signal. To ensure the sender and receiver are synchronized, they must agree on a set of rules beforehand:

  • Baud Rate: The speed of communication, measured in bits per second (bps). Both devices must be set to the same baud rate. A common value is 9600.
  • Data Frame: Each chunk of data is sent in a packet, or frame, which consists of:
    • Start Bit: One bit that signals the start of a new frame (a transition from high to low).
    • Data Bits: The actual data, typically 8 bits (one byte).
    • Parity Bit (Optional): An extra bit for basic error checking.
    • Stop Bit(s): One or two bits that signal the end of the frame (a transition back to high).

On an Arduino UNO, the Serial object refers to the built-in hardware UART connected to digital pins 0 (RX - Receive) and 1 (TX - Transmit). These pins are also connected to the USB-to-serial chip, allowing your Arduino to talk to your computer’s Serial Monitor.

Best Practice: Avoid using pins 0 and 1 for general I/O (like LEDs or buttons) if you are using Serial for debugging, as it can interfere with code uploads and communication.

  • Serial.begin(speed): Initializes serial communication and sets the baud rate (e.g., Serial.begin(9600)).
  • Serial.available(): Returns the number of bytes (characters) available for reading from the serial buffer.
  • Serial.read(): Reads one incoming byte of serial data.
  • Serial.print(data): Prints data to the serial port in a human-readable ASCII text format.
  • Serial.println(data): Same as print(), but adds a carriage return and newline character at the end.
  • Serial.write(data): Writes raw binary data (a byte or series of bytes) to the serial port.

Code Example: A Simple Command Interpreter

Section titled “Code Example: A Simple Command Interpreter”

This advanced example demonstrates reading a string from the Serial Monitor and having the Arduino perform an action based on the command.

/*
Serial Command Interpreter
Listens for 'ON' or 'OFF' commands from the Serial Monitor
to control the built-in LED.
*/
const int LED_PIN = LED_BUILTIN; // Use the built-in LED
void setup() {
// Start serial communication at 9600 bps
Serial.begin(9600);
pinMode(LED_PIN, OUTPUT);
Serial.println("Arduino is ready.");
Serial.println("Send 'ON' to turn the LED on, 'OFF' to turn it off.");
}
void loop() {
// Check if there's any data available to read
if (Serial.available() > 0) {
// Read the incoming command as a String until a newline is received
String command = Serial.readStringUntil('\n');
// Remove any leading/trailing whitespace
command.trim();
if (command.equalsIgnoreCase("ON")) {
digitalWrite(LED_PIN, HIGH);
Serial.println("Command received: ON. LED is now on.");
} else if (command.equalsIgnoreCase("OFF")) {
digitalWrite(LED_PIN, LOW);
Serial.println("Command received: OFF. LED is now off.");
} else {
Serial.print("Unknown command: ");
Serial.println(command);
}
}
}

While UART is great for computer-to-Arduino or simple point-to-point communication, Arduino also supports other serial protocols for more complex tasks:

  • I2C (Inter-Integrated Circuit): A two-wire protocol (SDA and SCL) that allows you to connect multiple devices (sensors, displays, etc.) to the same bus. Each device has a unique address.
  • SPI (Serial Peripheral Interface): A faster, four-wire protocol often used for high-bandwidth devices like SD card readers, Ethernet modules, and some display types.