Serial Monitor displays nothing

Hi. I upload this code but nothing id displayed on serial monitor. What is the problem?

String name="";
void setup() {
  Serial.begin(115200);
}
void loop() {
  Serial.println("What is your name?");
  while (Serial.available() == 0) {}
  name = Serial.readString();
  Serial.print("Hello ");
  Serial.println(name);
}

What speed is your serial monitor set to?

See here if is 115200...

SerialMonitor3

It is 115200. But nothing is displayed.

That you did not tell us which board you're using :smiley:

Your code happily does what it's supposed to do when loaded to a SparkFun Redboard (read: Uno).

1 Like

Restart the monitor, and press the reset button on the board. Sometimes they are finicky.

I use ESP8266.

I reset the board but the result didn't changed.

Since you uploaded the code we know the serial interface works.
Comment these two lines out and see what happens:

 while (Serial.available() == 0) {}
  name = Serial.readString();
1 Like

In setup, wait until the port is ready. Once you get into the loop, enter something into the input box. The output could still be buffered, you could add some flush statements in the appropriate places.

Should I comment these in void setup or void loop?

The loop.

Sorry, I don't understand what you say. Please explain with details.

I added it in the loop but nothing is displayed in Serial Monitor.

Post the code again, you should have only added "//" in front of those two lines.

I highly recommend you get a copy of the Arduino Cookbook and skim it cover to cover and try the projects relative to yours.

Is this your first project? Have you tried any of the examples included with the IDE?

I am sorry to say I feel you are not quite ready to start the project at this point. We have no idea of your skill set or what resources you have available to you. I would suggest you start with some of the tutorials that are on line, sorry to say some are not so good but many are very good. Start by learning the basics, you will need to control outputs as well as interpret inputs such as reading a switch, receiving a message etc. Start with the LED, they are not expensive and there is even one on most of the Arduinos. At this point you should have also found several tutorials on basic electronics that you have gone through. You should acquire a copy of the Arduino Cookbook and go through that. I have no clue as to how fast you will learn this but it will probably take a few months.

During this process you will learn what an IDE is and how to use it to generate and upload your code to the Arduino. Let us know how you progress. For more help check this link: https://docs.arduino.cc/learn/starting-guide/getting-started-arduino/

In setup, after the Serial.begin, add a while statement similar to what is in your loop. Make SURE the Serial port is ready. I often AND it with a millis timer and if I drop thru restart the board.
After each Serial.print(ln), add a flush.

Tank you for your guidance. I tried LED and it worked correctly. but I don't know why this project doesn't work?

This is the code:


String name="";
void setup() {
  Serial.begin(115200);
}
void loop() {
  Serial.println("What is your name?");
//  while (Serial.available() == 0) {}
//  name = Serial.readString();
  Serial.print("Hello ");
  Serial.println(name);
}

I uploaded this code and this is the result:

It works fine, what are you not doing?

Hi @alija22. The sketch program starts running as soon as the upload finishes, or you reset or power the board. Any data the board prints to Serial between that time and when the Serial Monitor is started is lost. Your sketch prints "What is your name?" immediately after startup, so you might not see this message in Serial Monitor.

Upload your real sketch to the board again, then with Serial Monitor open, press and release the reset button on the board. Do you now see the expected "What is your name?" message printed in Serial Monitor?

1 Like