3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

ラズパイ・ピコ(Raspberry Pi Pico)のmicropythonで3軸加速度センサー(ADXL345)を動作させる

Posted at

開発環境(IDE)

今回の開発の環境にはThonny https://thonny.org/ を使用しました。
3軸加速度センサー(ADXL345)は https://akizukidenshi.com/catalog/g/gM-06724/を使用し、I2Cで通信させてます。
MicoroPythonはRaspberry Pi PicoのGetting StartedよりダウンロードしRaspberry Pi Picoに書き込んでください。

接続例

以下のように接続します。
ジャンパーワイヤの色が統一感が無いのはDEBUGで一部不良だったため適当な色に交換しています。その影響で分かりにくいかもしれません。

P_20210227_174128_vHDR_Auto.jpg

コード例

動作したコードを置いておきます。
若干、無駄なような記述がありますが、動作した時点のコードですのでご容赦ください。

from machine import Pin
from machine import I2C
import time
import ustruct

DATA_FORMAT         = 0x31
BW_RATE             = 0x2c
POWER_CTL           = 0x2d
INT_ENABLE          = 0x2E

BW_RATE_1600HZ      = 0x0F
BW_RATE_800HZ       = 0x0E
BW_RATE_400HZ       = 0x0D
BW_RATE_200HZ       = 0x0C
BW_RATE_100HZ       = 0x0B
BW_RATE_50HZ        = 0x0A
BW_RATE_25HZ        = 0x09

RANGE_2G            = 0x00
RANGE_4G            = 0x01
RANGE_8G            = 0x02
RANGE_16G           = 0x03

OFSX = 0x1e
OFSY =0x1f
OFSZ =0x20

class adxl345:
    def __init__(self, scl, sda):
        self.scl = scl
        self.sda = sda
        self.i2c = I2C(0,scl = self.scl, sda = self.sda, freq = 100000)
        slv = self.i2c.scan()
        for s in slv:
            buf = self.i2c.readfrom_mem(s, 0, 1)
            if(buf[0] == 0xe5):
                self.slvAddr = s
                print('adxl345 found')
            break
        #self.writeByte(POWER_CTL,0x00)  #sleep
        #time.sleep(0.001)
        self.writeByte(DATA_FORMAT,0x2B)
        self.writeByte(BW_RATE,0x0A)
        self.writeByte(INT_ENABLE,0x00)

        self.writeByte(OFSX,0x00)
        self.writeByte(OFSY,0x00)
        self.writeByte(OFSZ,0x00)
 
        self.writeByte(POWER_CTL,0x28)
        time.sleep(1)

    def readXYZ(self):
        fmt = '<h' #little-endian
        buf1 = self.readByte(0x32)
        buf2 = self.readByte(0x33)
        buf = bytearray([buf1[0], buf2[0]])
        x, = ustruct.unpack(fmt, buf)
        x = x*3.9

        buf1 = self.readByte(0x34)
        buf2 = self.readByte(0x35)
        buf = bytearray([buf1[0], buf2[0]])
        y, = ustruct.unpack(fmt, buf)
        y = y*3.9

        buf1 = self.readByte(0x36)
        buf2 = self.readByte(0x37)
        buf = bytearray([buf1[0], buf2[0]])
        z, = ustruct.unpack(fmt, buf)
        z = z*3.9
        return (x,y,z)

    def writeByte(self, addr, data):
        d = bytearray([data])
        self.i2c.writeto_mem(self.slvAddr, addr, d)
 
    def readByte(self, addr):
        return self.i2c.readfrom_mem(self.slvAddr, addr, 1)


scl = Pin(17)
sda = Pin(16)
snsr = adxl345(scl, sda)
while True:
    x,y,z = snsr.readXYZ()
    print('x:',x,'y:',y,'z:',z,'uint:mg')
    time.sleep(0.5)


実行風景

P_20210227_175745_vHDR_Auto.jpg

参考文献

Raspberry Pi PicoのmicroPythonでI2CとADCを使ってみる
https://qiita.com/SamAkada/items/07f4929a9fcfbfd942bc

【以上】

3
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?