Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- Dev Board: WiPy 3
- Language: Micropython
- SDA: P09
- SCL: P10
- 3V3: 3V3
- GND: GND
- GY-85 9 DOF 9
- GY-85 9 DOF 9
- """
- import time
- import struct
- from machine import I2C
- ADXL345_ADDR = 0x53
- class ADXL345:
- RESET = bytearray([0x2d, 0])
- POWER_CTL = bytearray([0x2d, 8])
- DATA_FORMAT_RESET = bytearray([0x31, 0])
- DATA_FORMAT_FULL = bytearray([0x31, 11])
- MEASURE = bytearray([0x32])
- def __init__(self, i2c, addr=None):
- self.i2c = i2c
- self.addr = addr or ADXL345_ADDR
- self._reset()
- def _write(self, buffer):
- self.i2c.writeto(self.addr, buffer)
- def _reset(self):
- self._write(self.RESET)
- self._write(self.POWER_CTL)
- self._write(self.DATA_FORMAT_RESET)
- self._write(self.DATA_FORMAT_FULL)
- def get_xyz(self):
- self._write(self.MEASURE)
- time.sleep_ms(1)
- return struct.unpack("<3h", self.i2c.readfrom(self.addr, 6))
- @property
- def xyz(self):
- return self.get_xyz()
- i2c = I2C()
- itg = ADXL345(i2c)
- while True:
- print("X: {:04d} | Y: {:04d} | Z: {:04d}".format(*itg.xyz))
- time.sleep_ms(100)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement