chapter 5

Serial communication

Serial communication is a fundamental aspect of microcontroller programming, and it plays a crucial role in enabling devices to exchange information. In the world of Arduino, serial communication is a powerful tool that allows you to send and receive data between your Arduino board and other devices, such as computers, sensors, displays, and more. In this article, How Does Serial Communication Works in Arduino, I will be covering everything from the basics to advanced techniques.

serial communication, data is sent sequentially, usually as a stream of binary digits (0s and 1s). This method allows for reliable and efficient data transmission over long distances and between different devices. Serial communication can be either synchronous (data is sent at a constant rate, synchronized by a clock signal) or asynchronous (data is sent without a clock signal and relies on agreed-upon timing parameters).

Serial communication is a commonly used method with which to exchange data between computers and peripheral devices. Serial transmission between the sender and receiver are subject to strict protocols which provide security and reliability and have led to its longevity. Many devices from personal computers to mobile devices make use of serial communication. Let’s take a closer look at its fundamentals.

Serial communication makes use of a serial digital binary method of exchanging data. It employs a number of interfaces and serial communication protocols including RS232,RS485, SPI, and I2C among others.

How are Serial Communication work

we must first understand the fundamental concept of a bit, the smallest unit of data in an electronic circuit. A bit, in its simplest terms, is like a single piece of digital information. You can think of it like a light switch that can only be in one of two positions: 'on' or 'off'. In the language of computers, instead of 'on' or 'off', we say that a bit can be either '1' or '0'.

In serial communication, data transmission occurs bit by bit on a single communication line or channel. This process means that data bits are sent one after the other in a sequence or series (hence the term 'serial'), with the receiving device collecting and reassembling these bits into a complete message.

In simple terms, serial data transmission is like a single-lane road where cars can only travel one after the other, not side by side. Here is a simple breakdown of how serial data is transmitted:

1. Start of communication: The device sending the data, called the transmitter, sends a start bit to the device receiving the data, known as the receiver. The start bit is like a heads-up, signaling, "Hey, I'm about to send some data."

2. Data transmission: Next, the transmitter sends the data bit by bit in a specific order. It's like sending a long message, one letter at a time.

3. End of communication: When all the data bits have been sent, the transmitter sends a stop bit, saying, "That's it, I've sent everything I had to send."

4. Error checking (optional): The receiver then checks if it has received the data correctly. This is done through a process known as parity checking. If the data isn't correct, the receiver can ask the transmitter to resend the data.


The telegraph was one of the first devices for long-distance serial communication, using a single wire to transmit data. Serial communication protocols and standards began to develop in the 1960s. These protocols, such as RS-232, SPI, I²C, RS485, USB, and MIPI, are widely used in electronic circuits, LCDs, OLEDs, computer systems, embedded systems, and telecommunications.

Advantages of Serial Communication

- Simpler Wiring: Fewer cables mean reduced cost and complexity.

- Longer Distances: Works better than parallel for long-distance connections.

- Reduced Crosstalk: Sequential data transfer minimizes interference.

Serial communication requires fewer lines or wires than parallel communication, which leads to lower implementation costs, less complex hardware, and simpler data transfer processes over long distances, making it the preferred choice for telecommunication networks.

https://iotboys.com/how-does-serial-communication-works-in-arduino/ https://newhavendisplay.com/blog/serial-vs-parallel-communication/

Serial communication can be either synchronous or asynchronous, depending on the protocol used. Asynchronous serial communication sends data one bit at a time, without a dedicated clock signal, relying on start and stop bits to frame each byte. Synchronous serial communication sends data in a continuous stream at a constant rate, requiring a shared or separate clock signal to synchronize the transmitting and receiving devices.

Types of Serial Communication

UART (Universal Asynchronous Receiver/Transmitter)

This is the most common form of serial communication used in Arduino. UART communication is asynchronous and is typically used for communication between the Arduino board and other microcontrollers, sensors, and modules.

I2C (Inter-Integrated Circuit)

I2C is a synchronous serial communication protocol that allows multiple devices to communicate over a shared bus. It is commonly used for connecting sensors and displays to Arduino.

SPI (Serial Peripheral Interface)

