In this Section

↩ Return Home

↩ Return to Resources

↩ Return to Quick Start

Libraries in CircuitPython


Generally, a "Library" for embedded devices usually refers to code someone else has written to control specific hardware or execute particular functions. Importing libraries allows us to quickly interface and use different electrical devices or perform repeated tasks.

Aside for a few built-in exceptions, libraries in CircuitPython are also plain-text python files like we experienced in HelloWorld.py. See the next section for details on finding and using new libraries with PyCubed.

<aside> 📚 PyCubed will look for library files in the root directory: /PYCUBED/, as well as the lib folder: /PYCUBED/lib of your board.

The pycubed.py Helper Library


PyCubed ships with a "helper" library called pycubed.py and is located in the lib folder. It's a plain-text file written in Python just like our previous examples, which makes it easy to read through and learn from.

This helper library initializes the hardware on-board PyCubed as well as provides functions for common tasks that you may find helpful when using the board. You are encouraged to add your own functions to pycubed.py by using the existing code as a template.

How to Use pycubed.py

If you followed along for the final example in HelloWorld.py, you've already been exposed to the helper library.

In Example 2, we manually initialized the RGB LED like this:

import board
import neopixel
RGB = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.5, auto_write=True)

Looking at Example 3, however, we notice that controlling the RGB LED is performed like this:

from pycubed import cubesat
cubesat.RGB = (0,255,0)

This is made possible by the pycubed.py helper library we import on line 1 above.

<aside> 💡 NOTE: the pycubed.py library is designed to always be invoked with from pycubed import cubesat. From then on, you interface with the library through the cubesat object.

</aside>

This is just scratching the surface of what pycubed.py can do. For example, to read the accelerometer data from the IMU, it's as simple as:

from pycubed import cubesat

x_accel, y_accel, z_accel = cubesat.acceleration # m/s^2