how to change baud rate at run time in arduino

Hello,

Do anybody know how to change Serial baud rate at run time in arduino?

Thank You

Serial.begin(newBaudRate);

@Westfw,

I have tried that, but I am unsuccessful. Specifically, I declared "Serial.begin(baud rate)" in setup function.
Then I want to change baud rate, so I declared "Serial.begin(new baud rate)" in loop, but still I am unsuccessful. The program is working only with the baud rate declared in setup function.

Thank You.

did u try to change in serial monitor ?

Yes, I have changed it in serial monitor, but still its not working.

Have you tried Calling Serial.end() before switching (flushes buffers)

What else are you doing in the code we don’t see ?

How do you actually test the new baud rate ?

when you come back here , please have your updated code posted, with code tags

@J-M-L,

Yes, I have tried using Serial.end() as well.

My code is as follows:

void setup() {
  Serial.begin(2400);
  pinMode(seg_a, OUTPUT);
  pinMode(seg_b, OUTPUT);
  pinMode(seg_c, OUTPUT);
  pinMode(seg_d, OUTPUT);
  pinMode(seg_e, OUTPUT);
  pinMode(seg_f, OUTPUT);
  pinMode(seg_g, OUTPUT);
  pinMode(seg_dp, OUTPUT);
  pinMode(seg_a1, OUTPUT); // configure all pins used to outputs
  pinMode(seg_b1, OUTPUT);
  pinMode(seg_c1, OUTPUT);
  pinMode(seg_d1, OUTPUT);
  pinMode(seg_e1, OUTPUT);
  pinMode(seg_f1, OUTPUT);
  pinMode(seg_g1, OUTPUT);
  pinMode(seg_dp1, OUTPUT);
  pinMode(com1, OUTPUT);
  pinMode(com2, OUTPUT);
  pinMode(com3, OUTPUT);
  pinMode(com4, OUTPUT);
  pinMode(com5, OUTPUT);
  pinMode(com6, OUTPUT);
  pinMode(com7, OUTPUT);
  pinMode(com8, OUTPUT);
  pinMode(com9, OUTPUT);
  pinMode(com10, OUTPUT);
  pinMode(com11, OUTPUT);
  pinMode(com12, OUTPUT);
  pinMode(com13, OUTPUT);
  pinMode(com14, OUTPUT);
  pinMode(com15, OUTPUT);
  pinMode(com16, OUTPUT);
  pinMode(com17, OUTPUT);
  pinMode(com18, OUTPUT);
  pinMode(com19, OUTPUT);
  pinMode(com20, OUTPUT);
  pinMode(com21, OUTPUT);
  
}

