25 Using Libraries

Installing libraries was covered in the configuration section and the same process can be used to install other libraries for additional hardware or coding support.

Adapting an Example Sketch

Just about every library you will install comes with example(s) to help you understand how to use the library. The fastest and easiest way to get something working in your own code is to adapt by copying capabilities from an example.

  1. Install all the libraries you need.
  2. Open the sketch to which you want to add some capabilities. Make sure it is working as expected for its other capabilities.
  3. Open the example under File/Examples, make the right electrical connections for your sensor and make sure the example is working.
  4. Copy over the #includes needed for the libraries and any global variable declarations required.
  5. Copy over the elements needed in the setup() function, usually a x.begin() call and maybe something to configure the sensor.
  6. Copy over the parts that are useful from the loop() function, usually some kind of getValues() function call.
  7. Add the results to your printing to report to the serial monitor.

Compile at each step along the way to make it easier to find the errors that you will inevitably introduce.

This video (5:53) shows a condensed version of me copying and pasting all the components I needed in order to get an LIS3DH accelerometer built into a sketch I already had from a previous lab.

You can use the same approach to incorporate the library for any I2C device into your own code. Note that you may need to make some minor edits, like including a different address for the purple BMP280 breakouts (bmp.begin(0x76);)

 

Creating Your Own Libraries

These hints may help you get started if you are ambitious enough to develop your own libraries.

Adding C++ Classes

Most Arduino libraries are built as C++ classes using object oriented programming techniques.  S4_Sample in the Learning Sequence provides a C++ example of how you could develop a library while using the Arduino IDE as your development environment. Once your library actually functions, you will need to copy it to the Arduino/libraries folder so other sketches can find it. Developing libraries directly in the libraries folder is difficult because the IDE will not overwrite existing files within the libraries folder and subfolders. This is meant to keep you from breaking things.

By formatting the library root folder to follow the Arduino IDE 1.5 Library Specification you can edit the .cpp and .h files in the src folder by opening the src.ino file. Although not required, a library.properties file will help the IDE integrate your library and a keywords.txt file will colour your keywords in the IDE editor to help your coding syntax. Add an examples folder that will be visible in the IDE under the file menu.

If you have trouble remembering, you can use the word ‘master’ (e.g. S4_Sample-Master) to remind yourself which is the master version of the library. Remove the word ‘master’ from the copy that resides in the Arduino/libraries folder to help keep track.

(You may be able to override the read-only provision by creating a file called .development in the library root folder, e.g. ~/Documents/Arduino/libraries/Virtual217/.development, but be extra cautious if you do this so that you avoid breaking your library code inadvertently. Also note that this fix will only work if the library root folder includes a valid library.properties file and sometimes not even then.)

Consider using the tools described here https://learn.adafruit.com/the-well-automated-arduino-library/overview to manage your own library code and understand how the Adafruit libraries you use are maintained and updated.

License

Icon for the Creative Commons Attribution-ShareAlike 4.0 International License

Rick's Measurement for Mechatronics Notes Copyright © 2019 by Rick Sellens is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License, except where otherwise noted.

Share This Book