I’ve got several Nanos, some work at 115K for Serial; others only 38K4. I’ve also got a few other MCUs that run at various lower speeds.
So I connect a random one to Serial Monitor and … a bunch of Klingon algebra appears on the screen. What speed do I need to connect at?
So I created a standard chunk of code to tell me …
#if defined(HardwareSerial_h) // // HardwareSerial defined in .arduino15/packages/arduino/hardware/avr/1.8.6/cores/arduino/HardwareSerial.h
void printSwitchBaudrateMessage(HardwareSerial* pSerial, long bps){ // print automated message to switch BAUDRATE
if(pSerial){
pSerial->end();
pSerial->begin(bps);
while(!(pSerial)){} // for Leonardo, Micro etc.
while(pSerial->available()){ pSerial->read(); } // empty any spurious input chars
pSerial->flush(); // wait for any spurious out chars to be transmitted
pSerial->print(F("\n(@"));
pSerial->print(bps, DEC);pSerial->print(F(") Switch to BAUDRATE "));
pSerial->println(BAUDRATE, DEC);
pSerial->flush(); // wait for any spurious out chars to be transmitted
delay(100);
pSerial->end();
}
}
void printAutomatedSwitchBaudrateMessage(HardwareSerial pSerial){
printSwitchBaudrateMessage(pSerial, 9600);
printSwitchBaudrateMessage(pSerial, 19200);
printSwitchBaudrateMessage(pSerial, 38400);
printSwitchBaudrateMessage(pSerial, 115200);
}
#else
void printAutomatedSwitchBaudrateMessage(Print* pPrt){
pPrt->print(F("Looks like BAUDRATE would be ")); pPrt->println(BAUDRATE, DEC);
}
#endif
void setup(){
delay(500); // wait for stability on some boards to prevent garbage Serial. 100mS too short
printAutomatedSwitchBaudrateMessage(&Serial);
Serial.begin(BAUDRATE); // Serial defined in .../cores/arduino/HardwareSerial.h
...
I guess you didn’t buy a batch of Chinese Nano clones with an old bootloader that drop Serial bits even at 57K6 baud.
I’ve also got an old alarm system with firmware that used to have a version sticker on it. The supplier asked what version it was on the sticker. I told him that the sticker had fallen off. If only there was some way for the firmware itself to tell me …
I really like self-documenting programmes, so when my Arduino powers up, it tells me things like …
I recall that in the past on the forum one or two sensors had an obscure baud rate of 70000-80000 something. However the Serial was not used simultaneously for user interaction. And yes , MIDI uses 31250 baud.
I have lots of good and crappy stuff in my deawers
The UART is hardware - bootloader you can change…
Also with your code printing at various baud rates - how do you handle that in the terminal ? You set a baud rate, it will just read correctly what is sent at that baud rate…
You could, in a loop, emit the same message at a series of baud rates, perhaps a table in memory, or in EEPROM; then, sit waiting at the desired rate for the user to type a specific message, like "Hello"; once that is received, proceed with your program.
But I think it's a poor approach. I work at one of two bauds - 115200, or 9600 IFF there is a solid reason for the slower baud rate.
YMMV, works for me.
I have a lot of NANO clones, all my sketches use either 9600 or 115200, and my monitor is set to the same. Everything works. Do I sometimes get garbage at startup, as does every board I own? When I upload, I see it is finished, click the monitor start and/or press the reset button and voila, all is good. It's been that way forever.
obviously a chicken & egg problem -- how can the Arduino report the speed on the Serial port without the port set to that speed.
i think you like the code to always come up at some speed (e.g. 300) and report the new speed specified in Serial.begin() before changing to that speed, similar to how modems use to handshake to determine a common speed
if you're deperate, use an o-scope to measure the bit period
The threshold on F_CPU is arbitrary and does not reflect the real USART baud rate error, which depends on the UBRR divider and the resulting timing error.
The claim that the old Nano bootloader “drops at 57.6 k” is misleading; 57600 is the bootloader speed, but user serial at 115200 generally works within acceptable error margins.
Choosing 38400 for the old bootloader has no clear mathematical or datasheet justification.
As long as you are happy with you voodoo approach - that’s OK - but really this makes no sense at all to me.
The offered code publishes the message about the correct baud rate at a number of baud rates. Very clever. One will print correctly, directing the choice the use must make at the serial monitor.
But noobs aren't going to be any better off, they won't find and include this code in their sketches.
It's for them just something to learn by having it happen once or twice, or for some of us a few dozen times, like any stupid error = v. ==, integer overflow, stray semicolons &c.
And ppl who know what they are doing, well, know what to do.
This is useful I suppose if you use a large number of baud rates in your sketches, can't be bothered to flip through them to find the correct one when you see garbage (or nothing) on the serial monitor and don't have a pencil to write down what each of all that vast number of variously fast projects you've strewn about runs at.
Which seems odd for someone who could come up with this self-identifying trick.
I built a Nano oscilloscope using IDE Serial Plotter for its display. That needs top speed to keep refreshing the display at a useful rate. (I also run the ADC pretty fast, too; maybe 6 bit resolution which is ‘good enough’)
I have an atmega328p chip on battery power using the 128KHz oscillator; that has to communicate at a tiny fraction of the oscilloscope speed.