Using AS5600 with RPi Pico

少し前に入手したAS5600を試す。
このセンサは、磁気回転位置を読み取るモノで、DJがレコードをキュッキュと回してる動作を実装できないかと、購入したみた。
Amazon.co.jp: Ahvqevn 4個 AS5600磁気エンコーダ磁気誘導角度測定センサーモジュール12ビット…
https://www.amazon.co.jp/dp/B0CQZV489Z
Aliexpressだと、送料込みで1個350円くらい。
部材
- AS5600
- RPi pico(互換品)
- 3Dプリントしたクロージャー
- XHコネクタとケーブル
- SSD1306
- ミニチュアベアリング 内径8mm×外径22mm×幅7mm
クロージャー
磁気なので、センサの上にクルクル回転できる磁石が必要。そのための部品は、3Dプリンタで作成。
モデルデータは、thingiverse から取得。
ただし、センサの裏側にXH端子を出すために、底部は底上げした。
配線
AS5600 の裏側からXH端子の3PINと4PINを半田付け
0から360度の回転位置のを、PWM OUT と I2Cの2つの方法で取得できるけど、素直にI2Cにする。
- SCL .. GP1
- SDA ... GP0
program
circuitpython 9.0.4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import board | |
import displayio | |
import adafruit_ssd1306 | |
import busio | |
from adafruit_bus_device.i2c_device import I2CDevice | |
# AS5600 の I2C アドレス | |
AS5600_I2C_ADDRESS = 0x36 | |
# レジスタアドレス | |
ANGLE_REG = 0x0E | |
displayio.release_displays() | |
# I2C 初期化 | |
i2c = busio.I2C(board.GP1, board.GP0) | |
# Create the SSD1306 OLED class. | |
display_width = 128 | |
display_height = 64 | |
display = adafruit_ssd1306.SSD1306_I2C(display_width, display_height, i2c) | |
# | |
# AS5600 デバイスの初期化 | |
# | |
device = I2CDevice(i2c, AS5600_I2C_ADDRESS) | |
def read_angle(): | |
with device: | |
# 2 バイトの角度データを読み取る | |
angle_data = bytearray(2) | |
device.write_then_readinto(bytearray([ANGLE_REG]), angle_data) | |
# 角度データを計算する | |
angle = (angle_data[0] << 8) | angle_data[1] | |
return angle * 0.08789 # 角度を度に変換 (360 / 4096) | |
count = 0 | |
pre_angle = -1 | |
WIDTH=128 | |
fact = WIDTH/2/180 | |
while True: | |
angle = read_angle() | |
if count % 2 == 0: | |
if int(pre_angle) == int(angle): | |
continue | |
print("{:.2f}".format(angle)) | |
display.fill(0) | |
display.text('{:.1f}'.format(angle), 12, 4, 1) | |
if angle >= 180: | |
# center +---- | |
display.rect(int(WIDTH/2), 20, int((angle-180)*fact), 20, 1) | |
else: | |
# ----+ center | |
display.rect(int(angle*fact), 20, 64-int(angle*fact), 20, 1) | |
display.show() | |
pre_angle = angle | |
count += 1 | |
demo
動画プレーヤー
00:00
00:00