How to use C++ Classes in Arduino IDE without creating a Library

If you looked inside an Arduino Library you would see filename extensions such as .h and .cpp, which makes us conclude that the Arduino IDE uses C++ for its codes. If you look inside those files most of the Arduino Libraries have C++ classes inside of them. The question now is: Can you use C++ Classes in Arduino IDE without Creating a Library?

The answer to that is YES!

It is a bit tricky though as there are rules you need to follow. I shall dive into that later.

Sample Code

Below is a short Arduino Code with a C++ Class without using a Library.

Okay, I admit that was not very short. At least you can see that I have used a C++ Class without creating a library. You can compile it in your Arduino IDE if you still do not believe me. You can see the Circuit Diagram somewhere below, there is also a Fritzing Diagram if you want to seriously test it.

You can check the code in my Github here.

What the code does is almost the same as the Blink Code (File >> Examples >> 01.Basics >> Blink) but what it does differently is that it lets you Blink many LEDs with different On and Off times simultaneously.

I will not tell you how to create C++ Classes in this post, I shall write another post about it, but I will point out some rules you need to follow for your C++ Classes to compile properly in the Arduino IDE.

2 Rules to follow when using Classes in Arduino IDE

1. Class Definitions must be done before using the Class as a variable

Unlike functions, you cannot define a Class after you use it. Functions can, but classes cannot.

This will NOT compile. (Github differentBlinkersNonCompileExample1)

  1. setup()
  2. loop() – uses a variable of Class Blinker to access a class function
  3. Definition of Class Blinker
  4. Variable declaration of Class Blinker

This will throw the error: differentBlinkers:19: error: 'blink1' was not declared in this scope

Nor will this compile. (Github differentBlinkersNonCompileExample2)

  1. Variable declaration of Class Blinker
  2. setup()
  3. loop() – uses a variable of Class Blinker to access a class function
  4. Definition of Class Blinker

This will throw the error: differentBlinkers:14: error: 'Blinker' does not name a type

The correct sequence should be like this.

  1. Definition of Class Blinker
  2. Variable declaration of Class Blinker
  3. setup()
  4. loop() – uses a variable of Class Blinker to access a class function

2. Using Tabs is possible

I have been experimenting with Arduino IDE Tabs and noticed that it compiles in the order of Left to Right.

Github Code – differentBlinkersTab.

What that means is that the Arduino IDE will compile the tabs on the above photo in the order of

differentBlinkers First to be compiled
BlinkerClass Second to be compiled
_main Last to be compiled


What I do is that I put my setup and loop function on the _main. I use specifically the underscore (_) in the tab name so that it would be forced to go to the rightmost side of the tab list. This is also where I declare variables where I use my defined class.

If the Class Definition is made on the tab to the right of where the setup or loop function is located (or where the Class Variables are declared and used) the compile will fail.

Those are the two rules that I find that limits the usage of Classes in the Arduino IDE without creating a library.

Below is the diagrams for you to test the code that I have done. There is also a video that the program is working.

Circuit Diagram

Fritzing Diagram

Video of the differentBlinkers.ino at work

2 thoughts on “How to use C++ Classes in Arduino IDE without creating a Library”

  1. Hallo,
    Thanks for the nice class solution, I copied your sketch and it is working fine. I like to modify the times “ON” and “OFF” which are fixed now in the initiate statements:
    Blinker blink1 = Blinker(PIN_LED1, 500, 500);
    Blinker blink2 = Blinker(PIN_LED2, 1000, 1000);
    Blinker blink3 = Blinker(PIN_LED3, 2000, 2000);
    Blinker blink4 = Blinker(PIN_LED4, 1000, 2000);
    My intention is to modify these times through a “mqtt publish message”, so I have to give the blink1.check() statement the parameters of the “ON” and “OFF” times. The PIN_LED1 can be an integer because it is a fixed address belonging to blink1 instance.
    I tried a lot but did not succeed, often I had the compile message:
    ‘ no matching function for call to ‘Blinker::check(…….)’

    Are you able to help me out to solve this question,.
    Thanks in advance for your reply
    best regards
    Piet Deen

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.