SPI is another synchronous serial communication protocol that is used for high-speed communication between Arduino and devices like displays, memory chips, and other microcontrollers.

UART (Universal Asynchronous Receiver/Transmitter):

UART is the most common form of serial communication used in Arduino. It is asynchronous, which means that data is sent without a shared clock signal. Instead, both the sender and receiver agree on a set of parameters to determine the data rate and timing.

The key parameters in UART communication are:

Baud Rate: This defines the data transmission speed in bits per second (bps). Both the transmitter and receiver must use the same baud rate for successful communication.

Data Bits: The number of data bits in each byte of data. Common values are 8 bits, 7 bits, or 6 bits.

Stop Bits: Stop bits indicate the end of a data byte. Common values are 1 or 2 stop bits.

Parity: Parity bits are optional and can be used for error checking. Common options are no parity, even parity, or odd parity.


https://mypractic.com/lesson-12-serial-port-uart-in-arduino-serial-library-debugging-programs-on-arduino/

Baud Rate

The baud rate determines how quickly data is transmitted over the serial communication channel. It is specified in bits per second (bps). Common baud rates used in Arduino projects include 9600, 19200, 38400, and 115200 bps. Both the sender and receiver must use the same baud rate to communicate successfully.

Choosing the appropriate baud rate depends on factors such as the hardware capabilities of your devices and the required data transfer speed. Higher baud rates allow for faster data transfer but may require more precise timing and can be susceptible to noise and errors over longer distances.

Setting Up Serial Communication

Setting up hardware serial communication is straightforward on Arduino boards with built-in serial ports


Unlike SPI or I2C, which are synchronous, UART is asynchronous, meaning it does not use a clock signal to synchronize the data transmission between devices. However, both devices must agree on the baud rate (speed of transmission).


UART ports allow us to communicate with other devices, such as other microcontroller boards (an Arduino, an ESP8266, another ESP32 board, or others), the computer, sensors, GPS .

UART Interface

TX (Transmit): Sends data.

RX (Receive): Receives data.

GND GND: Common ground.

ESP32 UART Peripherals

The ESP32 supports up to three UART interfaces:UART0, UART1 b>, and UART2, depending on the ESP32 board model you’re using.

UART0 is usually reserved for communication with the serial monitor during upload and debugging. However, you can also use it for communication with other devices after uploading the code if the Serial Monitor is not needed.

UART1 and UART2: available to communicate with external devices.

UART0 and the Serial Monitor

Relation Between UART0 and Serial Monitor. UART0 is the hardware port. Serial Monitor is the software tool on your computer.

When you open Serial Monitor, it connects to the COM port (Windows)

Microcontroller sends data → UART0 TX → USB cable → PC → Serial Monitor shows it.

PC sends data (you type in Serial Monitor) → USB → UART0 RX → Microcontroller receives it.

Setting up serial communication in software involves configuring the serial port with the appropriate settings. Here’s how you can do it in an Arduino sketch.


Serial.begin() initializes the serial communication with a specific baud rate. Replace 9600 with the baud rate you intend to use.

demostration in Wokwi





// ESP32 UART0 demo with Serial Monitor
// UART0 is the default "Serial" port connected to USB

void setup() {
  Serial.begin(115200);         // Start UART0
  Serial.println("UART0 Ready!"); // First message to Serial Monitor
}

void loop() {
  // Send data to Serial Monitor
  Serial.println("Hello from ESP32 via UART0");
  delay(1000);

  // If user types something in Serial Monitor, read it
  if (Serial.available() > 0) {
    String input = Serial.readStringUntil('\n');
    Serial.print("You typed: ");
    Serial.println(input);
  }
}

https://randomnerdtutorials.com/esp32-uart-communication-serial-arduino/

การสื่อสาร I2C

I²C ย่อมาจาก Inter Integrate Circuit (IIC) แต่นิยมเรียกว่า I²C มากกว่า เป็นการสื่อสารอนุกรมแบบ synchronous (ใช้สัญญาณนาฬิกากำหนดจังหวะการส่งข้อมูลระหว่างกัน) สามารถเชื่อมต่อกันเป็นระบบบัสทั้งรับและส่งข้อมูลหว่างกันได้หลายอุปกรณ์ ซึ่งสามารถกำหนดตำแหน่งของอุปกรณ์แต่ละตัวได้ โดยใช้สายสัญญาณจำนวนเพียง 2 เส้น คือ SCL(Clock) และ SDA (Data) ทำให้สามารถเชื่อมต่ออุปกรณ์ได้หลายตัวในระบบเดียวโดยใช้จำนวนสายไฟน้อยลง.

