JM9 XRCU Tutorials 0.0.1a
Learn how to program XRCU
Loading...
Searching...
No Matches
Serial Monitor

Serial monitor is a very popular tool in Arduino programming. It is the default interface for programmers to 'talk' to Arduinos.

Here is a code snippet of using serial monitor to send data to your computer.

void setup() {
// Activate the serial monitor with a baud rate of 9600.
Serial.begin(9600);
}
void loop() {
// Print a string on your computer screen.
Serial.println("Hello, world!");
// Delay a little bit to prevent overflow.
delay(100);
}

Use of Baud Rate

You may be taught that the baud rate is a magical number which must be the same in the code and in the serial monitor, otherwise a bunch of random nonsense codes would appear in the serial monitor.

However, this is not the case for XRCU. XRCU uses a native USB port, so it does not depend on the baud rate. The serial monitor will work even both baud rates don't match, as long as the Serial.begin(...) function is called.

Note
Unlike traditional Arduinos, XRCU uses a native USB port instead of a Serial-USB converter.
Since the baud rate is only needed for Serial communication but not USB, the baud rate doesn't matter much here.

Yet, it is still a good idea to keep a reasonable baud rate (e.g. 9600) in the Serial.begin(...) function. Sometimes you may want to reuse the same piece of code on an Arduino and on an XRCU, so keeping a reasonable baud rate reduces the changes we need to adapt when switching the board.

Checkpoint 4.1

Now, try to upload the above code to XRCU and run it. Open the serial monitor with a different baud rate (e.g. 115200). See whether the serial monitor prints Hello, world! or a bunch of random codes. Is this the same as what you would expect to see?

Take a screenshot of the serial monitor with its baud rate and the output. Submit this screenshot to your assignment portal at checkpoint 4.1.

Pros of Native USB

Fast Transmission

For traditional Arduinos, the data exchange speed with serial monitor is largely confined to the baud rate we choose. Since we don't have a Serial-USB converter acting as a 'translator' between XRCU and your computer, data could be exchanged much faster than Arduinos.

Checkpoint 4.2

Remove the delay(100); statement in your code and run it on XRCU again. Open the serial monitor with timestamp. Observe the speed of new lines of Hello, world! being printed on the serial monitor. This is the speed an Arduino Uno could never produce, not even at its maximum baud rate of 2,000,000 bps. Could you guess how many lines are being printed every second?

Take a screenshot of the serial monitor with the output and their timestamps. Submit this screenshot to your assignment portal at checkpoint 4.2.

Cons of Native USB

Flooding

However, this instant large amount of information could flood the serial monitor. In Arduino IDE 1.X, the serial monitor could take up a large portion of your computer's memory in a very short period of time, causing the serial monitor and the IDE to lag. This is obviously a bad idea. That's why sometimes we would put delays to prevent sending too much data to the serial monitor in a short period of time.

No Reset

On traditional Arduinos, the program is restarted every time you open the serial monitor. This is because the computer sends a reset signal via the USB power cable. However, XRCU does not rely on the power from USB cable. The program continues regardless of whether the serial monitor is opened or not.

Checkpoint 4.3

Try to run the code snippet below.

int counter;
void setup() {
// Activate the serial monitor with a baud rate of 9600.
Serial.begin(9600);
// Set the counter to 0.
counter = 0;
}
void loop() {
// Print the value of the counter on the serial monitor.
Serial.println(counter);
// Increase the value of the counter by 1.
counter = counter + 1;
// Delay 0.01 seconds.
delay(10);
}

Close the serial monitor before running this code. Only open the serial monitor after you started running this code. Disable auto-scroll in serial monitor. Observe the first number being printed. Is it 0? or is it a number larger than 0? Is this the number you expected to see in the first line?

Take a screenshot of the serial monitor with the first few lines of output. Submit this screenshot to your assignment portal at checkpoint 4.3.

Serial Monitor Detection

Sometimes we don't want to miss any data from XRCU. For this, our program would need to wait for us to open the serial monitor. Fortunately, this could be easily achieved.

We could simply add a line while (!Serial) {} after Serial.begin(...); to wait for the user to open serial monitor on their computer. You could imagine Serial as giving false when the user hasn't opened the serial monitor yet, and turns true after the user has opened the serial monitor.

Checkpoint 4.4

Try to run the code snippet below.

int counter;
void setup() {
// Activate the serial monitor with a baud rate of 9600.
Serial.begin(9600);
// Newly added:
// Wait for the user to open serial monitor on computer.
while (!Serial) {}
// Set the counter to 0.
counter = 0;
}
void loop() {
// Print the value of the counter on the serial monitor.
Serial.println(counter);
// Increase the value of the counter by 1.
counter = counter + 1;
// Delay 0.01 seconds.
delay(10);
}

Close the serial monitor before running this code. Only open the serial monitor after you started running this code. Disable auto-scroll in serial monitor. Observe the first number being printed. Is it 0? or is it a number larger than 0? Is this the number you expected to see in the first line?

Take a screenshot of the serial monitor with the first few lines of output. Submit this screenshot to your assignment portal at checkpoint 4.4.

Previous Next
Hardware Setup Digital I/O