How to create Constants that can be used across an Arduino project

Oscar de la Hera Gomez
A flower that represents Arduino with the text "System Constants" beneath it.

Create a new header and CPP file and define the constants. To use the constants, include the header file in your script and implement the constant.

The code for the following tutorial is found on Github through our Open Source Arduino starter project. This can be cloned using the command below:

git clone git@github.com:delasign/arduino-starter-project.git

Step One: Create the Files

A screenshot of Arduino showing you how you can create a new file by clicking "New Tab" and entering the name of the file. Please note that this must include the extension (i.e. .h or .cpp).

Create the Header and CPP file for the Constants.

Step Two: Declare the Constants

A screenshot of the Arduino IDE showing the Constants.h file where we have declared an integer constant.

Add the constants to the header file.

The only thing that you need to add to the CPP file is the #include constants.h line.

Step Three: Implement the Constants

A screenshot of the Arduino IDE showing how we have included the Constants.h file in our Arduino ino file.  We have also highlighted how we have implemented the constant by writing the variable into the script.

Wherever you wish to implement the constant:

  • Include the Constants.h script.
  • Implement the constant (i.e.SAMPLE_CONSTANT) in a function.

Step Four: Test

A screenshot of the Arduino IDE highlighting how the constant is correctly printed into the Serial monitor.

Upload the code onto your board and confirm that the constant works as expected.

Looking to learn more about things you can do with Arduino ?

Search our blog to find educational content on Arduino.

Any Questions?

We are actively looking for feedback on how to improve this resource. Please send us a note to inquiries@delasign.com with any thoughts or feedback you may have.
delasign logo

Book a Free Consultation.

An icon of an email.

Click here to email us.

Fill in the details below to book a free consultation or to let us know about something else. Whatever it is, we are here to help.

How can we help you ?

Contact Details