SCL (Serial CLock) จะกำหนดหรือส่งสัญญาณนาฬิกาเพื่อกำหนดความเร็วในการรับส่ง

SDA (Serial Data)เป็นข้อมูลที่จะใช้ในการรับหรือส่ง

ในระบบจะต้องมี 1 อุปกรณ์ที่ทำหน้าที่เป็น Master เพื่อคอยควบคุมการสื่อสารกับอุปกรณ์ของลูกข่ายที่เรียกว่า Slave ซึ่งอุปกรณ์ Master จะมีตัวหลักในการจัดการติดต่อสื่อสาร หรือเป็นตัวกลาางในการรับหรือส่งระหว่าง Slave โดย Master จะเป็นผู้เริ่มต้นในการติดต่อก่อนเสมอ โดยลักษณะของการสื่อสารเป็นระบบบัส ดังแสดงในรูปด้านล่าง


https://www.totalphase.com/blog/2020/12/differences-between-uart-i2c/?srsltid=AfmBOoolrKnYHi-NfX4S9fIWqYnEINlKNsnOjEghzgMs5wjxVqJj614i

หลักการทำงาน

เริ่มต้น Master จะส่งสัญญาณ Start ด้วยการ กำหนดลอจิก Low ที่ขา SDA และ SCL เพื่อให้อุปกรณ์ Slave รอรับข้อมูล

จากนั้นจะเรื่มส่งข้อมูล ซึ่งประกอบด้วย 7 บิตแรกเป็นตำแหน่ง (Address) ของ Slave ที่ต้องการติดต่อด้วย

จากนั้นจะส่งสัญญาณการอ่าน / เขียน เพื่อแจ้งอุปกรณ์ Slave ตัวที่ต้องการติดต่อด้วยว่าต้องการอ่าน(ส่งข้อมูลกลับ) หรือ เขียนข้อมูลให้กับ Slave (หากเป็นบิต 0 หมายถึง อ่านข้อมูล หากบิต 1 หมายถึงต้องการเขียนข้อมูล)

อุปกรณ์ Slave ทุกตัวจะได้รับสัญญาณพร้อมๆกัน และจะตรวจสอบตำแหน่ง Address ว่าตรงกับตนเองหรือไม่ ถ้าตัวใดตรงกับ Address ก็จะส่งสัญญาณ ACK กลับไปยัง Master เพืื่อแจ่งกลับไปยัง Master ว่ามีตำแหน่งที่เรียกเชื่อมต่ออยู่

ขั้นต่อไปจะเป็นการส่งสัญญาณ Clock จำนวน 8 บิต หรือข้อมูล 8 บิต (1Byte) ซึ่งอาจจะเป็นการเขียน หรืออ่านข้อมูล ระหว่าง Master และ Slave แล้ว อุปกรณ์ Slave จะตอบสนองด้วยสัญญาณ ACK อีกครั้ง เพื่อแจ้งการรับหรือส่งข้อมูลเรียบร้อยไม่ผิดพลาด หาก Slave ไม่ส่งสัญญาณ ACK กลับไป Master จะส่งข้อมูลชุดเดิมมาอีกคร้ัง เนื่องจากเข้าใจว่าอาจจะมีการผิดพลาดในการส่งเกิดขึ้น

เมื่อ Master ส่งข้อมูลเสร็จสิ้นก็จะส่งสัญญาณ STOP แจ้งมายัง Slave สิ้นสุดการส่งข้อมูล

โดยจากรูปด้านล่างเป็นลักษณะ Master อ่านข้อมูลจาก Slave

การเขียนข้อมูลไปยัง Slave

