Skip to main content

Programming with Circuit Python

CircuitPython is a programming language that simplifies experimenting and programming on basic microcontroller boards. Once the board is set up, programming can be done using any text editor. The senseBox MCU is CircuitPython compatible.

Installation of Circuit Python

The latest version can always be downloaded from the official CircuitPython Website. Connect the senseBox MCU to your computer using a USB cable and put it into programming mode by double-clicking the red reset button. Copy the .uf2 file to your senseBox MCU. The senseBox will then be recognized as a new device named "CircuitPy".

Programming

Open/create the file Code.py on the "CircuitPy" drive and copy the following code as the first example:

import board
import digitalio
import time

led = digitalio.DigitalInOut(board.GREEN_LED)
led.direction = digitalio.Direction.OUTPUT

while True:
led.value = True
time.sleep(0.5)
led.value = False
time.sleep(0.5)

After saving the file, the code will be executed immediately, and the small LED next to the reset button will start blinking. You can now easily modify and save the code, and it will be executed directly.

Alternatively, you can use the following web editor (only with Google Chrome): https://circuitpython.sensebox.de/

Many more details about programming with CircuitPython are available on the official CircuitPython Website.

Libraries

Most components of the senseBox have libraries available that can be used. However, it is important to note that the libraries must be copied to the CIRCUITPY drive. A total of 48Kb of storage is available for this purpose.

Note: Due to the limited storage space available for libraries, more complex projects such as the CO2 traffic light or the environmental monitoring station cannot be implemented with CircuitPython. However, the storage space should be sufficient to read individual sensors.

The complete package with the libraries can be downloaded here. Make sure to download the .mpy version.

Supported Components

Since some components do not have libraries available or the libraries are too large, here is a brief list of the tested components:

info

The table may be incomplete and is continuously updated. If you have tested components that are not yet listed, we would appreciate a short feedback.

ComponentLibrary AvailableTest Successful
BME680 Environmental SensorYesYes
CO2 SensorYesYes
BMP280YesYes (adjustment of the I2C address necessary)
HDC1080NoNo
BMX055NoNo
HC-SR04 UltrasonicYes
WS2818 RGB LEDYesYes
DisplayYesYes
SD-BeeYesYes
LoRa-Bee
WiFi-BeeNoWiFi is not supported by CP

Serial Monitor

To access the Serial Monitor via the terminal

ls /dev/tty.*

alle verfügbaren Ports auflisten lassen. Die senseBox MCU sollte als usbmodemXXX auftauchen.

screen /dev/tty.board_name 115200

SD Card

To load libraries from the SD card, copy the following code as sdmount_lib.py onto the SD card.

import sys

import adafruit_sdcard
import board
import busio
import digitalio
import storage
from digitalio import DigitalInOut, Direction

xb2_pwr = DigitalInOut(board.XB2_PWR)
xb2_pwr.direction = Direction.OUTPUT
xb2_pwr.value = False


# Connect to the card and mount the filesystem.
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
cs = digitalio.DigitalInOut(board.XB2_CS)
sdcard = adafruit_sdcard.SDCard(spi, cs)
vfs = storage.VfsFat(sdcard)
storage.mount(vfs, "/sd")
sys.path.append("/sd")
sys.path.append("/sd/lib")

The libraries can then be copied to the SD card in /lib. By using import sdmount_lib, the SD module is activated and the libraries can be used. More information on using an SD module with Circuit Python can be found directly at Adafruit.

Uninstallation

To uninstall Circuit Python, put the senseBox into programming mode by double-clicking the red reset button. Simply copy a .bin file (e.g., created in Blockly) onto the senseBox MCU, and the senseBox MCU will be programmable again as usual via Blockly and Arduino.

Power Supply of the Ports

By default, the individual ports/pins are not powered. The following code (as used in the SD example) can be used to power the individual interfaces.

Example for the XBee 2 port:

xb2_pwr = DigitalInOut(board.XB2_PWR) # hier den jeweilen Pin angeben siehe Tabelle
xb2_pwr.direction = Direction.OUTPUT
xb2_pwr.value = True

The other ports can be turned on as follows. Note that the status of the XBee ports is inverted:

Ports/PinName in CodeStatus (activated)Status (deactivated)
XBEE 1XB1_PWRFalseTrue
XBEE 2XB2_PWRFalseTrue
I2CI2C_PWRTrueFalse
UARTUART_PWRTrueFalse
Green LEDGREEN_LEDTrueFalse
Red LEDRED_LEDTrueFalse
Pins D1-D13D1-D13TrueFalse