My Own Arduino Library

Writing Arduino Libraries

J3
Jungletronics
4 min readJun 27, 2018

--

Let’s go through the process of setting up a simple Arduino library.

Once installed in the correct location, your new library and associated examples should be accessible from within the Arduino IDE.

This is a very limited introduction to Arduino Libraries; the focus here is the discover the applicable techniques to comply with the methodology outlined if we want to create or even effectively use Arduino libraries.

A candidate for the library is the startup methods of the Grove — Serial Camera Kit that I have in my collection, but for now, let’s start with a blinking hello world ;)

Lessons are From this book:

This is an amazing book

Step#01 — Create a file folder named LED in the libraries folder of the Arduino installation;

Mine is in C:/ Arduino/Libraries/LED.

This is the directories we must have inside Arduino/libraries folder

Step#02 — Create the Library Header File LED.h inside; The header file is where you store the definitions needed to use the library in your sketches.

If the Arduino software version is less than 100, then the older Program.h file is included.

In the public section of the class definition, you see prototypes of five functions. The first function is considered a constructor because it shares the name of the class, LED. This is one special method. You can have more than one constructor for a class as long as they all have a different signature — that is, a different combination of parameters or types of parameters. The constructor function is special in that it doesn’t specify a return type.

Note this:

You wrap up the header file by enclosing it within a conditional compiler directive to prevent recursive inclusion. You do this by checking to see if a compiler symbol named LED_h has already been defined. The #ifndef is super-short for “if not defined.” It shouldn’t be unless this header file has been included somewhere upstream. It shouldn’t be, and by making this check you prevent it from being duplicated. Best practicing!

Step#04 — Now create LED.cpp file; it is where you implement the methods nominated in .h file:

Your constructor is called LED::LED().

The first part is the name of the class (LED), the middle part (double colons) is the scope resolution operator, and the last part is the name of the function. Because the name of the function is the same as the name of the class, this tells the compiler that this is the class’s constructor.

In your example constructor, a single byte parameter named pin is passed to the function, indicating the pin that is connected to the LED you want to control.

Prefixing private members with an underscore is a convention to help you remember who can see what, and when.

The remainder of the functions in the class definition are public in scope, meaning they can all be called directly from your sketches.

Step#05 — Using the LED Library; Create Arduino/libraries/LED/examples folder; new Arduino file and name it NewLED:

And another one, named ToggleLED;

You don’t have to tell it which pin to use, because the object knows best and remembers which pin you specified when it was constructed. The object name is led, the period is the member of the operator, and both on() and off() are public methods defined in the class. You already know how the delay() function works.

Step#06 — Recognizing New Library Keywords

To fully integrate your library into the Arduino environment, you can also supply an optional keywords.txt file that describes the new words your library adds to the Arduino’s vocabulary.

then either KEYWORD1 for user-defined data types and classes, KEYWORD2 for methods and functions, or LITERAL for constants defined in the class. Your example LED library doesn’t use any unique literals, whereas the built-in Arduino functions like pinMode(), for example, use INPUT and OUTPUT as literals.

So that’s it for this post!
I hope you enjoyed it,
And I hope you found the project interesting.

Bye for now!

References & Credits

Google Image Result for…
Edit descriptionwww.google.com.br

Arduino Internals | Dale Wheat | Apress

Your focus determines your reality.

— Qui-Gon Jinn — Star Wars

reviewed: Oct/2020

--

--

J3
Jungletronics

😎 Gilberto Oliveira Jr | 🖥️ Computer Engineer | 🐍 Python | 🧩 C | 💎 Rails | 🤖 AI & IoT | ✍️