การเขียนข้อมูลจาก Master ไปยัง Slave จะคล้ายกับการอ่านข้อมูลจาก Slave จะแตกต่างกันตรงส่วนทีบิตระบุว่าจะอ่านหรือจะเขียน ซึ่งหาก Master ต้องการอ่าน หรือให้ Slave เขียนกลับไปที่ Master บิตที่ระบุนี้จะเป็นลอจิก 1 จำนวน 8 บิต เมื่อครบ 8 บิต Master จะให้สัญญาณ ACK แต่หาก Master ไม่ต้องการข้อมูลแล้วหลัง Byte สุดทั้าย Master จะส่งสัญญาณ NACK กลับมายัง Slave และปิดท้ายด้วยสัญญาณ STOP เพื่อสิ้นสุดการส่ง ดังแสดงในรูป

ซึ่งการสื่อสารในรูปแบบ I2C ส่วนใหญ่จะให้ MCU ทำหน้าที่เป็น Master และให้อุปกรณ์อื่น เช่น Display หรือ Sensor ทำหน้าที่เป็น Slave โดย Display ที่ใช้การสื่อสารแบบ I2C เช่น จอแสดงผล LCD หรือ OLED เป็นต้น

การสื่อสารแบบ RS485

การสื่อสาร RS-485 เป็นมาตรฐานทางไฟฟ้าสำหรับการสื่อสารแบบอนุกรม (serial communication) ที่ใช้สายคู่บิดเกลียว (twisted pair) ในการส่งข้อมูลแบบหลายจุด (multi-drop) ผ่านสายเคเบิลระยะทางไกล ในสภาพแวดล้อมที่มีสัญญาณรบกวน. มีคุณสมบัติเด่นคือ สามารถเชื่อมต่ออุปกรณ์ได้หลายตัวบนบัสเดียวกัน, ทนทานต่อสัญญาณรบกวนได้ดี, และส่งสัญญาณได้ไกลถึง 1,200 เมตร.

เป็นการสื่อสารแบบดิฟเฟอเรนเชียล (Differential Signaling): RS-485 ใช้สัญญาณไฟฟ้าแบบสมดุล โดยส่งสัญญาณดั้งเดิมและสัญญาณกลับด้านกันบนสายสองเส้น (A และ B) เพื่อสร้างความแตกต่างของแรงดันไฟฟ้าที่ใช้แทนข้อมูล. วิธีนี้ช่วยลดผลกระทบจากสัญญาณรบกวนที่ส่งผลต่อสายทั้งสองเท่าๆ กัน เป็นการทำงานที่นิยมที่สุดคือการใช้สาย 2 เส้น คือ A และ B สำหรับรับและส่งข้อมูล. อุปกรณ์จะสามารถส่งข้อมูลไปในทิศทางใดทิศทางหนึ่งได้ในแต่ละครั้งเท่านั้น แบบ Half Duplex สามารถเชื่อมต่ออุปกรณ์ได้สูงสุดถึง 32 อุปกรณ์บนบัสเครือข่ายเดียวกัน (bus topology), ทำให้เหมาะสำหรับการควบคุมระบบอุตสาหกรรมที่มีอุปกรณ์หลายชิ้น ระยะทางได้ไกลถึง 1200 m การใช้การส่งสัญญาณแบบดิฟเฟอเรนเชียล ทำให้ RS-485 มีความทนทานต่อสัญญาณรบกวนทางแม่เหล็กไฟฟ้า (EMI) ในสภาพแวดล้อมอุตสาหกรรมได้ดี

การรับ-ส่งข้อมูลดิจิตอลแบบ RS-485 นั้น จะส่งข้อมูลโดยใช้สายไฟเพียงแค่ 2 เส้น คือ A กับ B เป็นตัวบอกค่ารหัสดิจิตอล (Digital Code) โดยจะใช้ความแตกต่างของแรงดันไฟฟ้าระหว่างขั้ว A และ B เป็นตัวบอก ดังนี้

• เมื่อ Va - Vb ได้แรงดันไฟฟ้าน้อยกว่า -200 mV คือสัญญาณดิจิตอลเป็น 1

• เมื่อ Va - Vb ได้แรงดันไฟฟ้ามากกว่า +200 mV คือสัญญาณดิจิตอลเป็น 0

