How to create and use classes in an Arduino project

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

Create and use reusable classes that optimize your code for modularity.

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 Class.

Step Two: Code the Header File

A screenshot of the Arduino IDE showing the header file for our Sample Manager.

Write the code for the header of the class using code similar to the one below.

Please note that Public, Protected and Private will dictate that is available to scripts that use your class.

Step Three: Code the CPP File

A screenshot of the Arduino IDE showing the code for the CPP file of the Sample Manager class.

Write the code for the CPP file of the class using code similar to the one below.

Please note syntax for Public, Protected and Private do not need to be applied in the cpp file.

Step Four: Implement the Class

A screenshot of the Arduino IDE showing how to implement the Manager into the Arduino ino file.

Implement the class into the Arduino script by:

  • Including the Header file at the top of the script.
  • Creating an object for the manager (i.e. SampleManager sampleManager;).
  • Use its variables or apply its functionality in the script (i.e. sampleManager.setup();).

Step Five: Test

A screenshot of the Arduino IDE highlighting the Serial Monitor whose logs demonstrate that the class works as expected.

Upload the code onto your board and confirm that the code 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