In this section, we'll be looking at using code modules called 'libraries' which can be used to simplify complex applications.

Transcript

As you know, the Arduino IDE uses a software project called Wiring, which hides the nitty-gritty aspects of microcontroller programming and offers a collection of easy-to-use software functions for GPIO control, software timers, serial communications, and heaps more. These are all part of the core Arduino libraries and are built into the IDE.

However, as with most programming environments and languages, Arduino allows you to use pre-existing chunks of code, called libraries, to make different software applications easier to work with. So what is a library? A library is a collection of code designed to integrate with the specific platform you're working with. It's designed to perform a certain purpose, such as controlling a type of sensor or making serial communications easier. It provides a set of functions which you can use in your program, and the functions take care of the heavy lifting and complex code, allowing you to continue writing your program without getting bogged down in data sheets and numbers.

We used a library in the previous chapter to integrate the non-volatile EEPROM with a simple project to retain data after power cycles. We'll use the Arduino Wire library and SPI library further on in this chapter, which allows easy control of the I2C and SPI interfaces.

If you want to send some data on it, you could spend days or even weeks trying to understand the data sheet. Or you could use the Wire library which provides a function, called writeWithData, which takes care of all of it for you. Without the library, if you called write and gave it some sort of parameter, it would give you an error. However, when you include the library with your sketch, then calling that function, write for example, runs a number of lines of code in the library to carry out that request, and the function is passed the data that you wish to send.

As with the built-in functionality of Arduino, the IDE comes bundled with a collection of libraries which are community tested and used in thousands of projects around the world. Makers, DIYers, and developers can also release their own libraries for Arduino, which you can download and use in your sketch in the same way.

Including libraries in your code is easy. You simply go to Sketch, Include Library, and then you select the library that you wish to include. Now if you're interested in more info, www.arduino.cc/en/reference, and I'll provide a link in the workshop content, is the place to go to learn how to use the libraries, and also more about the built-in functions of the Arduino IDE. You can find out what functions are available for different libraries, the syntax they use, and the required data types. Third-party libraries should also contain a documentation page, which has examples and information.

You also can add external libraries to your IDE by choosing the add.zip library option. In the sketch menu, you can see it there, include library, add.zip library, and that takes a simple zip file and adds it to the library collection of your IDE. Most libraries consist of two main files, which are the.h and.cpp files. The.h file is also known as the header file and acts as a bridge between the code in the.cpp file and the rest of your sketch. Some libraries, however, don't come in a handy zip format and instead just give you that.h and the.cpp files, and perhaps some examples. Adding libraries in this format is still really easy, you just get the whole folder which contains all of the library files, and copy it into your Arduino user libraries folder. By default, this libraries folder is located in the same folder where you save your sketches.

So now that we know a bit about what libraries are and how to use them, let's take a look at some examples in the workshop.Arduino IDE provides a convenient way to add libraries for various functionalities. For instance, if you want to add a library for using the SN3218PWM expander, which utilizes the I2C interface, you can simply copy the library into the "documents/Arduino/libraries" directory. Upon reopening the IDE, the library will appear in the libraries folder, and you can include it in your sketch by navigating to "Sketch > Include Library" and selecting the desired library.

Additionally, Arduino comes with built-in libraries that can be easily included in your sketches. For example, if you want to use a liquid crystal display (LCD), you can include the "LiquidCrystal" library and create an object called "LCD" to utilize its specific functions.

When adding a library, the "hash include" statement followed by the library name will be automatically generated. It's important to note that Arduino libraries often come with extensive documentation and examples, which can be accessed through the documentation page. This documentation provides references and examples to help users understand the library's functions, parameters, and usage.

Exploring examples in the subsequent sections will further illustrate how to effectively utilize libraries within the Arduino IDE.

Feedback

Please continue if you would like to leave feedback for any of these topics:

  • Website features/issues
  • Content errors/improvements
  • Missing products/categories
  • Product assignments to categories
  • Search results relevance

For all other inquiries (orders status, stock levels, etc), please contact our support team for quick assistance.

Note: click continue and a draft email will be opened to edit. If you don't have an email client on your device, then send a message via the chat icon on the bottom left of our website.

Makers love reviews as much as you do, please follow this link to review the products you have purchased.