Arduino Libraries! What they are, how they work and how to install them

Got Questions? Click here and post them in the Brainy-Bits Forum to get help!

OVERVIEW

If you've been following our tutorials for a while, you have seen us use Arduino Libraries in many of our tutorials.

 

Libraries make programming the Arduino much easier since they contain the code needed to control certain modules, sensors, etc...

 

But do you know how they work their magic?

Arduino Library Magic

 

In this tutorial we will explain how libraries work and the different ways to install them.

 


ARDUINO LIBRARY INTRODUCTION

When you start the Arduino IDE you get an empty page (sketch) where you can enter your code.

Arduino IDE Interface

 

You could skip using libraries and code all the needed command to move a stepper motor for example, but depending on the hardware used to control the stepper you might have to write a lot of code to make the stepper move.

 

The AccelStepper library for example (Created by Mike McCauley), works very well with the Easy Driver interface and contains all the code needed to move a stepper connected to an Easy Driver board.

 

Arduino Libraries consist of at least two files: a header file (with the extension .h) and a source file (with extension .cpp).

Arduino Library Files

 

The header file (.h) contains definitions for the library, which is a listing of everything that's inside, including commands (functions) and needed variables.

The source file (.cpp) contains the code used to make the library work.

 

If you look inside the library source file (.cpp) you can see the code used to communicate with the Easy Driver.

Arduino Library Code

 

As you can see the code is quite complicated and if you don't use this library, that code would have to be entered (by You) inside the Arduino IDE and would make it harder to debug and make sense if you are just beginning to program.

 

To access and use the code inside libraries you use what's called commands.

Arduino Library Commands

 

These commands link to a specific piece of code inside the library and execute it.  

 

You can think of this as the same as using functions inside the Arduino IDE.

 

When you create a function, it contains a bunch of code that gets executed when you enter the function name inside your code, by using a function you don't have to repeat the same code over and over, that way you save space and make your sketch easier to debug.

 

So a library is kinda of like that but external to your sketch.

 

In the next tutorial we will create our own library and go more in depth on how they work, so stay tuned...

 


HOW TO INSTALL A LIBRARY

In this tutorial we will look at three ways of installing a library.

 

- First way is to download a .zip file of the library from the creator's website or GitHub.

GitHub Arduino Library  

 

Once you've downloaded the .zip file, locate it and right-click to unzip and extract  the contents.

zip file extract

 

Now that the .zip is extracted, open the folder to see if it contains the library files or just another folder with the same name.

As you can see in this instance, the folder contains another folder with the same name.

You want to make sure that this one gets copied in the next step and not the first one, since you want to copy the folder that contains the library files and not just another folder.

 

Once you have located the right folder you can go ahead and right-click and select Copy from the drop down menu.

Arduino Library Copy

 

Then to install the library you navigate to the Library folder located inside your Arduino IDE install location.  By default it should be located at: Program Files (x86) -> Arduino -> libraries.

Arduino Library Location

 

Go ahead and right click again, this time choosing Paste, to copy the library inside the folder.

Arduino Library Paste

*note: When doing Paste, make sure you are not right clicking on a folder, if you do then the library will be pasted inside that folder.  You want to right click to the right of the window empty space to make sure you Paste the library inside the libraries folder. 

 

It's good practice to have your Arduino IDE software closed while installing a library this way.  

If you have it open then you want to close and restart it so the newly installed library gets picked up by the software and is ready for use.

 

To confirm that the library is there you can go to: Sketch -> Include library and look for it under Contributed libraries.

Arduino Library Include

 

 

- Second way is to use the Arduino IDE install .zip option to install the downloaded library for you.  

 

To do this all you have to do is go to in the top menu of the Arduino IDE and choose: Sketch --> Include Library --> Add .zip Library.

Arduino add Library

 

Then navigate to the .zip library you just downloaded are select it.

Arduino Library Download

 

The Arduino IDE will then do the rest of the installation, and you will get a confirmation message when it's completed.

Arduino Library Installation

 

Using this method you don't have to manually extract and find the right folder, the Arduino IDE will do that for you.

 

*note: Funny thing about using this method is that it will put the Library file inside a different folder than the one used in the first install option:

Arduino Library file location

 

This doesn't affect the functionality of the library, but it's good to know where the library gets installed when using this method.

 

- Third way to install a library is available if you're using a version of the Arduino IDE 1.56 or higher.

 

Basically all you have to do is go to in the top menu of the Arduino IDE and choose: Sketch --> Include Library --> Manage Libraries.  

Arduino Manage Library

 

Then search for the library by name... If found, all you have to do is select the version of the library you want to install and click : "Install". 

Arduino Library Versions

 

After a few moment, the library is downloaded and is ready to be used right away.

Arduino Library Installed

 

This option should be able to find most libraries but will not find less popular ones, in that case using the 1st or 2nd option will be the way to go.

 


USING THE LIBRARY IN YOUR CODE

Once you have installed a library it will be available to use in any sketch you create.

 

To be able to use a library you must include it at the beginning of your sketch by using the "#include" statement.

All you have to do is type: #include <name_of_library> at the start of your code.

Including Arduino Library

The name_of_library refers to the actual Filename of the downloaded library Header file (.h).

Arduino Library H file

 

Instead of typing it manually you can also navigate to Sketch -> Include Library and choose the one you want to use in the drop down menu.  The Arduino IDE will then insert the #include statement with the name of the library you selected automatically.

Arduino Library include

 

That's it!  You can now use the commands provided by the library inside your Arduino sketch.

 


TUTORIAL VIDEO



CONCLUSION

Now you should have a better understanding of how Arduino Libraries work, how to install and how to use them.

 

In the next tutorial we will be creating our own, very simple, library from scratch.

 

As always please watch the tutorial video for more information.

2 Comments

Alejandro Davila Ibarra - Feb 21, 2017

Easy to follow instructions! Got a library installed on the first try using the first method mentioned in the tutorial

Hector Chavez - Feb 21, 2017

It is relaxing to have easy explanation like these

Thank you very much

Got Questions? Click here and post them in the Brainy-Bits Forum to get help!

Leave a comment

Comments have to be approved before showing up