I2c problems reading data ADXL345 WEMOS MINI


#1

Hi im traying to use i2c for ADXL345 MODULE , and i have the next output

5528

PRINT BUFFER :

65418

65508

65527

PRINT BUFFER :

65419

65508

65527

PRINT BUFFER :

65419

65508

65528

PRINT BUFFER :

65418

65508

65527
Its just happens when it has negative values =( in positive values works fine
Here is mi code:

----------------------------------------constants.py----------------------------------------
D_ADDR = 0X53 # DEVICE ADDR
#---------------------------------# DEVICE CONFIGURATION
DATA_FORMAT = 0X31
RANGE = 0X01
#---------------------------------# DEVICE CONFIGURATION
POWER_CTL = 0X2D
AUTO_SLEEP = 0X10
MEASURE = 0X08
WAKEUP = 0X00
#---------------------------------# DATA X Y Z
DATA = 0X32
DATA_SIZE = 6
#---------------------------------#
CLK = 100000

----------------------------------------main.py----------------------------------------

ADXL345

Created at 2019-03-27 19:47:34.705783

import streams
import i2c
import constants as cn
streams.serial()

def writeTo(REGISTER,DATA):
obj.start()
obj.write_bytes(REGISTER)
obj.write_bytes(DATA)
obj.stop()

def readFrom(REGISTER,NB2_READ):
obj.start()
BUFFER = obj.write_read(cn.DATA,cn.DATA_SIZE)
obj.stop()
return BUFFER

def showData(BUFFER):
print("PRINT BUFFER : ")
print(int( (BUFFER[1] << 8 ) | BUFFER[0] ))
print(int( (BUFFER[3] << 8 ) | BUFFER[2] ))
print(int( (BUFFER[5] << 8 ) | BUFFER[4] ))

obj = i2c.I2C(I2C0,cn.D_ADDR,cn.CLK)
writeTo(cn.DATA_FORMAT,cn.RANGE)
writeTo(cn.POWER_CTL,cn.WAKEUP)
writeTo(cn.POWER_CTL,cn.AUTO_SLEEP)
writeTo(cn.POWER_CTL,cn.MEASURE)

while True:
BUFF = readFrom(cn.DATA,cn.DATA_SIZE)
showData(BUFF)
sleep(1000)


#3

i found my error , the device return’s the value in twos complement , but ty anyway, regards

My solution :slight_smile: ::::

def showData(BUFFER):
print("PRINT BUFFER : ")
x_roll = (BUFFER[1] << 8) | BUFFER[0]
y_pitch = (BUFFER[3] << 8) | BUFFER[2]
z_yaw = (BUFFER[5] << 8) | BUFFER[4]

print(twos_complement(x_roll))
print(",")
print(twos_complement(y_pitch))
print(",")
print(twos_complement(z_yaw),end="\n")

def twos_complement(axes):
if(axes & (1 << 15)):# if the format are in 2’s?
axes = axes - (1 << 16)
return axes


#4

Hello @epsilon11101,

great that you found the issue. BTW I would like to let you know that, if you wish, you can release the support to the ADXL345 chip as a community library :slight_smile: