Sensor Libraries

Sensor Libraries#

For many of these sensors you will need to use a library, an extra set of code files that allow you to interact with a given sensor.

Importing Libraries#

Here are a couple of methods to import libraries to a Pico

Circuit Python Libraries

For many sensors Adafruit (who develop Circuit Python) provide a set of libraries

To access these libraries, we need to upload them to the Pico from a separate bundle. Follow the instructions below:

  1. Go to this website where you can download a bundle of official CircuitPython libraries. Make sure to download the bundle that matches your version of CP (most likely 9.x).

  2. Open the .zip file and in the lib folder, find the files you require, (for example for the DHT11 the files are adafruit_dht.mpy).

  3. Copy these files into the lib folder on your CIRCUITPY drive.

Watch out

It is good practise to put these files in the lib folder on the pico as it keeps your file structure clean and easy to follow. Try not to have other versions in the main directory with the code.py file. This can cause issues and complications, which are all avoided with good house keeping of your files and folders

Other Sources of Libraries

Some sensors may not be available with Circuit Python libraries in the correct format, but there may be a python driver available from micropython. One method to then create a Circuit Python library is given below. In this case we will use the HM3301 dust sensor as an example:

  1. Navigate to this github link where you can find the custom HM3301 library.

  2. On this page, you can press the download raw file button to the top right of the code block. Once downloaded, save this seeed_hm3301.py file to a location you’ll remember on your home drive.

  3. We now need to convert this to a .mpy file. Use this link to download the mpy-cross application and save it in the same place as your seeed_hm3301.py file.

  4. Right click on your mpy-cross file and click open in terminal. then enter

    ./mpy-cross seeed_hm3301.py
    
  5. Once this has run, you should now see a .mpy version of your .py library, in the same place.

  6. Copy this .mpy file to your Pico lib folder. It is now available to be used in your code.

Here are a couple of methods to import libraries to an Arduino

Arduino IDE available Libraries

Many/most libraries can be accessed through the Arduino IDE.

../../_images/ard_libs_1.png

Fig. 8.1 Arduino libraries#

  1. Click on the library symbol in the side bar (see figure), or go to the menu and select ‘Tools -> Manage Libraries..’

  2. Type the name of the library or sensor you are interested in. If available it should appear here

  3. It is common for more than one library to be available as many companies produce different libraries. Select a library from a reputable source

  4. Once selected it should be available for compliation to the Arduino board

Other Sources of Libraries

Sometimes, though it is getting less common, a library you need to use isn’t available in the IDE list of libraries.

This can be solved in a couple of ways. It is possible to add other repo sources to the IDE list, however, this can have security implications. This other is to find the library, download it in a zip and install directly into the IDE

  1. Source the library in a packaged zip format. If meant to be used with the Arduino it should have the correct structure

  2. In the IDE go to the menu and select ‘Sketch -> Include Library -> Add .Zip library’

  3. In the file manager that is opened find the zip file and select it. This should install the library and make it available for compliation