5. CircuitPython簡介
CircuitPython is a programming language designed to simplify experimenting
and learning to program on low-cost microcontroller boards.
CircuitPython is based on Python
Advantage:
Running quickly
Easily update your code
Serial console and REPL
5
6. CircuitPython安裝
Follow the step from https://learn.adafruit.com/adafruit-circuit-playground-
bluefruit/circuitpython
Download the latest UF2 file -- CircuitPython 6.0.0-beta.2
Double-click the small RESET button in the middle of the CPB
Drag the adafruit_circuitpython_xxx-6.0.0-xxx.uf2 file to CPLAYBTBOOT
Download the latest library bundle file -- Bundle Version 6.x
Copy the following folders and files from the bundle lib folder to the lib folder on
your CIRCUITPY drive
6
7. Installing Mu Editor
Download Mu from https://codewith.mu
Click [Download] -- Windows 64-bit
Using Mu
7
8. CircuitPython實作
Blinking the D13 LED -- https://learn.adafruit.com/adafruit-circuit-playground-
bluefruit/creating-and-editing-code
Copy Code
Paste the code into Mu editor
Save as code.py on your CIRCUITPY drive
Connecting to the Serial Console – print("Hello, world!")
The REPL
Press any key to enter the REPL
Use Ctrl+D to reload
Press Ctrl+C to interrupt
CircuitPython Playground: https://learn.adafruit.com/adafruit-circuit-playground-
bluefruit/circuitpython-playground
8
11. CircuitPython(SGP30+PMSA003+Audio)
實作-1
""" Example for using the SGP30 and PM25 with CircuitPython
and the Adafruit library """
import digitalio
import busio
import adafruit_sgp30
import board
import adafruit_pm25
from tool import show_SGP, show_SGP_baseline # for SGP30
from tool import show_PM25 # for PMSA003
from tool import color_chase_1 # for NeoPixel
from tool import play_file # for AudioOut
# 設定 LED 顏色
RED = (255, 0, 0)
GREEN = (0, 255, 0)
OFF = (0, 0, 0)
i2c_bus = busio.I2C(board.SCL, board.SDA, frequency=100000)
# Create library object on I2C port
sgp30 = adafruit_sgp30.Adafruit_SGP30(i2c_bus)
print("SGP30 serial #", [hex(i) for i in sgp30.serial])
sgp30.iaq_init()
# sgp30.set_iaq_baseline(co2eq_base, tvoc_base)
# elapsed_sec = 0
reset_pin = None
uart = busio.UART(board.TX, board.RX, baudrate=9600)
pm25 = adafruit_pm25.PM25_UART(uart, reset_pin)
# Enable the speaker
spkrenable = digitalio.DigitalInOut(board.SPEAKER_ENABLE)
spkrenable.direction = digitalio.Direction.OUTPUT
spkrenable.value = True
# Make the 2 input buttons
11
12. CircuitPython(SGP30+PMSA003)實作-2
# Tools
import time
import board
import neopixel
dup = 37 # hyphen
# include audio libraries
try:
from audiocore import WaveFile
except ImportError:
from audioio import WaveFile
try:
from audioio import AudioOut
except ImportError:
try:
from audiopwmio import PWMAudioOut as AudioOut
except ImportError:
pass # not always supported by every board!
pixels = neopixel.NeoPixel(
board.NEOPIXEL,
10,
brightness=0.2,
auto_write=False
)
# LED 全亮
def color_chase_all(color, wait):
for i in range(10):
pixels[i] = color
time.sleep(wait)
pixels.show()
12