I am a newbie at using i2C and working with registers and reading and writing bits. I am trying to create python program that will use an MMA8452Q accelerometer to measure the vibration produced by a device I am testing. I have managed to get a program that spits out some data, but I am not getting all the data. I am pretty sure I am being limited by how I have the code set up.

After reading through the spec sheet, I know I can set the data rate up to 800Hz and that I can poll the accelerometer to tell me when new data is available. When the status of the ZYXDR bit changes new data is available but I don't know how to monitor the status of that bit.

The read commands for I2C over SMBus seem to be reading the whole register at one time.


How do I know that just one bit has changed?

1 Answer 1



For an explanation see: https://stackoverflow.com/questions/10493411/what-is-bit-masking

If we assume that the ZYXDR is bit 2 then we construct a mask in python by defining a binary literal, then we use the bitwise-and (python wiki) to extract that bit of data.

ZYXDR_MASK = 0b00000100
val = read_byte_data(add,cmd)
if (val & ZYXDR_MASK): 
  print "zyxdr bit set"
  print "zyxdr bit not set"

Typically, when writing a driver or interface code, one defines many constants representing the masks and bit shifts

There are alternatives which require other libraries, you may consider using bitstring

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.