I'm writing a program in Arduino 0022.

Calling Serial.println works fine in my main sketch code, but when I attempt to use it in my header file "Menu.h", which is in a separate tab, I get an error:

In file included from AppController.cpp:2:
Menu.h: In constructor 'Menu::Menu()':
Menu.h:15: error: 'Serial' was not declared in this scope

How can I use Serial.println outside of sketch code?


3 Answers 3


You should not be calling functions from within header files. Header files are for defining pre-processor macros (#define) and references to variables / functions in other files.

You should be creating multiple C files and linking them together at compile time. The header file is used to tell each C file what functions and variables the other C files have.

To use multiple files in the Arduino IDE you require at least 1 header file to describe the functions that are in the other files that you want to share between them. Also, any global variables that you want to use across all files.

These definitions should be qualified with the "external" attribute.

Then you need to add one or more "pde" file which contains the actual code and variable definitions for the functions.

For instance, I have a "mouse.h" file:

extern void mouse_read(char *,char *, char *);
extern void mouse_init();

and a "mouse.pde" file:

#include <ps2.h>

PS2 mouse(6,5);

void mouse_read(char *stat,char *x, char *y)
  mouse.write(0xeb);  // give me data!
  mouse.read();      // ignore ack
  *stat = mouse.read();
  *x = mouse.read();
  *y = mouse.read();

void mouse_init()
  mouse.write(0xff);  // reset
  mouse.read();  // ack byte
  mouse.read();  // blank */
  mouse.read();  // blank */
  mouse.write(0xf0);  // remote mode
  mouse.read();  // ack

Then in my main file I have:

#include "mouse.h"

and I can call the functions that are in "mouse.pde" as if they were in the local file.

  • \$\begingroup\$ Thanks Majenko, your answer is very helpful to me as a novice C++ coder, and I'll definitely be adopting your advice. Even so, I was more interested in why the Serial class isn't available outside of the main sketch. Cheers! \$\endgroup\$
    – aaaidan
    Oct 23, 2011 at 2:41

As an alternative to @Majenko's very good answer, you could make a C++ class to encapsulate your functions and put it in the libraries folder as described in http://www.arduino.cc/en/Hacking/LibraryTutorial.

You may need to #include <Serial.h> in your class's implementation file to be able to call Serial methods. I'd be careful about doing this thoug as there are obviously side effects to calling Serial functions (read in particular). I prefer to define a method in my class that takes a char * and pass bytes from the serial interface into it from my main program, rather than having it interact with the serial interface directly.

#if defined(ARDUINO) && ARDUINO >= 100
  #include "Arduino.h"
  #include "WProgram.h"
  • \$\begingroup\$ Hi! Yeah I had a look around in the arduino library directory for Serial.h or similar, and all I could find was HardwareSerial.h, which didn't contain the definitions I needed. In the end I stumbled upon the solution of including <WProgram.h>, which seems to declare the Serial object. \$\endgroup\$
    – aaaidan
    Oct 23, 2011 at 2:43

I found a way to have the Serial class/object declared in header files/tabs:

#include <WProgram.h>  // at the top of the file

This doesn't feel super clean to me, but it doesn't seem to have any drawbacks yet.

  • 1
    \$\begingroup\$ Ah right, if you want to make sure you are Arduino 1.0 compatible, you should conditionally include that or "Arduino.h" depending on the ARDUINO compile flag see my answer for a detail. Also, the tutorial link in my answer says to always include WProgram.h in your classes \$\endgroup\$
    – vicatcu
    Oct 24, 2011 at 16:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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