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

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.

Oscar de la Hera Gomez
Written by Oscar de la Hera Gomez
First published on 09/28/2023 at 11:21
Last Updated on 09/28/2023 at 11:52
<p>A flower that represents Arduino with the text "System Constants" beneath it.</p>

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.

SubscribeDownload Open Source Project

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
View Repository

Step One: Create the Files

<p>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).</p>

Create the Header and CPP file for the Constants.

How to break up Arduino functionality into separate files

Step Two: Declare the Constants

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

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

<p>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.</p>

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

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

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.

Search our Blog

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.
SubscribeContact UsVisit our BlogView our ServicesView our Work

Partner with us

We would love to get to know you and see how we can help your organization with its goals and needs.
Let's Talk

Stay Informed

Get occasional updates about our company, research, and product launches.
Subscribe