Multiple Source Files with the Arduino IDE

Granted, the Arduino IDE isn’t conducive to heavy-duty projects, but it’s useful for quick and dirty programs.

Once you outgrow a single file and wish to break the project up into multiple files, you can do so in the Arduino IDE with multiple tabs. Each tab represents a single file stored in the same directory as the sketch file (.ino).

You create new tabs using the drop-down menu that appears by clicking on the inverted triangle above the upper right-hand corner of the editor.

Showing the Tab Menu in the Arduino 1.0.1 IDE

Note that unlike the sketch’s main .ino file, you will not have automatic access to the Arduino basic definitions. That is easily remedied by including

#include "Arduino.h"

Happy Hacking.

Library Warning

Please note that there is a quirk in the Arduino IDE that can prevent libraries from being included in the compile. The IDE requires all libraries to be #include’d in the main (.ino) file. If you do not do this, the library will not be included, and you’ll get errors during compile.

How It Works

When compiling, the IDE copies all the files to a temporary directory. It scans the .ino file for libraries, and copies the library files to the temporary directory. Finally, it does the actual compile. If you #include a library in any of the tabbed files, but not in the .ino file, the library will not be available to the parser. The IDEĀ only scans the .ino file for libraries.


6 thoughts on “Multiple Source Files with the Arduino IDE

  1. electronicMe

    I tried to use the LiquidCrystal library in a cpp tab in the arduino sketch. I added the #include statement to the cpp file and it didn’t work (‘LiquidCrystal’ was not declared in this scope)
    After hours of searching for the reason why it didn’t worked i found this blog entry. I had to add the #include statement ALSO in the main ino file, as you said.

    Thank you sooo much for this post! You saved me a lot of time :DD

  2. Gabor

    How could I include the additional ino files into the main file? Currently I cannot call any function from the main file that is not in the main file.

  3. Laurent

    Gabor, have you tried #include “myfile.cpp” in the main file, myfile.cpp being a tab in your ino project?

  4. Emma

    Hey I have a question, I’m trying to do the same thing as Gabor, I have two Arduino sketches that I would like to merge together, I added the #include “myfile.cpp” in the main sketch but it came up with an error saying it is having trouble compiling to my Arduino because the file does not exist. Is this because the sketches I am trying to merge are both .ino files? I have the second file as a tab to the main file, I’m unsure why its saying the file doesn’t exist in the directory. (I’m a bit of an Arduino noob, any help is greatly appreciated!)

    Hope my question is clear, thank you!

Leave a Reply