void loop() {

  while (Serial.available() > 0) {
    xString = Serial.readString();
    ////Serial1.println(xString);
    xString.toCharArray(iArray, 60);
    //Serial.println(iArray);
    char *str;
    char *p;
    strncpy(stringbuffer, iArray, 50);

  }

  switch (stringbuffer[0])
  {
    case '

Firstly, in setup, Serial.begin(2400) is declared. After that, in loop, a switch case is declared in which new baud rate is sent from serial monitor and then the declaration Serial.begin(new baud rate);:
     ptr = NULL;
     ptr = strchr(stringbuffer, '


Firstly, in setup, Serial.begin(2400) is declared. After that, in loop, a switch case is declared in which new baud rate is sent from serial monitor and then the declaration Serial.begin(new baud rate););
      memset(Id, '\0', sizeof(Id));
      memset(Type, '\0', sizeof(Type));
      j = 1;
      while (ptr[j] != ',')
      {
        Id[strlen(Id)] = ptr[j];
        j++;
      }
      j++;
      while (ptr[j] != '|')
      {
        Type[strlen(Type)] = ptr[j];
        j++;
      }
      j++;

      display_code(Id);
      display_type(Type);
      break;
    case '^':
      ptr = NULL;
      ptr = strchr(stringbuffer, '^');
      memset(Fat, '\0', sizeof(Fat));
      memset(SNF, '\0', sizeof(SNF));
      memset(Water, '\0', sizeof(Water));
      memset(CLR, '\0', sizeof(CLR));
      memset(Rate, '\0', sizeof(Rate));
      j = 1;
      while (ptr[j] != ',')
      {
        Fat[strlen(Fat)] = ptr[j];
        j++;
      }
      j++;
      while (ptr[j] != ',')
      {
        SNF[strlen(SNF)] = ptr[j];
        j++;
      }
      j++;
      while (ptr[j] != ',')
      {
        Water[strlen(Water)] = ptr[j];
        j++;
      }
      j++;
      while (ptr[j] != ',')
      {
        CLR[strlen(CLR)] = ptr[j];
        j++;
      }
      j++;
      while (ptr[j] != '|')
      {
        Rate[strlen(Rate)] = ptr[j];
        j++;
      }
      j++;
      display_code(Id);
      display_type(Type);
      display_fat(Fat);
      display_snf(SNF);
      display_water(Water);
      display_slr(CLR);
      display_rate(Rate);
      break;
    case '#':
      ptr = NULL;
      ptr = strchr(stringbuffer, '#');
      memset(Qty, '\0', sizeof(Qty));
      memset(Amount, '\0', sizeof(Amount));
      j = 1;
      while (ptr[j] != ',')
      {
        Qty[strlen(Qty)] = ptr[j];
        j++;
      }
      j++;
      while (ptr[j] != '|')
      {
        Amount[strlen(Amount)] = ptr[j];
        j++;
      }
      j++;

      display_code(Id);
      display_type(Type);
      display_fat(Fat);
      display_snf(SNF);
      display_water(Water);
      display_slr(CLR);
      display_rate(Rate);
      display_quantity(Qty);
      display_amount(Amount);
      break;
    case '@':
      ptr = NULL;
      ptr = strchr(stringbuffer, '@');
      memset(Baud, '\0', sizeof(Baud));
      j = 1;
      while (ptr[j] != '|')
      {
        Baud[strlen(Baud)] = ptr[j];
        j++;
      }
      j++;
      Baudrate = atol(Baud);
      Serial.println(Baudrate);
      Serial.flush();
      delay(2);
      Serial.end();
      delay(500);
 
  //Start the Serial port and wait for it to initialize
     Serial.begin(Baudrate);
     delay(3000);
     // setup();
    //  memset(stringbuffer, '\0', sizeof(stringbuffer));   
      break; 
    case '*':
      ptr = NULL;
      ptr = strchr(stringbuffer, '*');
      memset(Date, '\0', sizeof(Date));
      memset(Time, '\0', sizeof(Time));
      memset(MID, '\0', sizeof(MID));
      j = 1;
      while (ptr[j] != ',')
      {
        Date[strlen(Date)] = ptr[j];
        j++;
      }
      j++;
      while (ptr[j] != ',')
      {
        Time[strlen(Time)] = ptr[j];
        j++;
      }
      j++;
      while (ptr[j] != '|')
      {
        MID[strlen(MID)] = ptr[j];
        j++;
      }
      j++;

      
      display_code(MID); 
      display_texthead();
   //   display_ons();              
      display_date(Date);  
      display_time(Time); 
       
      break;

  }
}

Firstly, in setup, Serial.begin(2400) is declared. After that, in loop, a switch case is declared in which new baud rate is sent from serial monitor and then the declaration Serial.begin(new baud rate);

Can you post ALL the code? What you have is missing all the variable definitions...
Which board is this for?

Baud[strlen(Baud)] = ptr[j];

That's a weird way to add characters to string. But I guess it would work OK (assuming Baud is a char array.)

What's the new speed supposed to be? Does it print correctly (the "Serial.println(Baudrate);" )?

Are you messing with pins 0 and 1 ?

Just curious- Why?

@westfw,

Here is the code with variable declarations:

