I like having serial communication for debugging and testing purposes but after a while it takes away too much speed from the sketch.

Is it possible to have the Arduino ignore serial.print and serial.println throughout my code, without turning it into a comment or placing every serial printing inside for example "if(debug == true)" statements?

Thanks in advance.

3 Answers 3


If you insist on top performance, the best thing would be to use a macro for that:

#define Sprintln(a) (Serial.println(a))

Then instead of

Serial.println(F("Hello world!"));


Sprintln(F("Hello world!"));

etc. To deactivate the Serial printing, define the macro empty:

#define Sprintln(a) 

This will have the preprocessor remove all debugging code defined with Sprintln from your code.

(Of course, there's a huge number of variations on this theme.)

  • 1
    You can also wrap all exiting prints in #ifdef ENABLE_PRINT and #endif and then define or comment out #define ENABLE_PRINT at the top of ur file
    – benathon
    Apr 5, 2015 at 12:20
  • 1
    You can even set a debug level and define macro's for debugL1();, debugL2(); etc, so you can choose how many details you want to "log". The macro's will though make it unable to change runtime, but at the advantage of not having runtime overhead.
    – Paul
    Oct 4, 2016 at 20:04

You could, for example, use the preprocessor to change all Serial in your code.

// disable Serial output
#define Serial SomeOtherwiseUnusedName
static class {
    void begin(...) {}
    void print(...) {}
    void println(...) {}
} Serial;

Yet another solution is to implement a dummy Serial device. That would be a class that as HardwareSerial inherits from Stream and implements the necessary virtual member functions with dummy functions.

class NullSerial : public Stream {
  virtual size_t write(uint8_t) { return (1); }
  virtual int available() { return (0); }
  virtual int read() { return (0); }
  virtual int peek() { return (0); }
  virtual void flush() {}
  void begin(unsigned long, uint8_t) {}
  void end() {}

NullSerial Serial;

As Serial is not defined as a weak symbol the application would need to use a Steam variable for output. And bind this to the HardwareSerial or the NullSerial.

#if defined(DEBUG)
Stream& trace = Serial;
NullSerial noSerial;
Stream& trace = noSerial;


This is not complete as all the HardwareSerial member functions (that the sketch uses) are needed but gives the general idea how to use OOP to solve the problem.


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.