Libraries and Modules

arduino_compatibles_Trinity_College.jpg
Long Room Interior, Trinity College Dublin, Ireland. From Wikipedia, user Diliff, CC BY-SA 4.0

Quick reference

Arduino

include <time.h>

CircuitPython

import time

Discussion

Both Arduino and CircuitPython provide a way to pull in code from outside of the file you are working on. This could be another file you've written, something that's part of the framework, or a sensor support module.

Arduino

C and C++ have code split into code files (that end in .c or .cpp, respectively) and header files (that end in .h). The Arduino environment makes a slight charge to that for the main file of your sketch/program. It ends in .ino. 

Header files traditionally contain function and global variable declarations, as well as macro and type definitions. In C++, class definitions go here as well. Code files contain function definitions. Header files provide the interface to libraries, telling your code how to access/call the library's facilities.

To make use of a header file (and the library it is part of) you include it:

Download: file
#include <string.h>

Now your code can use the functions and types defined in string.h which is a collection of string manipulation functions, e.g. strcpy() that is used to copy strings.

CircuitPython

All CircuitPython code files have a .py extension (ending). There are no separate files defining interfaces.

CircuitPython has a similar mechanism to the Arduino/C include called modules. Code creators collect a group of functions together for a specific purpose and create a module. 

Precompiled modules have a .mpy file extension. Not all modules must be precompiled, it just saves space.

To use a CircuitPython module, you should place the appropriate files, most often .mpy files, in the /lib folder on the board's flash drive.

Earlier on the Time page, the time module was imported and provided two functions: time.sleep and time.monotonic.

Note that when you import a module as above, you can't just refer to the things in it, you have to prefix them with the module name. I.e. time.monotonic(), not just monotonic(). There are ways to avoid this, but that's beyond the scope of this guide.

Download: file
import time

while True:
    print("Hello")
    time.sleep(0.1)

CircuitPython does not have the capability to import large modules available for full Python/CPython as the memory on microcontrollers is very limited.

Adafruit has committed to making a large number of modules available to support a broad range of hardware. This includes sensors, displays, smart LEDs (NeoPixel, etc.) and much more. Just as Adafruit is a leader in providing Open Source Arduino libraries, Adafruit is striving to do the same in the Python world.

There are more features to Python Modules but the above covers the basics. See a Python reference such as this one to learn more.

Refer to the Adafruit GitHub repository for the latest CircuitPython modules available.

This guide was first published on Oct 22, 2018. It was last updated on Oct 22, 2018. This page (Libraries and Modules) was last updated on Oct 22, 2019.