อย่างไรก็ตามที่ปลายสายของของวงจรสื่อสารบัสจะต้องมีการต่อความต้านทาน (เรียกว่า Termination Resistance หรือ RT ขนาด 120 โอห์ม) และการใส่ตัวต้านทาน (RB ) สำหรับ Failsafe Biasing (Source: TI) เพื่อป้องกันย้อนกลัยหรือการ Echo ของสัญญาณกลับมารบกวน

https://iot-kmutnb.github.io/blogs/electronics/rs485_modules/

การประยุกต์ใช้งาน ในระบบควบคุมอุตสาหกรรม: ใช้ในการเชื่อมต่ออุปกรณ์ควบคุม เช่น PLC, HMI, และอุปกรณ์ควบคุมอื่นๆ การควบคุม VFD (Variable Frequency Drives): เหมาะสำหรับการควบคุมมอเตอร์หลายตัวจากจุดควบคุมเดียว

https://www.sangchaimeter.com/article/310/PLC_MODBUS_RTU https://www.realpars.com/blog/rs485 Thumbnail of the YouTube video

https://youtu.be/3wgKcUDlHuM

https://www.nectec.or.th/news/news-public-document/modbus-protocol.html

โปรโตคอล Modbus

การสื่อสารของระบบ RS-485 จะใช้โปรโตคอล Mosbus ซึ่งเป็นการสื่อสารแบบอนุกรม (Serail Base Protocol) ในรูปแบบ Master / Slave ซึ่งอุปกร์ Master จะเป็นตัวเริ่มต้นสื่อสารกับกับอุปกรณ์ Slave หรืออีกนัยนึง อุปกรณ์ Salve จะไม่ใช่ตัวเริ่มต้นการสื่อสาร หรือจะไม่ส่งข้อมูลให้กับ Master จนกว่า Master จะร้องขอมาเท่านั้น Slave อาจเป็นอุปกรณ์ต่อพ่วงใดๆ เช่น Input/Output Transducer, วาล์ว (Valve), Inverter,อุปกรณ์บันทึกข้อมูล (Data Logger) หรืออุปกรณ์เครื่องมือวัดอื่นๆ เป็นต้น ซึ่งประมวลผลและส่งข้อมูลไปยังMaster การรับส่งข้อมูลด้วยโปรโตคอล Modbus Serial สามารถเลือกได้ 2 โหมด คือ โหมด ASCII และโหมด RTU ซึ่งทั้ง 2 โหมดนี้มีความแตกต่างกันที่การกาหนดรูปแบบของชุดข้อมูลภายในเฟรม จะเลือกโหมดใดก็ได้แต่มีเงื่อนไขว่าอุปกรณ์ทุกตัวที่ต่อร่วมกันอยู่ในบัสหรือเครือข่ายเดียวกัน จะต้องตั้งให้เลือกใช้โหมดเดียวกันทั้งหมด รวมถึง Serial Parameter ต่างๆ เช่น Baud Rate, Data Bit,Stop Bit และ Parity Bit

การสื่อสารแบบโปรโตคอล Modbus จะรับส่งข้อมูลในรูปแบบชุดข้อมูล ใน 1 ชุดข้อมูลจะประกอบด้วย 6 ส่วน คือ

1. Start Bit : เป็นส่วนที่ใช้ในการแจ้งการเริ่มต้นการติดต่อสื่อสารไปยัง Slave

2. Address : ส่วนที่ใช้ระบุตำแหน่งของอุปกรณ์ Slave ที่ต้องการติดต่อด้วย

3. Function Code : ส่วนที่ระบุว่าต้องการรับ หรือส่งข้อมูลให้แก่อุปกรณ์ Slave

4. Data : ข้อมูลที่จะรับหรือส่งแต่ละไบต์ และตามด้วยจำนวนของไบต์ที่จะรับหรือส่ง

5.CRC (Cyclic Redundancy Check): เป็นรหัสที่ใช้ตรวจสอบความผิดพลาดของการรับส่งในระหว่างการสื่อสาร

6. End Bit : ชุดปิดทั้ายเพื่อแจ้งสิ้นสุดการติดต่อสื่อสาร

ประเภทของ Function Code

