how to create and add a header file

Hello friends,
how can we create and add a header file in arduino.need to call the header file as
#include "headerfile.h"
please help me

1 Like

In the IDE, you will have a little dropdown button at the right (below the magnifying glass). Click it and select 'new tab'. Under the code window, a textbox will appear (at the right) where you can type the filename.

Alternatively, close the IDE, navigate to the sketch directory for the sketch that require the .h file using tools provided by the OS (e.g. windows explorer) and create a file with the correct name. Next open the sketch in the IDE and you will have two tabs. E.g. mysketch.ino and myheaderfile.h.

Further to what sterretje said, when writing the header file, to make sure you don't get an error from trying to add the header multiple times, add an 'include guard'.
(You might already know this, but it does no harm to mention it. :slight_smile: )

#ifndef _HEADERFILE_H    // Put these two lines at the top of your file.
#define _HEADERFILE_H    // (Use a suitable name, usually based on the file name.)

// Place your main header code here.

#endif // _HEADERFILE_H    // Put this line at the end of your file.

Here's that complete project, both the *.ino file and the header, compressed in zip format and attached.
It comiles fine for both UNO and Mega2560.

LEDScroll_8x32.zip (2.28 KB)

thank you both of my friends for giving me valuable advices on right time.. thank you very much....

1 Like

ecworks:
thank you both of my friends for giving me valuable advices on right time.. thank you very much....

No problem. Glad to help.

1 Like