กลับหน้าหลัก
views
Arduino Serial Communication Basics with Serial Port
Last updated on

Arduino Serial Communication Basics with Serial Port


Arduino Serial Communication Basics with Serial Port

Serial communication is one of the most practical tools in an engineer’s toolkit. It lets you send data from Arduino to your computer and vice versa over a USB cable, which is essential for debugging and interactive control. This guide walks through every Serial command you’ll use regularly.

Setting Baudrate: The Communication Speed

Before two devices can talk, they must agree on a speed. This is called the Baudrate and is set once at the start of your program. The Arduino board and your computer’s Serial Monitor must use the same value, or the output will be unreadable.

Common Baudrate values:

BaudrateTypical use
9600Default for most beginner projects
57600Moderate-speed data transfer
115200Higher-speed communication

Arduino supports 110, 300, 1200, 2400, 4800, 14400, 38400, 230400, 460800, and 921600 as well.

![Diagram showing USB cable connecting Arduino board to computer with TX/RX signals labeled](image: diagram showing USB connection between Arduino board and computer with TX/RX labels)

Core Serial Commands on Arduino

Serial.begin(speed)

Opens the serial port and sets the communication speed. Place this once inside setup() at the beginning of your program.

void setup() {
  Serial.begin(9600); // Set Baudrate to 9600
}

Serial.print() and Serial.println()

Send data to the Serial Monitor. The println variant appends a newline character, making each output appear on its own line.

Serial.print("Welcome to ");
Serial.println("My Arduino");

Serial.available() and Serial.read()

Check whether incoming data is waiting in the buffer. If available() returns greater than zero, use read() to grab one byte. These two commands work together in most input-handling sketches.

if (Serial.available() > 0) {
  char key = Serial.read();
}

Serial.flush()

Waits until all outgoing data in the transmit buffer has been sent. Useful when you need to ensure a complete message is delivered before your program continues.

Example 1: Arduino Sends Data to the Computer

This sketch repeatedly sends “My Arduino” to the Serial Monitor every second.

void setup() {
  Serial.begin(9600);        // Set Baudrate to 9600
  Serial.print("Welcome to ");
  Serial.println("My Arduino");
}

void loop() {
  Serial.println("My Arduino");
  delay(1000);               // Wait 1 second
}

How to view the output: Open Arduino IDE → Tools → Serial Monitor and make sure the Baudrate dropdown is set to 9600.

![Serial Monitor window showing repeated “My Arduino” output](image: Serial Monitor window displaying “My Arduino” repeated every second)

Example 2: Computer Sends Commands to Control LEDs

Connect 4 LEDs with 220–330Ω resistors to pins 2, 3, 4, and 5. Then type the numbers 0–4 in the Serial Monitor to select which LED turns on.

const int led1 = 2;
const int led2 = 3;
const int led3 = 4;
const int led4 = 5;
char key = '0';

void setup() {
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);

  // Turn all LEDs off initially
  digitalWrite(led1, LOW);
  digitalWrite(led2, LOW);
  digitalWrite(led3, LOW);
  digitalWrite(led4, LOW);

  Serial.begin(9600);
  Serial.print("Welcome to ");
  Serial.println("My Arduino");
}

void loop() {
  // Receive command from computer
  if (Serial.available() > 0) {
    key = Serial.read();
    Serial.print("key : ");
    Serial.println(key);
  }

  // Turn on the selected LED based on the command
  if (key == '0') {
    digitalWrite(led1, LOW);
    digitalWrite(led2, LOW);
    digitalWrite(led3, LOW);
    digitalWrite(led4, LOW);
  }
  if (key == '1') {
    digitalWrite(led1, HIGH);
    digitalWrite(led2, LOW);
    digitalWrite(led3, LOW);
    digitalWrite(led4, LOW);
  }
  if (key == '2') {
    digitalWrite(led1, LOW);
    digitalWrite(led2, HIGH);
    digitalWrite(led3, LOW);
    digitalWrite(led4, LOW);
  }
  if (key == '3') {
    digitalWrite(led1, LOW);
    digitalWrite(led2, LOW);
    digitalWrite(led3, HIGH);
    digitalWrite(led4, LOW);
  }
  if (key == '4') {
    digitalWrite(led1, LOW);
    digitalWrite(led2, LOW);
    digitalWrite(led3, LOW);
    digitalWrite(led4, HIGH);
  }
}

Testing steps:

  1. Upload the sketch to your Arduino board
  2. Open the Serial Monitor (set Baudrate to 9600)
  3. Type the numbers 1–4 and press Send to light up each LED in sequence
  4. Type 0 to turn all LEDs off

![Breadboard circuit showing 4 LEDs wired to Arduino pins 2-5 with resistors](image: circuit diagram showing 4 LEDs connected to Arduino pins 2-5 with resistors)

Quick Reference: Serial Commands

CommandPurpose
Serial.begin(speed)Open serial port, set Baudrate
Serial.available()Check if incoming data is waiting
Serial.read()Read 1 byte from the receive buffer
Serial.print(data)Send data on the same line
Serial.println(data)Send data then move to a new line
Serial.flush()Wait for transmit buffer to empty

Reference Videos

อยากทำโปรเจคแบบนี้?

รับทำโปรเจค Arduino / IoT จบงานไว ส่งงานครบ พร้อมสอน

If you need Arduino project service or urgent IoT development, see full service details on the home page

จ้างทำโปรเจคเลย

Project estimate

Want something like this? Open the estimate page.

The long form is now on a separate estimate page, so this article stays clean.

ความคิดเห็น

รีวิวจากคนใช้งานจริง

รีวิวจากลูกค้าและคนที่เคยใช้งาน

ถ้าเคยสั่งงาน เคยอ่านหน้านี้แล้วได้ประโยชน์ หรือมีข้อเสนอแนะ ฝากรีวิวไว้ได้เลย

กำลังโหลดรีวิว...