Function Code จะมีการทำงานหลัก 2 แบบ คือ การอ่าน (Read) และการเขียน (Write) ข้อมูลชนิดดิจิตอล (0 หรือ 1) ไปยังตำแหน่ง Coil หรือ Contact ของอุปการ Slave แต่หากข้อมูลเป็นตัวเลขเป็นอนาล๊อกจะเขียนไปยัง Register ของ Slave ซึ่งหากเป็นรีจิเตอร์จะมีขนาด 16 บิต ซึ่งมีค่าได้ตั้งแต่ 0000 ถึง FFFF โดย Function Code ที่ใช้งานดังรูป

Output coil : คือตำแหน่งควบคุมเอาท์พุต ของอุปกรณ์ Slave โดยข้อมูลจะเป็น 1 หรือ 0

Input Contacts : คือตำแหน่งควบคุมด้านอินพุตของอุปกรณ์ Slave โดยข้อมูลจะเป็น 1 หรือ 0

Input Registersตำแหน่งหน่วยความจำรีจิสเตอร์บนอุปกรณ์ Slave

Holding Register

ตำแหน่งหน่วยความจำบนอุปกรณ์ Slave

ตำแหน่งที่ใช้ระบุตำแหน่งหน่วยความจำบนอุปกรณ์ Slave ซึ่งมีขนาด 16 บิตนั้น สามารถจัดโครงสร้างตำแหน่งของ Ouput Coil หรือ Input Contact หรือ Registers ต่าง ๆ ได้ดังนี้

Output coils: ตำแหน่งแอดเดรสจะเริ่มต้นที่ 000001

Input contacts: ตำแหน่งแอดเดรสจะเริ่มต้นที่ 100001

Input registers: ตำแหน่งแอดเดรสจะเริ่มต้นที่ 300001

Holding registers: ตำแหน่งแอดเดรสจะเริ่มต้นที่ 400001

การอ่านข้อมูลจาก Slave

การอ่านข้อมูลจาก Slave นั้น Master จะเป็นตัวเริ่มต้นติดต่อ โดยจะส่งสัญญาณเริ่มต้นการติดต่อและตามด้วยตำแหน่งของ Slave ที่จะติดต่อด้วย จากนั้นจะตามด้วย Fucnction Code + ตำแหน่งที่ต้องการอ่านและจำนวนข้อมูลที่ต้องการอ่าน + CRC

ตัวอย่าง Master ต้องการอ่านค่าของ Holding Register ที่ตำแหน่ง 40103 ถึง 40105 จาก Slave หมายเลข 19 ซึ่งเฟรมข้อมูลที่จะถูกส่งออกไปยังบัสจะเป็นลักษณะ 13 03 0066 0003 E6A6 ซีงตัวเลขนี้เป็นเลขฐาน 16

13 คือตำแหน่งเลขหมายของอุปกรณ์ Slave เลขหมาย 19 (19DEC = 13HEX)

03 คือ Functio Code ที่ต้องการอ่านค่าจาก Holding Register

0066 ตำแหน่งของ Holding Register ที่ต้องการอ่าน โดยนับจากตำแหน่งแรกสุดของกลุ่ม Holding Register (40001) โดยตำแหน่งที่ต้องการอ่าน คือ 40103 ซึ่งห่างจาก 40001 = (40103-4001)=102Dec แปลงเป็น 66HEX

0003คือจำนวน Register ที่ต้องการอ่าน 3 จำนวน คือ 40103 - 40105

E6A6คือ CRC เพื่อเป็นรหัสตรวจสอบความผิดพลาดในการส่งข้อมูล

การตอบกลับของอุปกรณ์ Slave หมายเลข 19 ก็จะนำส่งขอมูลด้วยเฟรมข้อมูลดังนี้

06 xxxx xxxx xxxx

06คือจำนวนไบต์ของข้อมูลทีต้องการนำส่ง (ข้อมูลจำนวน 3 ตำแหน่งของ 40103-40105 ตำแหน่งละ 2 ไบต์ รวมกันเป็น 6 ไบต์

XXXX XXXX XXXX คือ ข้อมูลของทั้ง 3 ตำแหน่ง ที่ตอบกลับมา (ตำแหน่งละ 2 bytes)

https://www.wisco.co.th/main/book/export/html/373

หัวข้อ