Often I put a bunch of Serial.print( F("debugging messages") ) in my Arduino sketch.

Normally those messages go up a USB cable to a host PC so those messages can be seen on the serial monitor.

I want to install this Arduino as part of a stand-alone system far from any PC and plug the "host" end of the USB cable into a wall-wart USB charger.

Without anything to accept those messages, the buffer on the Arduino eventually fills up, right? Do I need to do anything to keep the Arduino from overflowing and crashing when it's not connected to a host PC?

Does it make any difference if I'm using a (1) "single-chip" Arduino-compatible where the processor includes on-chip USB, vs. (2) an Arduino-compatible with separate "USB interface chip" and "processor chip"? (By "single-chip Arduino-compatible", I mean things like the Arduino Leonardo, Arduino Yún, LilyPad Arduino USB, Arduino Micro, the Teensy-LC, etc.)


It is OK to write out serial data with nothing connected.

In case (2) "Arduino + USB interface chip", the arduino has no idea if anything is connected or listening to the serial port. The code will transmit the bytes and act exactly the same whether plugged in to anything or not.

In case (1) "arduino with built in USB" it has some idea, but just drops bytes that are going to be transmitted when the USB port is not connected.

Hardware serial (case 2) has an output buffer, but that just makes the program a little faster. When the buffer is empty, a call to serial.{print, write} will place bytes in the buffer and move on. When it's full, Serial.{print,write} will wait until there is enough room to put the rest of the message in the buffer and move on. The hardware UART and interrupts it generates will keep taking bytes out of the buffer and transmitting them at a constant rate whether something is plugged in or not. In any case, the buffer is a fixed size and will never overflow.

As far as I can tell USB-Serial (case 1) only buffers incoming data. Since the calls to Serial.{print, write} do nothing when not connected, they may take less time than they would otherwise. If your code is really unsafe about timing, that could cause a problem, but I doubt it will.

| |
  • 3
    When it's full, Serial.{print,write} will wait - to clarify, it fills up because of speed (ie. you are printing too fast) not because there is nothing connected. – Nick Gammon Feb 20 '16 at 21:36
  • In case (1), when you say "drops bytes that are going to be transmitted when the USB port is not connected" do you mean it transmits them (maybe into nothing) through hardware TX? – ChemiCalChems Feb 2 at 16:46
  • Sorry, @NickGammon, but it seems to me that, at least on a Leonardo-based board (with in-CPU USB support), if I upload a quite complex sketch with plenty of Serial.print (tested at both 9600 and 115200), I see that: 1) if I power-up the board, with external-USB connected to a PC; everything work perfectly. And still work if I "disconnect" the cable; 2) if I power-up the board WITHOUT any cable connected, the board never enter even the main loop()! - It seems to me that when buffer "fills", everything hang. Is this an expected behaviour (...that I should circumvent at app level)? – Damiano Verzulli Jul 2 at 9:59
  • Sketches with the Leonardo style chips with onboard USB often have a loop in setup : while (!Serial.available()) { } which loops indefinitely until the serial connection is established via the USB. It will be this loop that causes it to appear to hang. On the other boards that call returns immediately and any data sent just disappears "off the end of the wire" if nothing is plugged in. You could always make that loop exit after (say) 10,000 iterations, or after a couple of seconds have elapsed. – Nick Gammon Jul 2 at 22:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.