Arduino include file

Arduino include file DEFAULT

How do I add cpp file to an arduino project?

This answer has been tested and compiled to ensure it works. (Completed in Linux Ubuntu with the Arduino 1.8.7 IDE).

You have 2 problems.

1st: Arduino's unusual build process (described here) doesn't allow including from sub-folders in your project directory where your file for this project is located.

[UPDATE: THIS ONE MAY HAVE BEEN MY MISTAKE ONLY, NOT YOURS, when I was duplicating your code on my PC: I accidentally used instead of ]
2nd: C++ can only be used inside C++ source files, so you must change to , since is a C++ call to a C++ class's ('s) method.

To fix 1, simply change your folder structure to have everything in a single folder:

I present an alternate fix for #1 below too.

To fix 2, (this is mandatory!) make --> and (optionally, but recommended, to show it is a C++ header file) --> . Update your includes in the .ino and .cpp file now too to .

That's it! Now close the Arduino IDE, then reopen it and reopen your project, and you'll see the following new tabs show up:

enter image description here

It now compiles just fine!

First, turn on verbose compilation in the Arduino IDE: File --> Preferences --> check the box for "Show verbose output during 'compilation'".

Now, when you compile, all errors will show up in the bottom of the IDE window, as well as the exact compilation or linking commands which throw the error.

Once I fixed the folder structure, but your files were still C instead of C++ files, I saw this error:

Notice the file it failed to compile was , and that the C compiler (rather than the C++ compiler) was in use at the time.

I then opened the file to examine it and look for anything unusual about it.

