Change Serial1 baud rate during run time

Is it possible to change the baud rate of the software serial (Serial1) during run time?

I need to change it from 38400 to 9600 for one of my UART sensors.

I was thinking something like but it didn’t seem to work. Any ideas? Thanks in advance

setup()
{
   Serial1.begin(38400);
}

loop()
{
   blah blah
   Serial1.flush();
   Serial1.end()
   Serial1.begin(9600);
   blah blah
}

I’ve edited your post to properly format the code. Please check out this post, so you know how to do this yourself in the future. Thanks in advance! ~Jordy

@robertral, looking at the master firmware repo code, the code tried should work. When you say it didn’t “seem” to work, how did you test it?

@peekay123 thanks for the prompt reply. I will have a look this evening and get back to you with the result (I fear I might have misconnected one of the sensors, hence why it didn’t seem to work).

Last time I checked, flush() did nothing…

A work around is while(Serial1.available()) Serial1.read();

1 Like

@BDub, for Serial1 and 2, flush() actually flushes! It is Serial.flush() that does nothing. :wink:

1 Like

Cool thanks for the feedback! There are clearly lots of things to keep track of :slight_smile: