« RaspberryPiのファイルシステムにSSHFSでマウントする | トップページ | python3/micro:bit-micropython/CircuitPython用エディタ(mu-editor)をインストールする(linux版) »

2020年3月29日 (日)

RaspberryPiのadafruit_blinkaでSense_HATのセンサーにアクセスする

2020/3/29

Rpi adafruit_blinka Sense HAT

Rpi adafruit_blinka Sense HAT

概要

RaspberryPiのadafruit_blinkaライブラリでcircuitpythonのAPIを使ってSense_HATのセンサーにアクセスする。 Sense_HATのセンサーのアクセスはSense_HATのライブラリでできるが、ここでは、adafruit_blinkaライブラリのテストとしてSense_HATのセンサーにアクセスする。Sense_HATのセンサーとして以下があるので、それに対するアクセスをテストすることになる。

I2C address Device
0x5c LPS25H(Pressure/Temperature)
0x1c LSM9DS1(IMU (Accelerometer and Magnetometer))
0x5f HTS221(Humidity/Temperature)
0x6a LSM9DS1(IMU (Accelerometer and Magnetometer))
0x46 LED2472G(今回は使用しない)

adafruit_blinkaのインストールについては以下のリンクを参照のこと:

RaspberryPiのpython3でCircuitPythonのAPIを使用する

該当モジュールのインストール方法

Module Install:

pip3 install adafruit-circuitpython-lps2x pip3 install adafruit-circuitpython-lsm9ds1 pip3 install adafruit-circuitpython-hts221

Demo Script(動作確認)

demo script:
lps2x_HAT_test.py

import time import board import busio import adafruit_lps2x i2c = busio.I2C(board.SCL, board.SDA) lps = adafruit_lps2x.LPS2X(i2c, address=0x5c) while True: print("Pressure: %.2f hPa" % lps.pressure) print("Temperature: %.2f C" % lps.temperature) print("") time.sleep(1)

出力例:
LPS25H(lps2X)

$ python3 lps2x_HAT_test.py Pressure: 1018.85 hPa Temperature: 17.60 C Pressure: 1018.84 hPa Temperature: 17.62 C Pressure: 1018.72 hPa Temperature: 17.64 C

demo script:
lsm9ds1_HAT_test.py

# Simple demo of the LSM9DS1 accelerometer, magnetometer, gyroscope. # Will print the acceleration, magnetometer, and gyroscope values every second. import time import board import busio import adafruit_lsm9ds1 # I2C connection: i2c = busio.I2C(board.SCL, board.SDA) sensor = adafruit_lsm9ds1.LSM9DS1_I2C(i2c,xg_address=0x6a,mag_address=0x1c) # SPI connection: # from digitalio import DigitalInOut, Direction # spi = busio.SPI(board.SCK, board.MOSI, board.MISO) # csag = DigitalInOut(board.D5) # csag.direction = Direction.OUTPUT # csag.value = True # csm = DigitalInOut(board.D6) # csm.direction = Direction.OUTPUT # csm.value = True # sensor = adafruit_lsm9ds1.LSM9DS1_SPI(spi, csag, csm) # Main loop will read the acceleration, magnetometer, gyroscope, Temperature # values every second and print them out. while True: # Read acceleration, magnetometer, gyroscope, temperature. accel_x, accel_y, accel_z = sensor.acceleration mag_x, mag_y, mag_z = sensor.magnetic gyro_x, gyro_y, gyro_z = sensor.gyro temp = sensor.temperature # Print values. print( "Acceleration (m/s^2): ({0:0.3f},{1:0.3f},{2:0.3f})".format( accel_x, accel_y, accel_z ) ) print( "Magnetometer (gauss): ({0:0.3f},{1:0.3f},{2:0.3f})".format(mag_x, mag_y, mag_z) ) print( "Gyroscope (degrees/sec): ({0:0.3f},{1:0.3f},{2:0.3f})".format( gyro_x, gyro_y, gyro_z ) ) print("Temperature: {0:0.3f}C".format(temp)) print("") # Delay for a second. time.sleep(1.0)

出力例:
LSM9DS1(lsm9ds1)

$ python3 lsm9ds1_HAT_test.py Acceleration (m/s^2): (-2.947,6.827,6.356) Magnetometer (gauss): (-0.431,0.330,-0.180) Gyroscope (degrees/sec): (286.676,-194.495,285.128) Temperature: 17.500C Acceleration (m/s^2): (-2.955,6.819,6.273) Magnetometer (gauss): (-0.425,0.335,-0.185) Gyroscope (degrees/sec): (3.159,0.516,0.508) Temperature: 17.500C

demo script:
hts221_HAT_test.py

import time import board import busio import adafruit_hts221 i2c = busio.I2C(board.SCL, board.SDA) hts = adafruit_hts221.HTS221(i2c) # _HTS221_DEFAULT_ADDRESS = 0x5F while True: print("Relative Humidity: %.2f %% rH" % hts.relative_humidity) print("Temperature: %.2f C" % hts.temperature) print("") time.sleep(1)