Next, I used Eclipse to start tracking down includes, to see where gets pulled in (it should have been obvious to me already what the problem was, but I didn't see it yet). I found the following:

Arduino.h is found in "Arduino/Source/Arduino/hardware/arduino/avr/cores/arduino/Arduino.h". It includes . This header s the object:

HOWEVER, looking back at you'll see that is ONLY included if you are compiling with C++:

means that the headers above are ONLY included if you are compiling with C++! That's when it hit me! You're compiling a C file with the C compiler to access a C++ object. That's not ok. You must use the C++ compiler instead. Do this simply by changing to . Done.

Find your "Sketchbook location" from Arduino IDE: File --> Preferences. Mine, for example, is .

Now, go there and create a "libraries" folder. For me that would now be . Everything inside this folder is now considered an Arduino "library", and can be included. Move [do NOT use in this case] and there, like this:

Now close and reopen the Arduino IDE, then go to Sketch --> Include Library --> foo, and it will automatically add the following line for you:

The reason you can't use in this case is simply because Arduino is looking for files only when you add your library include using the menus in this way. That's a bug as far as I'm concerned, and should probably be reported to the Arduino developers. Feel free to take that on.

16 Apr. 2019:
A google search for "arduino add include path" led me to this:, where user @pert says:

In recent versions of the Arduino IDE(including 1.6.10) if you want to include libraries from the sketch folder you need to put them in a src subfolder. For example:

He then says you can include like this:

I haven't tried this, but that'd be super useful if it works. Give it a shot and let me know if it works. Be sure to tell us which OS and Arduino IDE version you are using.

  1. Additional discussion on Github here:
  2. The official Arduino Library specification here:

Arduino Libraries

Libraries are files written in C or C++ (.c, .cpp) which provide your sketches with extra functionality (e.g. the ability to control an LED matrix, or read an encoder, etc.). They were introduced in Arduino 0004.

To use an existing library in a sketch simply go to the Sketch menu, choose "Import Library", and pick from the libraries available. This will insert an #include statement at the top of the sketch for each header (.h) file in the library's folder. These statements make the public functions and constants defined by the library available to your sketch. They also signal the Arduino environment to link that library's code with your sketch when it is compiled or uploaded.

User-created libraries as of version 0017 go in a subdirectory of your default sketch directory. For example, on OSX, the new directory would be ~/Documents/Arduino/libraries/. On Windows, it would be My Documents\Arduino\libraries\. To add your own library, create a new directory in the libraries directory with the name of your library. The folder should contain a C or C++ file with your code and a header file with your function and variable declarations. It will then appear in the Sketch | Import Library menu in the Arduino IDE.

Note: for users of versions previous to 0017, libraries belong in a subdirectory of the Arduino application directory: ARDUINO/lib/targets/libraries. For version 0017, the libraries directory was moved to make them more convenient to install and use.

Because libraries are uploaded to the board with your sketch, they increase the amount of space used by the ATmega8 on the board. See the FAQ for an explanation of various memory limitations and tips on reducing program size. If a sketch no longer needs a library, simply delete its #include statements from the top of your code. This will stop the Arduino IDE from linking the library with your sketch and decrease the amount of space used on the Arduino board.

To get started writing libraries, download this test library. It should provide a basic template for creating a new library. After you've made changes to your library, in order to get it to recompile, you will have to delete the .o file generated in the library's directory.

  1. Jeep seat covers
  2. Gmc terrain denali
  3. Retinoid cancer



#include is used to include outside libraries in your sketch. This gives the programmer access to a large group of standard C libraries (groups of pre-made functions), and also libraries written especially for Arduino.

The main reference page for AVR C libraries (AVR is a reference to the Atmel chips on which the Arduino is based) is here.

Note that #include, similar to #define, has no semicolon terminator, and the compiler will yield cryptic error messages if you add one.


#include <LibraryFile.h>


Parameter Values

  • LibraryFile.h: When the angle brackets syntax is used, the Arduino IDE seaches for the LibraryFile.h in the libraries paths[1].
  • LocalFile.h: When the double quotes syntax is used, the Arduino IDE searches for the LocalFile.h in the sketch's folder[2]. If the file is not found, the Arduino IDE will searches for the file in libraries paths.


  • [1]: libraries paths includes
    • The path that contains the built-in libraries of Arduino IDE. It is located at where Arduino IDE is installed.
    • The path that contains the libraries installed via Library Manager of Arduino IDE. In Windows, it usually located at C:\Users\YOUR_ACCOUNT\Documents\Arduino\libraries.
  • [2]: sketch's folder[2] is the folder that your sketch (.ino file) is located.

Example Code

This example includes the Servo library so that its functions may be used to control a Servo motor.

#include <Servo.h>Servo myservo; // create servo object to control a servovoidsetup() { myservo.attach(9); // attaches the servo on pin 9 to the servo object}voidloop() {for (int pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees// in steps of 1 degree myservo.write(pos); // tell servo to go to position in variable 'pos'delay(15); // waits 15ms for the servo to reach the position }for (int pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees myservo.write(pos); // tell servo to go to position in variable 'pos'delay(15); // waits 15ms for the servo to reach the position }}

This example includes the pitches.h file, which is placed in the same directory with the sketch (the sketch's folder).

/* * This example code is in the public domain * Tutorial page: */#include"pitches.h"int melody[] = {NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, NOTE_B3, NOTE_C4};int noteDurations[] = { 4, 8, 8, 4, 4, 4, 4, 4};voidsetup() {for (int thisNote = 0; thisNote < 8; thisNote++) {int noteDuration = 1000 / noteDurations[thisNote];tone(8, melody[thisNote], noteDuration);int pauseBetweenNotes = noteDuration * 1.30;delay(pauseBetweenNotes);noTone(8); }}voidloop() {}


Please note: These are affiliate links. If you buy the components through these links, We may get a commission at no extra cost to you. We appreciate it.


Arduino: Header files tutorial detailed

Using an external header file

The reason for having you go look at the something like/AppData/Local/Temp/build/sketch folder was to make you think about what the build process is doing, and maybe think that you could google Arduino + Build to get more details.

The IDE copies your sketch, and stuff #included by your sketch to a build directory.

If you #include , the file header.h, and everything else in the directory it is in, will be copied to the build directory, too.

If you #include "header.h", the file header.h, and everything else in the directory it is in, will be copied to the build directory, too.

The difference between <> and "" is where the header.h file is looked for. "" looks in the current directory. <> looks in the library directories.

If the token between the <> or the "" is not found in the appropriate folder, you are not told that; nothing gets copied, though.

But, that explains why relative names are useless. A sketch directory might contain a file called header.h. It will not contain a file called ..\header.h, because ..\header is NOT a valid file name.

Note that I did NOT say that ..\header.h is not a valid NAME. I said that it is not a valid FILE NAME.

Only file names between the <> or "" are possible, as far as the IDE is concerned.

If you do not like that, you are free to not use the IDE.


Include file arduino

Including an external file in Arduino code

Hi. I'd like to be able to include external files in my Arduino code. Here's an example that works as intended:

Arduino code, saved as: 'C:\Arduino\IncludeTest\IncludeTest.ino': (note 'led' is not defined locally; the program will fail without its include file)

Include file, saved as: 'C:\Arduino\IncludeTest\Includes\Include.h': (note 'unsigned char' used rather than 'byte' because '.h' file is interpreted as c, not as Arduino code)

This program works. The only issue is that the entire path of the include file needs to be present in the #include command. Consequently, moving the Arduino program to a new folder will break the link, and it will not compile until you correct the #include path.

Is there a way to include a file using a relative link rather than an absolute one? For example:

I know the most obvious answer is to place the include file in the same folder as the main '.ino' file, so it will automatically be included. This won't help me, because all the program files located in this folder are also loaded in the IDE, and saved over when the IDE saves. I want the include file to remain independent of the Arduino IDE, and not be modified by it.

I've seen similar questions asked on a couple of threads, but I've never seen a satisfactory answer to it. Can anyone help?

2 Easy fixes to Arduino error: “.h: No such file or directory\

In this tutorial I’ll show you how to create your own Arduino library.

You’ll see, this might be much simpler than you thought! Very simply put, an Arduino library is a bunch of Cpp files that you have to organize in a certain way. Then you import it, use it, and that’s it.

Creating an Arduino library is a great way to write reusable code for your projects, and also to help other Arduino developers in their own projects.

Let’s get started!

Step 1: Write your Arduino library code

Starting code

Let’s start with a very simple program, and from that create a library step by step.

First, write this small Arduino sketch, which only has one function. The function will take 2 integers as parameters and return the sum.

You are learning how to use Arduino to build your own projects?

Check out Arduino For Beginners and learn step by step.

Get this course for FREE for 14 days! Just click on the link above.

int addTwoInts(int a, int b) { return a + b; } void setup() { Serial.begin(9600); int result = addTwoInts(4,3); Serial.println(result); } void loop() {}

Here we’ll make the addTwoInts() function a part of a library named my_library.

For now, all the code is in the same file, which isn’t really practical for separating the library from the main code.

Separating the library code from the rest

Let’s create 2 files: a Cpp and a header file.

To do that, find where your Arduino sketch is located. Usually it’s inside the “My Documents/Arduino” folder, unless you changed the default location. Also, you can find where is your current sketch file directly from the Arduino IDE: click on “Sketch” > “Show Sketch Folder”.

Arduino IDE - Show Sketch Folder

In this folder, you should already have a .ino file. If you named your project MyProgram, then the file is called MyProgram.ino, and is located inside the MyProgram/ folder.

Create 2 new files: my_library.h and my_library.cpp. Then restart your Arduino IDE, or close/open your current sketch. You should now see 3 files.

Arduino Separate Header and Cpp files

Write the following code into those 3 files.


#ifndef MY_LIBRARY_H #define MY_LIBRARY_H #include <Arduino.h> int addTwoInts(int a, int b); #endif

This is a typical header file. You start with the header guards and add your code between the and .

For the header guard’s name, usually it’s a good practice to use the name of your file in uppercase and add “_H”. For example, if you have a temperature_sensor.h file, you can use “TEMPERATURE_SENSOR_H”.

Then, don’t forget to include the Arduino library. On your main program it’s automatically added for you if you forget it, but not on other files or libraries.

Finally, you have the prototype of your function. The implementation will be done in the Cpp file.


#include "my_library.h" int addTwoInts(int a, int b) { return a + b; }

Here you just import your header file, and you implement the code for the function.

Now, let’s use this function in our sketch again.


#include "my_library.h" void setup() { Serial.begin(9600); int result = addTwoInts(4,3); Serial.println(result); } void loop() {}

You just have to include the header file and you can use the addTwoInts() function without changing your initial code.

All right, now that we have separated the code, let’s create a real Arduino library! Separating the code you want to put into your library is only the first step.

Step 2: Package your Arduino library

Let’s use the my_library.cpp and my_library.h files that we just created.

Place your library files into the Arduino libraries folder

First, you have to know where to put your library. As for your Arduino sketches, there is a specific folder for libraries.

Go again in your Arduino folder (“My Documents/Arduino” or similar), and find a folder named libraries/. If you can’t find it, you’ll have to create it. This folder path will be “My Documents/Arduino/libraries”.

In this libraries/ folder, create a new folder named my_library/. Move the my_library.h and my_library.cpp files into my_library/.

Documents/ └── Arduino/ └── libraries/ └── my_library/ ├── my_library.h └── my_library.cpp

Then, restart your Arduino IDE. Don’t forget this, as it doesn’t automatically detect changes in the file architecture.

On your main .ino code (now, in your sketch folder, you only have your .ino file again), change this line to this: . When including a header file located in your project directory, use “”. If the header file is globally installed, or in this case, from the Arduino libraries folder, use <>.

You can run your code, it should compile and work. You have successfully created your first Arduino library! Quite easy, isn’t it?

If you want to modify your library after you’ve put the files into your libraries/ folder, use any other text editor (for example Visual Studio Code). You won’t be able to direclty open those files in the Arduino IDE – unless you copy and paste the files into an existing project, modify the files, and put them back into the libraries/ folder.

Now, this library is quite basic and easy to use. But if you write something more complex, it may be more difficult for others to understand how to use it.

Remember, creating a library means that other people might actually use it!

Add examples to your Arduino library

A great way to provide a quick introduction to your library is to create a code example using your library. If you remember, when you started on Arduino, you may have used some code examples such as the famous Blink.ino, to teach you how to blink a LED.

It’s very simple to add examples to your libraries.

In the my_library/ folder, create an examples/ folder. Inside this examples/ folder, you’ll simply add a new Arduino project for each example.

For our custom library, let’s create an example to show how to use the addTwoInts() functions. In my_library/, create a folder add_two_ints/, and inside it a file named add_two_ints.ino.

Documents/ └── Arduino/ └── libraries/ └── my_library/ ├── examples/ ├── add_two_ints/ ├── add_two_ints.ino ├── my_library.h └── my_library.cpp

Here’s the code example:

/* * * Explanation of what your example does * */ #include <my_library.h> void setup() { // Setup Serial communication to print result on Serial Monitor Serial.begin(9600); // Get the sum of two integers into another integer int result = addTwoInts(4,3); // Check result Serial.println(result); } void loop() {}

Now, how to see your example from the Arduino IDE?

Simply restart your IDE, and your example is now showing in the menu!

Arduino IDE - Open Custom Library Example

You can click on it, compile it, and run it on your Arduino board. This example will make things easier for anyone who wants to learn how to use your library.

Step 3: Share your Arduino library

It’s great to have a library that you can use on your own computer, in your own programs. But the true purpose of a library is to be shared.

Export the library

To export and share your library, create an archive of the my_library/ folder, located into Arduino/libraries/.

Then, you just have to share it with other people you know, or online. You can easily find websites to host your library archives (usually a few KiloBytes). Now, the hard part is to make people get to know your library exists!

Once someone gets your library, there are 2 ways to install it on their Arduino environment (and this can be useful for you too, when you install libraries developed by other people).

You can try it on your own computer. Export your library into an archive, and place it somewhere else in your computer. Also remove the my_library/ folder. Now, if you restart your Arduino IDE, you won’t be able to use the library or open the examples. So, you’ll have to import it.

Manually import the library

Once you get the library, just 3 steps are required before you can use it:

  1. Extract the archive.
  2. Place the library folder in Arduino/libraries/.
  3. Restart the Arduino IDE.

Import the library through the Arduino IDE

This will work only if you have created a .zip archive (not .rar or other extensions).

Simply open the Arduino IDE, click on “Sketch” > “Include Library” > “Add .ZIP Library…”, and browse to find your .zip archive.

Arduino IDE - Add zip Library

The Arduino IDE will extract the archive, place the library in the Arduino/libraries/folder, and update itself so you don’t need to restart it.

Another example: a library for a LED class

The code example I provided above is really small. The purpose was to focus on the library itself and not the code.

Now, let’s use a more real-world example. We’ll use the LED class we created in another tutorial on Arduino OOP. So, this will also be an opportunity for you to work on how to create an Arduino library with classes.

Library folder structure

Documents/ └── Arduino/ └── libraries/ ├── examples/ └── blink_led/ └── blink_led.ino └── Led/ ├── Led.h └── Led.cpp

And the files:


#ifndef LED_H #define LED_H #include <Arduino.h> class Led { private: byte pin; public: // Setup pin LED and call init() Led(byte pin); // Setup the pin led as OUTPUT // and power off the LED - default state void init(); // Power on the LED void on(); // Power off the LED void off(); }; #endif

Here I have put comments on each method of the class. Documenting your code is always a good idea.


#include "Led.h" Led::Led(byte pin) { this->pin = pin; init(); } void Led::init() { pinMode(pin, OUTPUT); off(); } void Led::on() { digitalWrite(pin, HIGH); } void Led::off() { digitalWrite(pin, LOW); }

Example file blink_led.ino

/* * * This examples shows how to blink a LED * using the Led class from the Led library * */ #include <Led.h> // Use the built-in LED #define LED_PIN 13 // Create a Led object // This will set the pin to OUTPUT Led led(LED_PIN); void setup() { } void loop() { // Power on the LED led.on(); delay(1000); // Power off the LED; delay(1000); }

After writing those files and setting up the Led library folder, you can now restart your Arduino IDE and test the blink_led.ino example.

Going further with your Arduino library

In this tutorial you have seen how to create your own Arduino library, how to add examples to it, how to package it, and how to export it.

Here is what you can do next:

  • Add a license to your library. I won’t tell you which license to use here because it depends on what you want to do. Some common open source licenses: MIT, BSD, Apache, GPL, … Adding a license means that you’ll add a comment header to every files in your library, stating the license agreements.
  • Host your code on GitHub if you’re going open source. This might be a good idea to share your code, so you can get feedback from other developers and improve your library, as well as increasing the awareness around it.
  • Communicate about your library: on the Arduino forum, on your own website, etc.
  • Add more documentation and more examples. The clearer your library, the easier it will be for new users to use it efficiently and keep it in their code.

Note: before creating your own library – unless you do it for an educational purpose, first check if someone else has already published a library that meets your needs. If yes, you’ll have to check the license to see if you can use it. It’s important not to reinvent the wheel for everything you create. This way you’ll progress faster on your projects!

Categories Arduino TutorialsSours:

You will also be interested:


1172 1173 1174 1175 1176