How to break up Arduino functionality into separate files

Optimize your work into modular components designed for reusability.

Oscar de la Hera Gomez
Written by Oscar de la Hera Gomez
First published on 09/26/2023 at 13:52
Last Updated on 09/28/2023 at 12:26
<p>A flower that represents Arduino with the text "Break Functionality into Separate Files."</p>

Optimize your work into modular components designed for reusability.

Subscribe

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 VSCode showing you how to create a new file.</p>

In the root folder of your .ino file, create a .cpp and .h file for the functionality.

Step Two: Write the Code

<p>A screenshot of VSCode showing the functionality for the sample.cpp and sample.h files. This code is available below.</p>

Using the sample code provided below, write the functionality within the .cpp and .h files.

A | Header File

Replace SAMPLE_H with the name of your header file (i.e. BLUETOOTHFUNCTIONALITY_H if your header file is for bluetooth functionality).

Replace void helloWorld() with the functions that you will include in the .cpp file.

If you wish to breakup functionality into public and private parts, you must implement it through a class.

To learn how to do this, consult the tutorial linked below.

How to create and use classes in an Arduino project

B | CPP File

Replace sample.h with the name of your header file (i.e. bluetoothFunctionality.h).

Replace the helloWorld() function with the functions that you expressed in the header file.

Please note that you can import libraries in the sample way that you can in Arduino code.

C | Arduino File

Include the header file in your Arduino script using a line similar to that found below:

Please replace sample.h with the name of your header file (i.e. bluetoothFunctionality.h).

#include "sample.h"

Call the functions that you wrote in Part B to allow you to confirm that the system works.

Step Three: Test

<p>A screenshot of the Arduino IDE showing how the Serial.println appears in the Serial Monitor.</p>

Open the Arduino IDE, upload the code and verify that the functionality functions 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