1

I wrote serial.println in my code but the string was printed in same line on serial moniter.

#include <WiFi.h>//for connecting esp32 to a wifi

#include <TinyGPS++.h>//to obtain gps data from neo-6m gps module 

#define WIFI_SSID "Airtel_pawa_4182"//wifi name in simple words
#define WIFI_PWD "Ahuja6230"//wifi password

TinyGPSPlus GPS;//creating a gps object from tinygps++ library

void setup() {
 Serial.begin(9600);
  //wifi setup{
  WiFi.begin(WIFI_SSID, WIFI_PWD);
  Serial.println("connecting to wifi");
  while(WiFi.status() != WL_CONNECTED){
   Serial.println(".");
   delay(1000);    
  }
  Serial.println("Successfully connected to ");
  Serial.print(WIFI_SSID);
  //}

  //gps setup{
  Serial2.begin(9600);
  delay(1000);
  Serial.println("ESP32-GPS Tracker");
  Serial.println("Initializing...");
  //}
}

void loop() {
}

In this code the string "esp32-gps tracker" is printing just after the previous string in same line on serial moniter.

I tried running code again and again but it didn't work...

I am using esp32 board(just a detail)

What do i do?

2
  • 1
    You need to be way more specific for your question to be answerable. Same line? Same as what? Please, post a minimal testable sketch showing the issue, tell us what you expected as an output, and tell us what the actual output was. Commented Mar 30 at 11:18
  • @EdgarBonet done
    – Yug Ahuja
    Commented Mar 30 at 11:30

3 Answers 3

2

The previous print statements are these:

  Serial.println("Successfully connected to ");
  Serial.print(WIFI_SSID);

The SSID won't be terminated by a newline. Perhaps you meant:

  Serial.print("Successfully connected to ");
  Serial.println(WIFI_SSID);

?

1

To follow on from previous answer, your solution is to either:

  • change the previous Serial.print() to a Serial.println() so that that text is followed by a line end, or
  • add a \n to the front of the line you are having the problem with, so it starts with a new line, to make up for the "missing" new line in the previous Serial.print().
3
  • 1
    For consistency with what Serial.println() does, you should add "\r\n", not only '\n'. If you ever use a terminal emulator instead of the Arduino serial monitor, a bare '\n' will likely move the cursor one line down while staying on the same column. Commented Mar 30 at 12:00
  • Depends on your operating system. In Linux a newline advances to the start of a new line. On Windows YMMV. It's very common in Linux code to use \n for a newline when outputting and not \r\n, which may in fact put some weird character (the carriage-return character) in your terminal window.
    – Nick Gammon
    Commented Mar 31 at 8:08
  • @NickGammon: On a Linux terminal, a \n alone is a vertical-only cursor movement. When a program outputs \n, the tty driver converts it to \r\n before it reaches the terminal. This should become clear if you try to use a terminal with this output post-processing disabled (stty -opost). A serial communication program like picocom, by default, disables all tty driver processing: the bytes from the Arduino reach the terminal unaltered. This plays well with Serial.println(), as it ends the line with \r\n. Commented Mar 31 at 10:20
0

When sending text through a serial line, it is customary to terminate each line with some sort of end-of-line marker: you print some text, and immediately after it the EOL to signify that “this is now a complete line”. Accordingly, the Serial.println() method prints the requested text, immediately followed by the EOL marker (in this case, CR+LF).

From your question, you seem to be thinking that the EOL immediately precedes the text. This is not the case.

See JRobert's answer for the correct way to build a line from multiple print statements: println() should be use on the last piece, not on the first one.

3
  • Yes. Nice explanation. Now I know where I mistook. I thought that by using "print.ln()", the string will be printed in new line(I got this info from google). But it isn't like that print.ln is for terminating a line and starting a new line
    – Yug Ahuja
    Commented Mar 30 at 12:05
  • Jrobert's comment was the first one to mark my mistake so I declared it as answer. But I also thank you for the explanation
    – Yug Ahuja
    Commented Mar 30 at 12:08
  • 1
    @YugAhuja println not print.ln you need to be very specific when coding.
    – Nick Gammon
    Commented Mar 31 at 8:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.