Digital Education Resources - Vanderbilt Libraries Digital Lab

return to CircuitPython landing page

First beginner Python lesson

Sensors with CircuitPython: Background

The purpose of this lesson is to provide background about microcontroller hardware and CircuitPython. The goal is to provide the necessary information you’ll need to start playing with a QT Py 2040 board and sensors.

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

Total video time: 31 min 56 s

Links

Lesson slides

DiSC CircuitPython code on GitHub


Introduction to QT Py RP2040 (0m42s)

.

QT Py RP 2040 information page


Introduction to CircuitPython (5m21s)

.

CircuitPython landing page

RP2040 chip specifications


The code.py script (2m16s)

.

Circumstances under which code.py begins execution:

Example code.py script for QT Py RP2040:

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)

Modules in CircuitPython (5m25s)

.

CircuitPython library bundle download page

CircuitPython libraries installation instructions

Blinka landing page


Connecting sensors (4m18s)

.

I2C protocol specification

QT Py RP2040 pinouts description


CircuitPython objects (5m26s)

.

Microsoft learning module on Python object-oriented programming

VCNL4040 breakout board CircuitPython reference page


Sensor code (7m06s)

.

Lesson on for and while loops

Code examples:

Execute loop 10 times:

count = 0
while count < 10:
    count += 1
    print(count, 'yaaaa!')

Execute loop 1 time:

count = 0
if True:
    count += 1
    print(count, 'yaaaa!')

Infinite loop:

count = 0
while True:
    count += 1
    print(count, 'yaaaa!')

digitalio module reference

CircuitPython digital inputs learn page

code.py example with triggering data collection using the button:

import time
import board
import busio
import digitalio
import adafruit_vcnl4040

i2c = busio.I2C(board.SCL1, board.SDA1)

# humidity/temperature sensor
sensor = adafruit_vcnl4040.VCNL4040(i2c)

# Code to use the built-in boot button on the board. Pressed is False (grounded)
button = digitalio.DigitalInOut(board.BUTTON)
button.switch_to_input(pull=digitalio.Pull.UP)

# Program will do nothing until the button is pressed.
while button.value: # button.value will be True when not pressed
    pass

# read the relative humidity and temperature every second indefinitely
while True:
    print("Proximity:", sensor.proximity)
    print("Light: %d lux" % sensor.lux)
    time.sleep(1.0)

For further information (1m22s)

.

Welcome to CircuitPython

CircuitPython reference

QT Py RP2040 microcontroller learning guide

Adafruit Industries home page


Next lesson:

Questions? Contact us

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