I'm using an Arduino Nano and trying to change the baud rate to talk with a wifi chip (HF-LP100). I've successfully changed from 11500 to 9600 but I am unable to change from 9600 to 115200. How I'm trying to do it
Edit: So I'm not quite sure why but it seems to be working correctly now. I put Serial.flush(); back in and delay(2); but it didn't work with these earlier...so code is like this
Serial.flush();
delay(2);
Serial.end();
delay(500);
//Start the serial port and wait for it to initialize
Serial.begin(115200);
delay(3000);
//code to change wifi chip is here already know this works
Serial.flush();
delay(2);
Serial.end();
delay(500);
Serial.begin(9600);
delay(3000);
}
You don't need to use the Serial.end() function, Just do a Serial.flush() then Serial.begin(newBaud);
The delays are not need either.
The problem you are probably having, is that when you issue Serial.end() it releases the TX pin. depending on how you had previously configured pin 1, it could have defaulted back to an INPUT pin. Since it is connected to another input it would float. Probably falling to 0v which is interpreted as a START bit by whatever it is connected to, then you reinit the Serial hardware. Which configures pin1 as an OUTPUT, who's idle state is HIGH. So your device see a start bit followed by a 0xff, no Stop bits, incorrect parity bit. This probably cause the attached device to enter an error cycle.
That is why your device sometimes does not respond correctly, you are sending it garbage while you are changing baud rate.
When you want to change Baud rate just do this:
Serial.flush(); // wait for last transmitted data to be sent
Serial.begin(newBaudRate);
while(Serial.available()) Serial.read(); // empty out possible garbage from input buffer
// if the device was sending data while you changed the baud rate, the info in the input buffer
// is corrupted.
Don't use Serial.end() unless you want to use the TX, RX pins for something other than Serial.
Chuck.