出力例:
HTS221(hts221)

$ python3 hts221_HAT_test.py Relative Humidity: 52.47 % rH Temperature: 16.71 C Relative Humidity: 52.38 % rH Temperature: 16.75 C Relative Humidity: 52.33 % rH Temperature: 16.80 C

demo script:
disp_press_tempe.py

# Adafruit_Blinka/sense_hat coexistence test import time import board import busio import adafruit_lps2x i2c = busio.I2C(board.SCL, board.SDA) lps = adafruit_lps2x.LPS2X(i2c, address=0x5c) #------------------------------------- from time import sleep from sense_hat import SenseHat sense = SenseHat() while True: print("Pressure: %.2f hPa" % lps.pressure) print("Temperature: %.2f C" % lps.temperature) print("") sense.show_message("Press:{:.2f} Tempe:{:.2f} ".format(lps.pressure,lps.temperature), text_colour=[255, 0, 0])

demo script:
disp_question_mark.py

# Adafruit_Blinka/sense_hat coexistence test import time import board import busio import adafruit_lps2x i2c = busio.I2C(board.SCL, board.SDA) lps = adafruit_lps2x.LPS2X(i2c, address=0x5c) #------------------------------------- from time import sleep from sense_hat import SenseHat sense = SenseHat() O = [0, 0, 0] # LED off zeros = [ O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O ] X = [255, 0, 0] # Red O = [255, 255, 255] # White question_mark = [ O, O, O, X, X, O, O, O, O, O, X, O, O, X, O, O, O, O, O, O, O, X, O, O, O, O, O, O, X, O, O, O, O, O, O, X, O, O, O, O, O, O, O, X, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, X, O, O, O, O ] sense.set_pixels(zeros) sleep(4) sense.set_pixels(question_mark) DELAY = 0.5 while True: print("Pressure: %.2f hPa" % lps.pressure) print("Temperature: %.2f C" % lps.temperature) print("") #------------------ sense.rotation = 0 sleep(DELAY) sense.rotation = 90 sleep(DELAY) sense.rotation = 180 sleep(DELAY) sense.rotation = 270 sleep(DELAY)

Adafruit_CircuitPython_Bundleのモジュールのインストール方法

# MODULEというモジュールをインストールする場合、以下を実行する: pip3 install adafruit-circuitpython-MODULE # python3でimportする場合のスクリプト import adafruit_MODULE # ダッシュ(-)とアンダースコア(_)の使い分けに注意すること # 実例: pip3 install adafruit-circuitpython-neopixel_spi # スクリプト例: import adafruit_neopixel_spi # インストールしたモジュールは以下のディレクトリに置かれる $ ls $HOME/.local/lib/python3.7/site-packages/*.py /home/pi/.local/lib/python3.7/site-packages/adafruit_gps.py /home/pi/.local/lib/python3.7/site-packages/adafruit_hts221.py /home/pi/.local/lib/python3.7/site-packages/adafruit_lps2x.py /home/pi/.local/lib/python3.7/site-packages/adafruit_lsm9ds1.py /home/pi/.local/lib/python3.7/site-packages/adafruit_pypixelbuf.py /home/pi/.local/lib/python3.7/site-packages/analogio.py /home/pi/.local/lib/python3.7/site-packages/bitbangio.py /home/pi/.local/lib/python3.7/site-packages/board.py /home/pi/.local/lib/python3.7/site-packages/busio.py /home/pi/.local/lib/python3.7/site-packages/digitalio.py /home/pi/.local/lib/python3.7/site-packages/micropython.py /home/pi/.local/lib/python3.7/site-packages/neopixel_spi.py /home/pi/.local/lib/python3.7/site-packages/neopixel_write.py /home/pi/.local/lib/python3.7/site-packages/pulseio.py

circuitpythonボードで、これらのモジュールを使用する場合、上のファイルをボードのflashにコピーすることになる。

LEDマトリクス(補足)

今回のセンサーには関係ないが、LEDマトリクスは、フレームバッファ/dev/fb1にマッピングされているので、以下のコマンドでも制御できる:

# clear cat /dev/zero | head -c 128 > /dev/fb1 # random cat /dev/urandom | head -c 128 > /dev/fb1

参照情報

Raspberry Pi Sense Hat
Sense HAT API Reference

Pinout for Sense HAT

RaspberryPiのpython3でCircuitPythonのAPIを使用する

CircuitPython on Linux and Raspberry Pi
CircuitPython 5.0.x - I2C

以上

|

« RaspberryPiのファイルシステムにSSHFSでマウントする | トップページ | python3/micro:bit-micropython/CircuitPython用エディタ(mu-editor)をインストールする(linux版) »

linux」カテゴリの記事

CircuitPython」カテゴリの記事

コメント

コメントを書く



(ウェブ上には掲載しません)




« RaspberryPiのファイルシステムにSSHFSでマウントする | トップページ | python3/micro:bit-micropython/CircuitPython用エディタ(mu-editor)をインストールする(linux版) »