How to create and use classes in an Arduino project
![Oscar de la Hera Gomez](https://www.delasign.com/CDN/headshots/profile.webp)
![A flower that represents Arduino with the text "Classes" beneath it.](https://www.delasign.com/CDN/images/How-to-create-and-use-classes-in-an-Arduino-project.webp)
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).](https://www.delasign.com/CDN/images/Create-New-Files.webp)
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.](https://www.delasign.com/CDN/images/Header-File.webp)
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.](https://www.delasign.com/CDN/images/CPP-File.webp)
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.](https://www.delasign.com/CDN/images/Implement-Class.webp)
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.](https://www.delasign.com/CDN/images/TEST_2023-09-28-162444_tslf.webp)
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.