Advertisement
DeaD_EyE

ADXL345 micropython wipy 3

Sep 2nd, 2021
1,569
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.18 KB | None | 0 0
  1. """
  2. Dev Board: WiPy 3
  3. Language: Micropython
  4.  
  5. SDA: P09
  6. SCL: P10
  7.  
  8. 3V3: 3V3
  9. GND: GND
  10.  
  11. GY-85 9 DOF 9
  12.  
  13. GY-85 9 DOF 9
  14.  
  15. """
  16.  
  17. import time
  18. import struct
  19. from machine import I2C
  20.  
  21.  
  22. ADXL345_ADDR = 0x53
  23.  
  24.  
  25. class ADXL345:
  26.     RESET = bytearray([0x2d, 0])
  27.     POWER_CTL = bytearray([0x2d, 8])
  28.     DATA_FORMAT_RESET = bytearray([0x31, 0])
  29.     DATA_FORMAT_FULL = bytearray([0x31, 11])
  30.     MEASURE = bytearray([0x32])
  31.    
  32.     def __init__(self, i2c, addr=None):
  33.         self.i2c = i2c
  34.         self.addr = addr or ADXL345_ADDR
  35.         self._reset()
  36.    
  37.     def _write(self, buffer):
  38.         self.i2c.writeto(self.addr, buffer)
  39.        
  40.     def _reset(self):
  41.         self._write(self.RESET)
  42.         self._write(self.POWER_CTL)
  43.         self._write(self.DATA_FORMAT_RESET)
  44.         self._write(self.DATA_FORMAT_FULL)
  45.  
  46.     def get_xyz(self):
  47.         self._write(self.MEASURE)
  48.         time.sleep_ms(1)
  49.         return struct.unpack("<3h", self.i2c.readfrom(self.addr, 6))
  50.  
  51.     @property
  52.     def xyz(self):
  53.         return self.get_xyz()
  54.  
  55.  
  56. i2c = I2C()
  57. itg = ADXL345(i2c)
  58.  
  59.  
  60. while True:
  61.     print("X: {:04d} | Y: {:04d} | Z: {:04d}".format(*itg.xyz))
  62.     time.sleep_ms(100)
  63.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement