Arduino Tutorials

Arduino Library: What is Arduino Library, Installing, Use and More

In the last tutorial, we discussed ardiuno program control flow. Here, we shall discuss Arduino Library in an intuitive way.

What Is An Arduino Library?

Arduino libraries are files embedded to the arduino integrated development environment (IDE) that add more functionality and ease to the use the arduino board. This functionality comes in the form of smart and easy use of sensors, modules, motors, data manipulation etc. with the arduino. It could be very difficult and challenging to use some hardware with an arduino, but with a library, the design and sketch will be elegant and simple.

Intuitive Understanding of a Library

Just as stated earlier, it would be challenging to use some hardware peripherals with arduino without a library, take a servo motor for example. How does a servo motor work?

Here is How Servo Motor Works

A servo motor work by sending pulse width modulation (PWM) signal to its signal pin. The duration of the signal determines the direction the motor shaft will move and the position it will assume. The recommended period of the signal is 20ms, however, to move the position of the shaft to 0° which is the left end position of the shaft, we fashion the signal to have a duty cycle of 5%. What this means is that the signal is ON for just 1.0ms and OFF for 19.0ms within the 20ms oscillation period. See image below.

Timing diagram PWM with 5% duty cycle
Figure 1. Timing diagram PWM with 5% duty cycle

At the other hand, to move the servo to 90° position, the signal should be 1.5ms ON and 18.5ms OFF, image below.

Timing diagram PWM with 7.5% duty cycle
Figure 2. Timing diagram PWM with 7.5% duty cycle

 While for 180° position, it will be 2ms ON and 18ms OFF.

Timing diagram PWM with 10% duty cycle
Figure 3. Timing diagram PWM with 10% duty cycle

Hence moving the servo motor from position 0° (extreme left) to position 180° (extreme right) requires a PWM signal with ON time from 1ms to 2ms. To control a servo motor without a library requires that we generate these signals and send them to the servo motor anytime we need to control the motor. To do that with an arduino requires we write codes that will generate the signals each time we need them. Doing that anytime we need to control the servo motor will be challenging and difficult to debug. However, with arduino servo library, everything is made easy, the library takes care of generating the signals, all we do is using simple library functions, choose the angle we want and the servo motor shaft will move to that position.

Nature Of Arduino Library

Arduino Libraries consist of at least two files: a header file (with the extension .h) and a source file (with extension .cpp). The servo motor arduino library contains mainly the two files as shown below.

A header file (with the extension .h) and a source file (with extension .cpp)
Figure 4: A header file (with the extension .h) and a source file (with extension .cpp)

Basically, the header file (.h) contains definitions for the library, which is a listing of everything that is inside the library, including functions and needed variables. The source file (.cpp) at the other hand contains the code used to make the library work.

The various actions that can be performed by a servo motor are already written in the servo library and these actions are executed with functions. All we need to do is, whenever we want the servo motor to perfume such actions like move to a certain position, etc. we call the name of the function in which the action is written. Some of the servo motor arduino library functions include:

  • attach
  • detach
  • write
  • read
  • readMicroseconds
  • writeMicroseconds
  • attached

In the future, we shall discuss how to use a servo motor.

How to Install Arduino Library

To use an arduino library, you have to first download and install the library if it is not already preinstalled in the arduino IDE. There are basically three methods to do that.

Installing Arduino Library Using the Library manager

To install arduino library using this method, follow the following steps: navigate to Sketch > Include Library > Manage Libraries

When this is done, you will see a window open showing similar images below:

Installing Arduino Library Using the Library manager 1
Installing Arduino Library Using the Library manager 2

You can search for any library you want by entering the name in the “filter your search” box. For example, if I type servo motor, it will bring any library that is somehow connected to the servo library; the image below shows what I mean.

Installing Arduino Library Using the Library manager 3

Since, I had already installed the servo motor library, it will show INSTALLED as can be seen in the image, to update the library, you click the ‘More info’ link, you will see the update button, but if the library is not yet installed in your system, when you click the ‘More info’ link, an option will come out for you to install the library. See images below.

Installing Arduino Library Using the Library manager 4
Installing Arduino Library Using the Library manager 5

So, by clicking install, the library will start installing, the process takes seconds. When it is done installing you will be notified. See images below.

Installing Arduino Library Using the Library manager 6
Installing Arduino Library Using the Library manager 7

To use the library you installed, navigate to Sketch > Include Library, you will see it under contributed libraries as you scroll down by dropping the cursor on the down scroll arrow in the include library window.

Installing Arduino Library Using the Library manager 8

Installing Arduino Library Using Add.ZIP Library in the Arduino IDE

To install a library using this method, you have to first download the library from github.com or other sources that have the library you need. Download the zip file of the library. If you are using github for example, after finding the library you want on their website, click the green “clone and download” button followed by the download ZIP. Here I am downloading the Dallas temperature sensor library.

Downloading and Installing Arduino Library from GitHub

After downloading the file, navigate to Sketch > Include Library > Add .zip Library in your Arduino IDE to install the library.

Installing Arduino Library Using Add.ZIP Library in the Arduino IDE 1

When you open the Add.ZIP library, you will be required to open the library zip file you downloaded in the file dialog window. See image below:

Installing Arduino Library Using Add.ZIP Library in the Arduino IDE 2

Clicking the open button will automatically install the library.

Installing Arduino Library by Copying library files

To use this method, you download the zip file as shown above, this time you have to unzip the file using any file unzipping software in your system like 7zip, Winrar, etc. after unzipping or extracting the files, proceed to copying the extracted file paste into the arduino libraries folder. To do so, open the arduino raw file and navigate to libraries. See image below:

Installing Arduino Library by Copying library files 1

When that is done, you will see the library file added to the libraries folder as shown below:

To ensure that the library was added, navigate to Sketch > Include Library > Manage Libraries and search for the library.

These are three ways of installing arduino library to arduino IDE.

Having learnt how to install arduino libraries, the next step is how to use the libraries. To use arduino libraries is easy as you will see in the “HOW TO” section of the blog, all you need to do is include the library or libraries you need. You can visit this section to see some of our fantastic projects.

Tags

Related Articles

Leave a Reply

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

Back to top button
Close