Understanding Arduino Serial Communication

Serial communication and debugging are essential to working with Arduino boards. Simply put, serial communication is a method that the board uses to communicate with other devices – such as another computer, a peripheral, etc. When we discuss Arduino serial communication, it’s also necessary to distinguish between its physical components (the Arduino’s serial ports, for example) and its software components (using the serial monitor in the Arduino IDE).

Arduino Serial Port

Every Arduino board has at least one serial port, and many have additional built-in serial ports (Serial1, Serial2, etc.). Other boards, such as the Leonardo, also have serial communication in the form of a USB) port. In USB-enabled boards, we refer to the USB as Serial. The other standard serial ports begin with “Serial1” and number progressively from there.

The port on your board called Serial (Serial1 if you have USB) communicates via pins 0 and 1 (RX and TX, respectively). Keep in mind that if you use these pins for serial data transmission, they will be unavailable for digital I/O. To use the extra serial pins (pins 18 and 19 on the Arduino Due, for instance), to communicate with your PC, make sure you have a USB-to-serial adapter, as the extra pins are not connected to the board’s built-in adapter.

The Arduino website has a detailed breakdown of the various serial ports and required adapters and connections by model on its website.

Arduino Serial Example Code

If you still need more serial ports than the built-in ones provided by your board, you have another option. The Arduino Software Serial library will allow you to use other digital pins, supplemented by software that replaces the RX and TX functions, to expand your board’s serial connectivity.

To better understand the idea, we can look at Tom Igoe’s public domain example code here:

#include <SoftwareSerial.h>

SoftwareSerial mySerial(10, 11); 
// RX, TX

voidsetup() {
  
// Open serial communications and wait for port to open:
  
Serial.begin(57600);
  
while (!Serial) {
    ; 
// wait for serial port to connect. Needed for native USB port only
  }


  
Serial.println("Goodnight moon!");

  
// set the data rate for the SoftwareSerial port
  mySerial.
begin(4800);
  mySerial.
println("Hello, world?");
}

voidloop() { // run over and over
  
if (mySerial.available()) {
    
Serial.write(mySerial.read());
  }
  
if (Serial.available()) {
    mySerial.
write(Serial.read());
  }
}

This code creates a virtual serial port using pins 10 and 11 as the RX and TX ports, respectively.

Arduino Serial Monitor Communication

Once you connect your device to a PC running the Arduino IDE software, you can use the serial monitor to communicate with the board. Just select “Serial Monitor” from the Tools menu (or click the little magnifying glass) to open the serial monitor. Use the same baud rate from the call to begin serial communication in your code.

The serial monitor displays any information that the Arduino outputs to the serial port, so it’s essential for debugging purposes. When your sketch or program isn’t executing as expected, you can use serial outputs in your code. Those output messages on the serial monitor allow you to assess the state of the program to figure out where the error is.

Conclusion

Understanding the Arduino serial ports and Arduino software serial is important for a number of basic functions like connecting your board to a personal computer, connecting to other peripheral devices, and successfully debugging problematic or faulty code. For more information on serial functions, software serial, and more, consult the Arduino database.

관련 상품 참조

A000066 | Arduino Uno Rev3

Arduino Corporation Embedded System Development Boards and Kits 보기

 

최신 뉴스

Sorry, your filter selection returned no results.

개인정보 보호정책이 업데이트되었습니다. 잠시 시간을 내어 변경사항을 검토하시기 바랍니다. 동의를 클릭하면 Arrow Electronics 개인정보 보호정책 및 이용 조건에 동의하는 것입니다.

당사의 웹사이트에서는 사용자의 경험 향상과 사이트 개선을 위해 사용자의 기기에 쿠키를 저장합니다. 당사에서 사용하는 쿠키 및 쿠키 비활성화 방법에 대해 자세히 알아보십시오. 쿠키와 추적 기술은 마케팅 목적으로 사용될 수 있습니다. '동의'를 클릭하면 기기에 쿠키를 배치하고 추적 기술을 사용하는 데 동의하는 것입니다. 쿠키 및 추적 기술을 해제하는 방법에 대한 자세한 내용과 지침을 알아보려면 아래의 '자세히 알아보기'를 클릭하십시오. 쿠키 및 추적 기술 수락은 사용자의 자발적 선택이지만, 웹사이트가 제대로 작동하지 않을 수 있으며 사용자와 관련이 적은 광고가 표시될 수 있습니다. Arrow는 사용자의 개인정보를 존중합니다. 여기에서 당사의 개인정보 보호정책을 읽을 수 있습니다.