int seg_a = 2;
int seg_b = 3;
int seg_c = 4;
int seg_d = 5;
int seg_e = 6;
int seg_f = 7;
int seg_g = 8;
int seg_dp = 9;
int seg_a1 = 22; // declare the variables
int seg_b1 = 23;
int seg_c1 = 24;
int seg_d1 = 25;
int seg_e1 = 26;
int seg_f1 = 27;
int seg_g1 = 28;
int seg_dp1 = 29;
int com1 = A0;
int com2 = A1;
int com3 = A2;
int com4 = A3;
int com5 = A4;
int com6 = A5;
int com7 = A6;
int com8 = A7;
int com9 = A8;
int com10 = A9;
int com11 = A10;
int com12 = A11;
int com13 = A12;
int com14 = A13;
int com15 = A14;
int com16 = A15;
int com17 = 30;
int com18 = 31;
int com19 = 32;
int com20 = 33;
int com21 = 34;

char n;
char* ptr;
int counter = 0;
int lastIndex = 0, g = 0;
String xString, input = "", pieces[10];
char iArray[60];
char stringbuffer[50];
long x;
int sSize;
int j;
long Baudrate;
long p, q, r, s, t, u, v, w, y, z, p1, q1, r1, s1;
char cArray[60];
bool v1[10];
bool b = 1;
int dPosition;
String sNumber = "";
bool isReady = false;
char Id[6];
char Type[2];
char Fat[5];
char SNF[5];
char Water[5];
char CLR[5];
char Rate[7];
char Qty[8];
char Amount[10];
char Date[8];
char Time[6];
char MID[6];
char Baud[10];
void setup() {
  Serial.begin(2400);
  pinMode(seg_a, OUTPUT);
  pinMode(seg_b, OUTPUT);
  pinMode(seg_c, OUTPUT);
  pinMode(seg_d, OUTPUT);
  pinMode(seg_e, OUTPUT);
  pinMode(seg_f, OUTPUT);
  pinMode(seg_g, OUTPUT);
  pinMode(seg_dp, OUTPUT);
  pinMode(seg_a1, OUTPUT); // configure all pins used to outputs
  pinMode(seg_b1, OUTPUT);
  pinMode(seg_c1, OUTPUT);
  pinMode(seg_d1, OUTPUT);
  pinMode(seg_e1, OUTPUT);
  pinMode(seg_f1, OUTPUT);
  pinMode(seg_g1, OUTPUT);
  pinMode(seg_dp1, OUTPUT);
  pinMode(com1, OUTPUT);
  pinMode(com2, OUTPUT);
  pinMode(com3, OUTPUT);
  pinMode(com4, OUTPUT);
  pinMode(com5, OUTPUT);
  pinMode(com6, OUTPUT);
  pinMode(com7, OUTPUT);
  pinMode(com8, OUTPUT);
  pinMode(com9, OUTPUT);
  pinMode(com10, OUTPUT);
  pinMode(com11, OUTPUT);
  pinMode(com12, OUTPUT);
  pinMode(com13, OUTPUT);
  pinMode(com14, OUTPUT);
  pinMode(com15, OUTPUT);
  pinMode(com16, OUTPUT);
  pinMode(com17, OUTPUT);
  pinMode(com18, OUTPUT);
  pinMode(com19, OUTPUT);
  pinMode(com20, OUTPUT);
  pinMode(com21, OUTPUT);
  
}

