Digital Education Resources - Vanderbilt Libraries Digital Lab
return to CircuitPython landing page
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:
code.py
file can be initiated in CircuitPython.while True:
loop to keep a Python script running indefinitely.Total video time: 31 min 56 s
DiSC CircuitPython code on GitHub
.
QT Py RP 2040 information page
.
.
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)
.
CircuitPython library bundle download page
CircuitPython libraries installation instructions
.
QT Py RP2040 pinouts description
.
Microsoft learning module on Python object-oriented programming
VCNL4040 breakout board CircuitPython reference page
.
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!')
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)
.
QT Py RP2040 microcontroller learning guide
Next lesson:
Revised 2022-04-03
Questions? Contact us
License: CC BY 4.0.
Credit: "Vanderbilt Libraries Digital Lab - www.library.vanderbilt.edu"