Programmatically enable/disable Serial communication?

Hi everyone!

I was wondering if it was possible to programmatically enable/disable the serial UARTs on the Arduino Due outside of Setup()?

The reason I ask is that I am hoping to toggle on/off (via an external switch) an HC-05 module I have connected to my Serial3 TX/RX pins. So far, I haven’t had much success in calling Serial3.begin() and Serial3.end() outside of Setup() (i.e., somewhere in loop()).

Is there something I’m missing in the Arduino documentation where you can only call Serial.begin()/Serial.end() in Setup() and nowhere else?

Thanks in advance!

I don't think they care or even know where they are called. What happens when you try your function calls? Perhaps you have other problems in the code you are not showing.


There's nothing special about setup other than the fact that it only gets called once. Aside from that it is a regular function. There is no such thing as a function that can only be called from setup. You must have made some mistake in your code. I'm guessing from the fact that you didn't post it that you don't want our help finding it.

Why do you need to diddle with the SerialN instances?

If you turn the hardware off that the SerialN pins are connected to, SerialN.available() will never report any more data, so you'll never call the functions to read the data.

The Arduino will still be ready to read data, when you decide to turn the hardware back on.

I can't really envision a scenario where turning the connected bluetooth device on or off is a reasonable thing to do.