void loop() {

  while (Serial.available() > 0) {
    xString = Serial.readString();
    ////Serial1.println(xString);
    xString.toCharArray(iArray, 60);
    //Serial.println(iArray);
    char *str;
    char *p;
    strncpy(stringbuffer, iArray, 50);

  }

  switch (stringbuffer[0])
  {
    case '

The new speed is supposed to be 9600.
Yes, "Serial.println(Baudrate);" is printing correctly.

@J-M-L,

Yes, I am using pins 0 and 1.:
      ptr = NULL;
      ptr = strchr(stringbuffer, '


The new speed is supposed to be 9600. 
Yes, "Serial.println(Baudrate);" is printing correctly.

@J-M-L,

Yes, I am using pins 0 and 1.);
      memset(Id, '\0', sizeof(Id));
      memset(Type, '\0', sizeof(Type));
      j = 1;
      while (ptr[j] != ',')
      {
        Id[strlen(Id)] = ptr[j];
        j++;
      }
      j++;
      while (ptr[j] != '|')
      {
        Type[strlen(Type)] = ptr[j];
        j++;
      }
      j++;

      display_code(Id);
      display_type(Type);
      break;
    case '^':
      ptr = NULL;
      ptr = strchr(stringbuffer, '^');
      memset(Fat, '\0', sizeof(Fat));
      memset(SNF, '\0', sizeof(SNF));
      memset(Water, '\0', sizeof(Water));
      memset(CLR, '\0', sizeof(CLR));
      memset(Rate, '\0', sizeof(Rate));
      j = 1;
      while (ptr[j] != ',')
      {
        Fat[strlen(Fat)] = ptr[j];
        j++;
      }
      j++;
      while (ptr[j] != ',')
      {
        SNF[strlen(SNF)] = ptr[j];
        j++;
      }
      j++;
      while (ptr[j] != ',')
      {
        Water[strlen(Water)] = ptr[j];
        j++;
      }
      j++;
      while (ptr[j] != ',')
      {
        CLR[strlen(CLR)] = ptr[j];
        j++;
      }
      j++;
      while (ptr[j] != '|')
      {
        Rate[strlen(Rate)] = ptr[j];
        j++;
      }
      j++;
      display_code(Id);
      display_type(Type);
      display_fat(Fat);
      display_snf(SNF);
      display_water(Water);
      display_slr(CLR);
      display_rate(Rate);
      break;
    case '#':
      ptr = NULL;
      ptr = strchr(stringbuffer, '#');
      memset(Qty, '\0', sizeof(Qty));
      memset(Amount, '\0', sizeof(Amount));
      j = 1;
      while (ptr[j] != ',')
      {
        Qty[strlen(Qty)] = ptr[j];
        j++;
      }
      j++;
      while (ptr[j] != '|')
      {
        Amount[strlen(Amount)] = ptr[j];
        j++;
      }
      j++;

      display_code(Id);
      display_type(Type);
      display_fat(Fat);
      display_snf(SNF);
      display_water(Water);
      display_slr(CLR);
      display_rate(Rate);
      display_quantity(Qty);
      display_amount(Amount);
      break;
    case '@':
      ptr = NULL;
      ptr = strchr(stringbuffer, '@');
      memset(Baud, '\0', sizeof(Baud));
      j = 1;
      while (ptr[j] != '|')
      {
        Baud[strlen(Baud)] = ptr[j];
        j++;
      }
      j++;
      Baudrate = atol(Baud);
      Serial.println(Baudrate);
      Serial.flush();
      delay(2);
      Serial.end();
      delay(500);
 
  //Start the Serial port and wait for it to initialize
     Serial.begin(Baudrate);
     delay(3000);
     // setup();
    //  memset(stringbuffer, '\0', sizeof(stringbuffer));   
      break; 
    case '*':
      ptr = NULL;
      ptr = strchr(stringbuffer, '*');
      memset(Date, '\0', sizeof(Date));
      memset(Time, '\0', sizeof(Time));
      memset(MID, '\0', sizeof(MID));
      j = 1;
      while (ptr[j] != ',')
      {
        Date[strlen(Date)] = ptr[j];
        j++;
      }
      j++;
      while (ptr[j] != ',')
      {
        Time[strlen(Time)] = ptr[j];
        j++;
      }
      j++;
      while (ptr[j] != '|')
      {
        MID[strlen(MID)] = ptr[j];
        j++;
      }
      j++;

      
      display_code(MID); 
      display_texthead();
   //   display_ons();              
      display_date(Date);  
      display_time(Time); 
       
      break;

  }
}

The new speed is supposed to be 9600.
Yes, "Serial.println(Baudrate);" is printing correctly.

@J-M-L,

Yes, I am using pins 0 and 1.

On what Arduino ? Pins 0 and 1 are very often connected to the Serial line So if you mess around with it you mess around with serial data

@J-M-L,

