Digital Education Resources - Vanderbilt Libraries Digital Lab

Previous lesson: Circuit Python background

Windows Instruction version

Lesson Feedbacl page

Sensors with CircuitPython: Programming the board on Mac OS

Instructions created: 20220216 (i.e., Feb 16, 2022)

Learning objectives At the end of this lesson, the learner will be able to:

Equiptment & Price breakdown 20220403:

Total set-up video time: 571 seconds or 9.51 minutes


Set up CircuitPython


Download CircuitPython

In this step, we’ll install the “operating system” (i.e., CircuitPython) for the board (i.e., QT Py RP2040) into your computer

1. Open https://circuitpython.org/board/adafruit_qtpy_rp2040/ link. ALERT: DO NOT open the file after you download it.

2. Download the latest stable release of CircuitPython .UF2 file (i.e., purple “DOWNLOAD .UF2 NOW”) for the QT Py RP2040

3. Save the file in a secure and accessible location in your computer.



Connect the board

In this step, we’ll be connecting the QT Py RP2040 microcontroller board to your computer with a usb cable

1. Connect the type-c end of a USB cable to the QT Py RP2040 board. ALERT: DO NOT connect the board to your computer, yet

2. While holding onto the red “button,” located in the center, connect the other end of the usb cable to your computer. After connecting to your computer you can stop holding onto the button.

3. To verify if your device recognized the QT Py board, view the “drive” in your finder. It’ll be named “RPI-RP2.”


Copy CircuitPython to the board

In this step, we’ll be connecting the “operating system” (i.e., .UF2 file) and the board (i.e., QT Py RP2040)

1. Open your finder and locate where the .UF2 file you downloaded in the “Download CircuitPython” step

2. Copy and paste the “operating system” (i.e., .UF2 file) into the external drive (i.e., RPI-RP2) you connected in the “Connect the board” step


Install the Mu code editor

In this step, we’ll install a code editor (i.e., Mu editor)

1. Open https://codewith.mu/en/download link

2. Select your operating system (i.e., Windows or Mac OSX) and start to download the Mu Editor in a secure and accessible location in your computer

3. Navigate to the location of the Mu Editor in your Finder and proceed to install the Mu Editor, this might take a few minutes (e.g., ~5 min).


4. Once the Mu Editor launches, or you launch the editor, set the mode to “Circuit Python.”


Download and install library modules


Download CircuitPython library bundle

In this step, we’ll download circuit python library bundle for version 7.x (i.e., “adafruit-circuitpython-bundle-7.x-mpy-2022XXXX.zip”) into our computer

1. Open https://circuitpython.org/libraries link

2. Scroll down the site to “Bundles” and “Bundles for Version 7.x”

3. Download CircuitPython 7.x (i.e., purple “adafruit-circuitpython-bundle-7.x-mpy-2022XXXX.zip”)

4. Navigate to the location of the “CircuitPython-bundle-7.x.x” folder in your device


Install modules

In this step, we’ll “install” two Modules from the CircuitPython Library Bundle (i.e., “adafruit_vcnl4040.mpy” and “neopixel.mpy”) into our CIRCUITPY External Drive board. We’ll do that by copying and pasting the two .mpy files from the adafruit-circuitpython-bundle-7.x-mpy-2022XXXX folder into the “lib” sub folder in the CIRCUITPY External Drive.

1. Open the “adafruit-circuitpython-bundle-7.x-mpy-2022XXXX” folder in your Finder

2. Once in the “adafruit-circuitpython-bundle-7.x-mpy-2022XXXX” folder navigate to the “lib” sub-folder within. In there, you’ll find the two .mpy files (i.e., “adafruit_vcnl4040.mpy” and “neopixel.mpy”) and one folder (i.e., ) you’ll need to copy and paste to the CIRCUITPY drive.

3. Copy and paste the two .mpy files (i.e., adafruit_vcnl4040.mpy and neopixel.mpy) and the folder (i.e., adafruit_register) from the adafruit-circuitpython-bundle-7.x-mpy-2022XXXX lib sub-folder into the board (i.e., CIRCUITPY (D:)) lib sub-folder


Connect a sensor

In this step, we’ll connect a sensor (i.e., Adafruit VCNL 4040) with a with the stemma QT cable (i.e., JST SH 4-Pin Cable 50mm long) to our QT Py RP2040 board

1. Take a stemma QT cable (i.e., JST SH 4-Pin Cable 50mm long) and connect to one end of the sensor (i.e., Adafruit VCNL 4040)

2. Connect the other end of the stemma QT cable to the board


Exercise:

At this point, you are ready to communicate with the board by loading/writing code scripts (i.e., instructions) into the editor and running/saving the editor

For this workshop we have two preconstructed code for you to copy, paste, and edit in the editor. The goal here is to play with the code, notice the changes, and get a general sense of what is going on with the board and sensor as you edit the code.

The two code blocks we’ll use can be found in this GitHub link


Practice Code for the board

In this exercise, we’ll be using code that is for for the QT Py board. In particular, we’ll be adjusting the LED light by changing the frequency, color, and delay of the QT Py board light.

PHOTOSENSITIVITY WARNING A small percentage of people may experience seizures when exposed to certain lights, patterns or images, even with no history of epilepsy or seizures

1. Within the Mu Editor, press the load button, navigate to the CIRCUITPY drive, and upload the code.py file.

2. Copy and Paste the following code into the code.py file into the Mu Editor

Raw Code online

import time
import board
import neopixel

pixels = neopixel.NeoPixel(board.NEOPIXEL, 1)

while True:
    pixels.fill((255, 0, 0))
    time.sleep(0.1)
    pixels.fill((0, 0, 0))
    time.sleep(0.1)

3. Press the save button to start running the code


Learning exercise: change the board LED color by editing the pixel.fill value numbers.

Learning Exercise: change the flashing rate by editing the sleep time

In your own words, can you explain why there are two instances of pixel.fill and time.sleep in the code? Can you explain what that does to the board?


Practice Code for the sensor

In this exercise, we’ll be using code that is for for the Adafruit VCNL 4040 sensor. In particular, the sensor will recording light and proximity data.

1. Within the Mu Editor, press the load button, navigate to the CIRCUITPY drive, and upload the code.py file.

2. Copy and Paste the following code into the code.py file into the Mu Editor

raw code online

import time
import board
import busio
import adafruit_vcnl4040

i2c = busio.I2C(board.SCL1, board.SDA1)
sensor = adafruit_vcnl4040.VCNL4040(i2c)

while True:
    print("Proximity:", sensor.proximity)
    print("Light: %d lux" % sensor.lux)
    time.sleep(1.0)

3. Press the save button to start running the code and press the serial button to view the output.


Learning exercise: Move your hand near the sensor and away from it and observe how the values in the console change.

Learning Exercise: change the value in for time.sleep()

To end the program: within the serial console hold ctrl + C on your keyboard

For further information (1m22s)

Welcome to CircuitPython

CircuitPython reference

QT Py RP2040 microcontroller learning guide

Adafruit Industries home page

Price breakdown 20220403

Next lesson: Running sensors remotely


Revised 2022-03-16

Questions? Contact us

License: CC BY 4.0.
Credit: "Vanderbilt Libraries Digital Lab - www.library.vanderbilt.edu"