top of page

Safari browser sometimes has issues displaying images...

I.e: *you have to click on the images to see them...

For a better browsing experience on Brainy-Bits

Please use Chrome, Edge or Firefox browser.

Writer's pictureBrainy-Bits

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



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 INTRODUCTION


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

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).

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.

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.


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.

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



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.


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.



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



*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.


– 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.



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



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



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:



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 only if you’re using a version of the Arduino IDE 1.56 or higher.CNC MILLING


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


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”.



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



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.


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


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.



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

 

TUTORIAL VIDEO




1,131 views0 comments

Comments


All my content is and will always be Free.

If you feel that my Videos / Tutorials are helping, and you would like to contribute...

 You can toss some coins in the Tip Jar via PayPal.

Select amount then click the “Donate” button.

bottom of page