Arduino Mega 2560.

OK - yes that’s the case on the mega

Don’t use pins 0/1 for anything other than serial

@J-M-L,

You misunderstood me.

I am not using pins 0/1 for anything other than Serial and code upload.

Ah ok

Have you tried with a simple program which says HELLO in the setup ? How do you switch baud rate on the client side?

What behavior do you see? I can’t test right now but wonder if switching the IDE baud rate does not reboot your Arduino which then would re-establish your baud rate to 2400 (print hello in the setup this way you’ll know if you go back there)

This works with Serial Monitor set to 9600:

void setup()
{
  for (long baud = 8000; baud <= 10000; baud+=10)
  {
    Serial.begin(baud);
    delay(10);
    Serial.println(baud);
    delay(10);
    Serial.end();
    delay(10);
  }
}


void loop() {}

The result is:

<redacted>
xvyp
xwp`
xwqp
xwrp
xwsp
xwtp
xwup
8wvp
xwwp
xwxp
xwy0
x80p
x8q0
88r0
8x30
x8t0
x85p
x8v0
88wp
88x0
x890
8y00
89qp
892p
8930
8940
8950
8960
8970
8980
8990
9000
9010
9020
9030
9040
9050
9060
9070
9080
9090
9100
9110
9120
9130
9140
9150
9160
9170
9180
9190
9200
9210
9220
9230
9240
9250
9260
9270
9280
9290
9300
9310
9320
9330
9340
9350
9360
9370
9380
9390
9400
9410
9420
9430
9440
9450
9460
9470
9480
9490
9500
9510
9520
9530
9540
9550
9560
9570
9580
9590
9600
9610
9620
9630
9640
9650
9660
9670
9680
9690
9700
9710
9720
9730
9740
9750
9760
9770
9780
9790
9800
9810
9820
9830
9840
9850
9860
9870
9880C⸮9⸮Nj
9900
9NLj
9920
⸮9⸮&j
⸮Nj
9⸮Mj
⸮N*
9Nj
9⸮⸮*⸮9Nj
1⸮⸮

johnwasser:
This works with Serial Monitor set to 9600:

void setup()

{
 for (long baud = 8000; baud <= 10000; baud+=10)
 {
   Serial.begin(baud);
   delay(10);
   Serial.println(baud);
   delay(10);
   Serial.end();
   delay(10);
 }
}

void loop() {}




The result is:


~~~
<redacted>

xvyp
xwp`
xwqp
xwrp
xwsp
xwtp
xwup
8wvp
xwwp
xwxp
xwy0
x80p
x8q0
88r0
8x30
x8t0
x85p
x8v0
88wp
88x0
x890
8y00
89qp
892p
8930
8940
8950
8960
8970
8980
8990
9000
9010
9020
9030
9040
9050
9060
9070
9080
9090
9100
9110
9120
9130
9140
9150
9160
9170
9180
9190
9200
9210
9220
9230
9240
9250
9260
9270
9280
9290
9300
9310
9320
9330
9340
9350
9360
9370
9380
9390
9400
9410
9420
9430
9440
9450
9460
9470
9480
9490
9500
9510
9520
9530
9540
9550
9560
9570
9580
9590
9600
9610
9620
9630
9640
9650
9660
9670
9680
9690
9700
9710
9720
9730
9740
9750
9760
9770
9780
9790
9800
9810
9820
9830
9840
9850
9860
9870
9880C⸮9⸮Nj
9900
9NLj
9920
⸮9⸮&j
⸮Nj
9⸮Mj
⸮N*
9Nj
9⸮⸮*⸮9Nj
1⸮⸮

Interesting! It seems like it’s reliable between 8930 and 9870 (9880 is missing a newline), which is about -7% to +3%. I take this to mean that the Arduino is running about -2% too slow (halfway between the two), aka 2% too fast? (Or at least it’s running 2% faster than your USB-to-serial converter.)

The display is on the console side and is set at 9600, so what it says is that the PC serial driver copes with approximative baud rate for a while - it does not say anything about the Arduino side