ESP32 / ESP8366 Arduino: Creating a simple class

The objective of this post is to explain how to create a very simple class and instantiate an object of it in the Arduino core.

This code was tested on both the ESP32 and the ESP8266. The ESP32 tests were performed using a DFRobot’s ESP-WROOM-32 device integrated in a ESP32 FireBeetle board.


The objective of this post is to explain how to create a very simple class and instantiate an object of it in the Arduino core.

Please note that this is just a simple tutorial on how to create a class, since object oriented programming (OOP) is a subject too broad to cover in just one post.

Nonetheless, learning OOP gives us a set of tools that will allow to write much cleaner and sophisticated code and its uses extend far beyond the scope of microcontrollers.

Furthermore, it allows us to be able to more easily analyse the source code of the many Arduino libraries out there, giving the possibility to understand what is happening under the hood and perform changes according to our needs.

Other important thing to mention is that the concepts we are covering here are from the C++ language, since Arduino is built on top of this language [1]. Besides that, although these tests were performed on ESP family devices, the code should work fine for any regular Arduino board.

This code was tested on both the ESP32 and the ESP8266. The tests were performed using a DFRobot’s ESP-WROOM-32 device integrated in a ESP32 FireBeetle board.

The code

Although it is a good practice to keep our classes in separate files and then import them where needed (pretty much like we do with Arduino libraries), we will declare ours in our main code file, for simplicity.

To declare our class, we use the class keyword followed by the name we want to call it. Then we enclose the class definition inside brackets. Note that as convention, classes are named with the first letter as uppercase.

class TestClass{
  // Class definition

Our class will have two data members, which will be integers. We will initialize them with some arbitrary values, just for illustration purposes.

But before we declare these class data members, we need to take a look at the concept of visibility. Visibility allows us to control the entities that can access our class members.

The concept of visibility is very important because we may have members of our class that should not be externally accessed or modified in order for it to keep working as intended. Thus, we can protect those members from being externally accessed and only expose the ones that make sense.

For example, if our class implements a very complex algorithm that needs a lot of internal configurations to work correctly, its wise that we don’t allow the modification of those data members, and thus they can be kept private. On the other hand, the members that are needed for passing the input values and getting the output values need to be accessible, and thus should be kept public.

In our case, to keep things simple for now, we will allow the access to our class members for both reading and writing. Thus, we use the public keyword followed by colon, before declaring the 2 previously mentioned integers.

Note that the keywords used for specifying the visibility are called access modifiers [2].

class TestClass{
    int x = 1;
    int y = 2;

Now that we have declared and defined our class, we are going to move to the remaining code. We will test our class in the Arduino setup function. But first we will open a serial connection to later get the test results.


Next we will instantiate our class, which corresponds to creating an object of that class. Since we declared no constructor, we can simply use the name of the class followed by our object name and the default constructor (takes no arguments and has empty body) is implicitly called [3].

TestClass myObject; 

Since we assigned default values to our data members, we can check their value using our object. So, to access a data member, we simply use the dot operator on our object followed by the data member name.


We can also assign a different value to the class data member by accessing it with the dot operator and then performing the assignment with the equal operator, like we would do with a regular variable.



Note that although this works well, it’s also a valid approach to define data members than need to be exposed as private and then access / modify them with setters and getters.

Other important note is that the data members of our object are not shared with other objects we may create from the same class. So, if we created another object of the same class, both x and y would have the value 1 and 2, respectively, on that object.

The full source code for this tutorial can be seen below.

class TestClass{
    int x = 1;
    int y = 2;

void setup() {

  TestClass myObject;

  Serial.print("Data member x value after creating object:");

  Serial.print("Data member y value after creating object:");


  Serial.print("Data member x value after assignment:");


void loop() {}

Testing the code

To test the code, simply upload it to your device using the Arduino IDE and then open the serial monitor. You should get an output similar to figure 1.

ESP32 ESP8266 Arduino create simple class.png

Figure 1 – Output of the program. Taken from the tests on the ESP32.

As expected, we first get the values we assigned when initializing the data members and then, after changing the value o x, we get that new value upon printing it.






4 Replies to “ESP32 / ESP8366 Arduino: Creating a simple class”

  1. Hi, excellent introduction to objects. I was wondering which classes and objects are default available in Arduino and how I can find and check them. For example String class is a default available class? I also not understand why/when/where the Serial object is made. Could you please explain which classes and objects are available default? Why we not need to make objects like Serial ourselves? And are there other objects made already? Thanks a lot again!!! Wim ( Antwerp, Belgium)

    Liked by 1 person

    1. Hi!

      Thank you very much for the feedback 🙂

      Yes, the String class is available by default.

      I’m not sure if there is a centralized place that lists all the functionalities that are available by default, but one good place to look around is the Arduino documentation:

      It should help you understand some of the functionalities that should be available by default,

      Note however that this documentation I’ve pointed is for the “official” Arduino boards.

      In this case, with the ESP32 / ESP8266, we are working with the Arduino core for these devices and thus I’m not sure if all the features listed in the Arduino reference were implemented and behave the same way.

      What I usually do is looking through the actual implementation of the Arduino core, on the GitHub page os the projects:

      Depending on how comfortable you feel with C++, this might be easier or harder.

      Nonetheless, I really think that there’s a huge benefit in trying to understand better what is happening under the hood in the implementation of the Arduino core functionalities.

      This is specially true for a device such as the ESP32, where the Arduino core is built on top of another Framework called IDF.

      Naturally, in order to be more productive, I recommend to first try to understand the basic concepts of object oriented programming and C++.

      In case of the Serial class, you can learn more about it here (header file):
      and here (implementation file):

      As far as I recall, the Serial object that we use is an extern variable:

      Hope this helps 🙂

      Best regards,
      Nuno Santos


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s