Serial.print in void loop

i am using Arduino uno r4 minima. i have simple code of hello world..

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  Serial.println("hello world");
}

void loop() {
  // put your main code here, to run repeatedly:

}

no output in serial monitor
same code is working in arduino uno r3 .

In the IDE, click on Edit, then Copy for Forum, that will copy your code for pasting on the Forum. Then come back here and just do a paste.

Did you make sure the Serial monitor is at 9600 bauds ?

In contrast to the UNO R3, the R4 has a native USB interface. After Serial.begin() you have to wait until the USB communication is established. Then you can print ( because of the native USB the baud rate has no meaning ).
Try:

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  while(!Serial);  // Wait until USB communication is ready
  Serial.println("hello world");
}

void loop() {
  // put your main code here, to run repeatedly:

}

N.B. Despite of the very similar name ( UNO R3 <-> UNO R4 ), be aware that he R4 ist technically a completely different board.

right - good point (and the 9600 bauds likely does not matter)

Serial is immediately ready.

bool wasReady;
unsigned waited;

void setup() {
  Serial.begin(9600);
  wasReady = Serial;
  while (!Serial) waited++;
}

void loop() {
  Serial.print(millis());
  Serial.print('\t');
  Serial.print(wasReady);
  Serial.print('\t');
  Serial.println(waited);
  delay(250);
}

After an Upload, the Serial Monitor, which was already open at the matching baud rate, takes about a half-second to print.

515 	1	0
773 	1	0
1031	1	0
1290	1	0

Hitting the Reset on my R4 WiFi, it prints immediately

0      	1	0
257 	1	0
515 	1	0
773 	1	0
1031	1	0
1290	1	0

This is on my Linux box, if that matters.

I can verify, as I expected, that the sketch shown in post #1 produced no output on my R4 Minima. Not even after pressing the reset button. I have minicom connected to /dev/ttyACM0, which automatically reconnects after the upload is complete.

However, the following does show "hello world", both after uploading, and after pressing reset:

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  while(!Serial);
  Serial.println("hello world");
}

void loop() {
  // put your main code here, to run repeatedly:

}

Running the sketch shown in post #6 showed the following:

1456    0       208981
1706    0       208981
1956    0       208981
2206    0       208981
2456    0       208981
2706    0       208981

which indicates to me that yes, while(!Serial); is both necessary, and functioning. And that Serial is not immediately ready.

Again, this was on an R4 Minima.

The R4 Wifi is very different from the R4 Minima in this aspect.

Thanks to both of you. Another bit of trivia to file away.

Any short explanation for the difference? ESP32 as the serial bridge or something?

Exactly. By default the R4 WiFi uses the ESP32 as USB-serial converter.

Using while(! Serial)
It is working fine.
But can you explain in simple words, why i use this in r4 and not need in r3.

I suggest you do your own research and learn why. Beginning with the Arduino Serial reference page would not go amiss.

Thanks sir.

working fine sir.
thanks a lot sir.

In the UNO R3, the UART handles serial communication by transmitting bytes without concern for the receiver’s status or whether anyone is listening.

The UNO R4 communicates over USB, which involves a negotiation process between the computer and the Arduino when the connection is established. This negotiation occurs at the hardware level and takes time, meaning your Arduino code must wait until the Serial connection is ready before using it.

That’s what the line

while(! Serial) ;

It polls the status of the Serial line and returns true when the link works. You can keep it on the R3 and it always says true right away, on the R4 - as explained - it will take a bit of time.

And the conversion to USB is done by an external chip that holds the USB connection even if the MCU is reset.