« Teensy4.0にCircuitPythonをインストールする | トップページ | CircuitPythonにMini-8x8-BackPackを接続する »

2020年3月19日 (木)

Turtorial Examples for CircuitPython(Teensy4.0)

2020/3/19

Turtorial Examples for CircuitPython(Teensy4.0)

Teensy4.0

Teensy4.0 pinout
Teensy4.0 Back Side pinout

How to install modules of Adafruit_CircuitPython_Bundle:

mkdir cpy_ws cd cpy_ws git clone https://github.com/adafruit/Adafruit_CircuitPython_Bundle.git cd Adafruit_CircuitPython_Bundle ./update-submodules.sh # sample case cd libraries/drivers cp /neopixel/neopixel.py CIRCUTPY/lib/ cd .. cd .. cd libraries/helpers cp adafruit_bus_device CIRCUTPY/lib/ cp adafruit_register CIRCUTPY/lib/

blink.py

import board import digitalio import time led = digitalio.DigitalInOut(board.D13) led.direction = digitalio.Direction.OUTPUT while True: led.value = True time.sleep(0.1) led.value = False time.sleep(0.1)

Pin/GPIO

# Teensy4.0 import time import board from digitalio import DigitalInOut, Direction, Pull led = DigitalInOut(board.D13) # LED on board led.direction = Direction.OUTPUT switch = DigitalInOut(board.D2) # switch on D2 switch.direction = Direction.INPUT switch.pull = Pull.UP while True: led.value = not switch.value time.sleep(0.01) # debounce delay

PWM

動作せず???

import time import board import pulseio # D0 thru D9, D22, D23 for PWM (BackSide: D24,D25, D28,D29,D33 etc) led = pulseio.PWMOut(board.D2, frequency=5000, duty_cycle=0) while True: for i in range(100): # PWM LED up and down if i < 50: led.duty_cycle = int(i * 2 * 65535 / 100) # Up else: led.duty_cycle = 65535 - int((i - 50) * 2 * 65535 / 100) # Down time.sleep(0.01)

ADC

import time import board from analogio import AnalogIn knob = AnalogIn(board.A9) def get_voltage(raw): return (raw * 3.3) / 65536 while True: raw = knob.value volts = get_voltage(raw) print("raw = {:5d} volts = {:5.2f}".format(raw, volts)) time.sleep(0.5)

SPI

N.A.

I2C

# software I2C import board import bitbangio i2c = bitbangio.I2C(board.D19, board.D18) # D19 -> SCL, D18 -> SDA i2c.try_lock() i2c.scan() >>> import board >>> import bitbangio >>> i2c = bitbangio.I2C(board.D19, board.D18) >>> i2c.try_lock() True >>> i2c.scan() [30] >>> # hardware I2C import board import busio i2c = busio.I2C(board.SCL, board.SDA) # SCL -> D19, SDA -> D18 i2c.try_lock() i2c.scan() >>> import board >>> import busio >>> i2c = busio.I2C(board.SCL, board.SDA) >>> i2c.try_lock() True >>> i2c.scan() [30] >>>

RTC

clock.py

# clock.py import time from time import sleep from rtc import RTC rtc = RTC() days = ("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday") # year, mon, date, hour, min, sec, wday, yday, isdst t = time.struct_time((2020, 3, 17, 11, 42, 1, 2, -1, -1)) rtc.datetime = t while True: dt = rtc.datetime print("date: {}/{}/{} {}".format(dt.tm_year, dt.tm_mon, dt.tm_mday, days[int(dt.tm_wday)])) print("time: {}:{:02}:{:02}".format(t.tm_hour, dt.tm_min, dt.tm_sec)) print("---------------") sleep(1)

wday does not work at this version.

NeoPixel

module install:

# refer to https://github.com/adafruit/Adafruit_CircuitPython_Bundle mkdir cpy_ws cd cpy_ws git clone https://github.com/adafruit/Adafruit_CircuitPython_Bundle.git cd Adafruit_CircuitPython_Bundle ./update-submodules.sh cd libraries/drivers/neopixel cp neopixel.py CIRCUTPY/lib/

Demo Script:

import time import board import neopixel # connect D11 of Teensy4.0 to DIN of neopixels pixel_pin = board.D11 # The number of NeoPixels num_pixels = 16 # The order of the pixel colors - RGB or GRB. Some NeoPixels have red and green reversed! # For RGBW NeoPixels, simply change the ORDER to RGBW or GRBW. ORDER = neopixel.GRB pixels = neopixel.NeoPixel(pixel_pin, num_pixels, brightness=0.2, auto_write=False, pixel_order=ORDER) def wheel(pos): # Input a value 0 to 255 to get a color value. # The colours are a transition r - g - b - back to r. if pos < 0 or pos > 255: r = g = b = 0 elif pos < 85: r = int(pos * 3) g = int(255 - pos*3) b = 0 elif pos < 170: pos -= 85 r = int(255 - pos*3) g = 0 b = int(pos*3) else: pos -= 170 r = 0 g = int(pos*3) b = int(255 - pos*3) return (r, g, b) if ORDER == neopixel.RGB or ORDER == neopixel.GRB else (r, g, b, 0) def rainbow_cycle(wait): for j in range(255): for i in range(num_pixels): pixel_index = (i * 256 // num_pixels) + j pixels[i] = wheel(pixel_index & 255) pixels.show() time.sleep(wait) while True: pixels.fill((255, 0, 0)) pixels.show() time.sleep(0.5) pixels.fill((0, 255, 0)) pixels.show() time.sleep(0.5) pixels.fill((0, 0, 255)) pixels.show() time.sleep(0.5) rainbow_cycle(0.001) # rainbow cycle with 1ms delay per step

第1番目のLEDは指定通りの色で光らないバグがあるようだ。 (ハード起因かソフト起因は不明)

DHT

以下、動作せず。 DHTのVCCとして5Vが必要??

import time import board import adafruit_dht # Initial the dht device, with data pin connected to: dht = adafruit_dht.DHT11(board.D12) while True: try: # Print the values to the serial port temperature_c = dht.temperature temperature_f = temperature_c * (9 / 5) + 32 humidity = dht.humidity print("Temp: {:.1f} F / {:.1f} C Humidity: {}% " .format(temperature_f, temperature_c, humidity)) except RuntimeError as error: # Errors happen fairly often, DHT's are hard to read, just keep going print(error.args[0]) time.sleep(2.0)

Networking

N.A.

Performance Test

performanceCircuitPython.py

# Peformace Test CircuitPython from time import monotonic_ns def performanceTest(): endTime = monotonic_ns() + 10000000000 # 10 sec count = 0 while monotonic_ns() < endTime: count += 1 print("Count: ", count) performanceTest()

出力例:
Count: 476009

monotonic_nsのオーバーヘッドが大きいようなので、 それを使わず、実行後、10秒後、Ctrl-Cを押して止めて、countの内容を確認するバージョンも作った:

# Manual Stop version # Peformace Test CircuitPython from time import monotonic_ns def performanceTest(): global count while True: count += 1 print("Count: ", count) count = 0 performanceTest()

出力例:
>>> count
1387524

monotonic_ns無しのほうが3倍くらい速い。
(Nucleo-F446REよりは遅い)

関連情報

Teensy4.0にCircuitPythonをインストールする
MicroPython Performance Test

以上

|

« Teensy4.0にCircuitPythonをインストールする | トップページ | CircuitPythonにMini-8x8-BackPackを接続する »

linux」カテゴリの記事

MicroPython」カテゴリの記事

CircuitPython」カテゴリの記事

コメント

この記事へのコメントは終了しました。