In the context of Arduino, CR primarily refers to the Carriage Return character, an important control character used extensively in serial communication to mark the end of a line or a message string. Often paired with the Line Feed (LF) character, CR signals to the receiving device that a new line of data should begin, playing a crucial role in data parsing and readability.
Understanding Carriage Return (CR) in Computing
Historically, the concept of a "carriage return" comes from typewriters, where moving the carriage back to the left margin was necessary before starting a new line of text. In digital systems, CR serves a similar purpose: returning the cursor to the beginning of the current line. It's a non-printable control character with a specific ASCII value, making it machine-readable.
ASCII Representation of CR
The Carriage Return character has a universally recognized ASCII value, allowing different systems to interpret it consistently.
Character | ASCII Decimal Value | ASCII Hex Value | Control Character |
---|---|---|---|
CR | 13 | 0x0D | \r |
LF | 10 | 0x0A | \n |
CR in Arduino Serial Communication
Arduino boards frequently communicate with computers or other devices via serial ports. When sending data, particularly text or numerical values, it's essential to signal when one message ends and another begins. This is where CR, often combined with LF, becomes indispensable.
The Role of Serial.println()
The Arduino IDE's standard Serial.println()
function simplifies sending data followed by line termination.
- Whenever Arduino prints data using the
Serial.println()
function, it automatically sends both the carriage return (\r
) and line-feed (\n
) characters after the data. - This combination (often referred to as CRLF) is vital because it helps the receiving side—such as a serial monitor, a Python script, or a data logging application—know that the full message string has been received and can then process that complete line of data.
- Additionally, in some specific Arduino communication protocols, a comma (
,
) might be sent after the last numerical value within a string. This serves as an extra delimiter to aid the receiving side in precisely detecting the end of a specific numerical value before the entire message string terminates with CR/LF.
CR vs. LF (Line Feed)
While often used together as CRLF, Carriage Return and Line Feed have distinct functions:
- CR (Carriage Return,
\r
): Moves the cursor to the beginning of the current line. - LF (Line Feed,
\n
): Moves the cursor down to the next line, maintaining the current horizontal position.
Many operating systems and communication protocols use different line termination sequences:
- Windows: Uses CRLF (
\r\n
) - Unix/Linux/macOS: Uses LF (
\n
) - Older macOS: Used CR (
\r
)
When communicating with an Arduino, it's crucial to be aware of the receiving system's expected line ending to ensure proper parsing.
Practical Applications and Parsing Data
Understanding CR and LF is critical for robust serial communication:
- Message Delimitation: CR/LF acts as a universal end-of-message marker, allowing the receiving application to know when a complete data packet has arrived from the Arduino.
- Input Recognition: When you type a message into the Arduino Serial Monitor and press Enter, the Serial Monitor typically sends both CR and LF characters to the Arduino, signaling the end of your input.
- Parsing Data Streams: Applications reading data from Arduino look for these characters to segment the incoming byte stream into individual, meaningful lines. This is essential for:
- Extracting sensor readings (e.g., temperature, humidity).
- Processing commands sent to the Arduino.
- Parsing structured data where values might be separated by other delimiters (like commas), and the entire line is terminated by CR/LF. The aforementioned comma after a numerical value provides a finer-grained delimiter within the line.
Sending and Receiving CR in Arduino
Arduino provides flexible ways to handle CR and LF.
Sending CR/LF from Arduino
While Serial.println()
is the simplest method, you can also send CR and LF explicitly using Serial.print()
:
void setup() {
Serial.begin(9600); // Initialize serial communication at 9600 baud
}
void loop() {
Serial.print("Hello"); // Sends "Hello"
Serial.print("\r"); // Sends Carriage Return (CR) explicitly
Serial.print("World\n"); // Sends "World" followed by Line Feed (LF) explicitly
// The following line is equivalent to the above three for basic line termination:
// Serial.println("Hello World");
// Example with a numerical value and comma delimiter as per protocol
int sensorValue = 1023;
Serial.print("SensorValue:");
Serial.print(sensorValue);
Serial.print(","); // Comma after the last numerical value, as specified for aiding detection
Serial.println("Done"); // CR/LF for the end of the full message string
delay(2000); // Wait for 2 seconds
}
Receiving CR/LF on Arduino
When Arduino receives serial data, you often need to detect CR or LF to know when a complete command or data string has been received.
String receivedData = "";
void setup() {
Serial.begin(9600);
Serial.println("Ready to receive...");
}
void loop() {
while (Serial.available()) {
char incomingByte = Serial.read();
if (incomingByte == '\n') { // Check for Line Feed (LF) as the primary line delimiter
// A full message line has been received
Serial.print("Received message: ");
Serial.println(receivedData);
// Process receivedData here
// For example, if receivedData contains "SensorValue:1023,", you can parse it
receivedData = ""; // Clear the string for the next message
} else if (incomingByte != '\r') { // Ignore Carriage Return (CR) if LF is used as primary delimiter
receivedData += incomingByte; // Append character to string
}
}
}