Arduino programming guide series

A closer look at line feeds and carriage returns

If you have an Arduino application that must parse an HTTP response, it helps to understand the difference between the two most common special characters: line feed and carriage return. Once you understand this, you will be able to reduce the complexity of your parser by half.

When writing Arduino sketches that parse HTTP requests, you come across code like this:

if (c == '\n' && currentLineIsBlank) {                           client.println("HTTP/1.1 200 OK");
   client.println("Content-Type: text/html");
   client.println("Connection: close");

In this code, there's a special character: "\n".

This special character is a line feed.

In some cases, you may also see the Carriage Return ("\r") special character.

In this article, I will discuss Line Feed and Carriage Return because they seem to be causing a lot of confusion to learners.

In a nutshell:

  • "\n" is the new line character, or "line feed (LF)." It is the character that marks the end of a line and the beginning of a new line. In ASCII code, the new line character is encoded as decimal 10.
  • "\r" is the carriage return (CR) character. This character returns the cursor to the beginning of the line, as opposed to creating a new line (this is what line feed does). In ASCII code, the carriage return character is encoded as decimal 13.

In modern computers (as opposed to mechanical typewriters where these operators find their roots), how exactly LF and CR work depend on the operating system.

In Unix and Unix like systems, like Mac OS X and Linux, an LF creates a new line and returns the cursor to the start of the line.

In Windows, to achieve the same outcome, you must combine CR with LF. This will result in, first, the generation of a new line, and then, the return of the cursor to the start of the line.

Which of these special characters should you use in your Arduino sketches?

Well, it depends on the application (you probably expected this).

For example, if the application involves the parsing of a web page, which is encoded in an HTTP message, and we want to know when the header has been received, then we can look for a CR + LF combination. We search for CR + LF because this is required by the HTTP standard.

But here's a simple trick: because in HTTP messages, CR and LF almost always appear together, we can reduce the workload of the Arduino by trying to detect one or the other. This realisation cuts not only the workload, but also the complexity of the parsing code.

Another thing to consider is that the HTTP standard is tolerant regarding the use a line feed (LF) to mark the end of a line; this is not strictly correct, but it is widespread and works just fine. This is why in Arduino sketches that parse HTTP messages (like the one in the top of this article), you see that the code will try to detect line feeds (LF, "\n"), and ignore carriage returns (CR).

New to the Arduino?

Arduino Step by Step Getting Started is our most popular course for beginners.

This course is packed with high-quality video, mini-projects, and everything you need to learn Arduino from the ground up. We'll help you get started and at every step with top-notch instruction and our super-helpful course discussion space.

We publish fresh content each week. Read how-to's on Arduino, ESP32, KiCad, Node-RED, drones and more. Listen to interviews. Learn about new tech with our comprehensive reviews. Get discount offers for our courses and books. Interact with our community. One email per week, no spam; unsubscribe at any time

Image
{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}