Serial Communication

Background

The Arduino comes in with a built-in serial port which allows you to communicate back and forth between the computer and the Arduino. Up until this point, we have really only sent messages from the Arduino to the PC for debugging or to view information. Now, we will talk more about the Serial interface and how to send commands and messages to the Arduino from a PC.

 

Hardware Description

Serial communication occurs via USB. Avoid using pins 0 & 1 on the Arduino to prevent conflicts with the serial communication.

 

Software Description

Full documentation and examples here: https://www.arduino.cc/reference/en/language/functions/communication/serial/

Information about String() class here: https://www.arduino.cc/reference/en/language/variables/data-types/stringobject/

Note: Arduinos are very-limited on runtime memory. Strings can take up a surprising amount of space. Since their space is dynamically allocated, they can easily grow too large and cause a stack overflow (data surpasses its allocated space and erases other important information causing unexpected errors or the program to crash). If you are experiencing weird behaviors, try to reduce the number of strings or migrate your code to use char[] arrays.

https://create.arduino.cc/editor/mjdargen_ravens/c19864e3-8db3-45f1-8c76-aa3d1aeee867/preview

 

Resources