I am using different codes which set different values of baud rate through the Serial.begin() function. Do I need to change the baud rate of the serial monitor to match the initialized value on the code I am running every time, or can I just leave the serial monitor baud rate set at the highest frequency of all my sketches (115200)?
-
2If at any point rates between receiver and sender are different the receiver will not be able to decode the data correctly. You will see strange characters after Serial.read()– Filip FranikOct 19, 2019 at 20:54
-
you can leave the serial monitor set to one baud rate ... you only need to change it if you want to be able to read the output– jsotolaOct 20, 2019 at 0:51
2 Answers
If your sketch has
...
void setup() {
Serial.begin(115200);
// ... other stuff here;
}
...
And you run the IDE serial monitor at 9600, you will get gibberish. The same happens the other way round. You need to match the monitor to the baud rate you've defined on the Serial.begin();
call.
-
Interestingly, I was not getting gibberish for having the line
Serial.begin(9600);
and having my serial monitor set to 115200. But ok, I will make sure to change the baud rate of the monitor to always match what's in the code, just in case. Thanks!– AleixOct 20, 2019 at 5:13
Sometimes yes, and sometimes no.
The majority of Arduino boards are connected to your computer through a USB connection (there are some rare outside cases that use other connections like bluetooth or RS485...). That USB connection can fall into two broad categories:
- A USB to UART adaptor that then connects to a UART on the Arduino's main MCU, and
- A direct USB connection to the main MCU
Type 1 are such boards as the Uno, Mega and Nano. Basically any board that has both a main MCU and a secondary chip that lies between the USB connection and that MCU.
In this case the baud rate in the serial monitor is communicated to the USB interface chip and tells that chip "You are to communicate with the main MCU at this speed". You also then have to have the main MCU communicate with the USB chip at that same speed. So it is imperative that those two speeds match.
In the second case this isn't true. USB itself has no real concept of "baud rate". Instead the baud rate you set is merely a piece of configuration information the device at the end of the USB connection can use for something if it sees fit (what that "something" is, is entirely up to the device). Changing that baud rate has no real effect on the communication. Boards like the Leonardo and the Mini fall into this category. They all have a "U" chip, like the ATMega32U4, which directly talk USB.
So, in short:
- If you have a "Non-U" chip based board with a USB interface chip you must match the baud rates, and
- If you have a "U" chip based board (or some other make of board with a direct USB interface) then the whole concept of baud rate is meaningless1 and it really doesn't matter what the numbers are you choose to use where.
1. 1200 baud is a special case in Arduino USB boards as it is used to trigger the bootloader for uploading new code.