reading from MMA8452Q

296 views
Skip to first unread message

John Nivard

unread,
Apr 19, 2013, 11:42:11 AM4/19/13
to quick2wi...@googlegroups.com
Hai

I am starting to get the MMA8452Q to work. It is connected to the i2c connector of the interface board and I have now 3 i2cdevices showing up. The MMA8452Q came mouted on a small board with only the interup lines and the data and clocl lines comming out. THe MMA is a xyz accelerometer.


#!/usr/bin/python3
#By John Nivard april 2013
import sys
#sys.path.append
from time import sleep
from quick2wire.i2c import I2CMaster, writing_bytes, reading
#
#   reading OUT_X_MSB,OUT_X_LSB   most significant bits MSB,
#           OUT_X(Y,Z)_MSB and OUT_X(Y,Z)_LSB
#   F_READ bit in CTRL_REG1 must be set for a fast read. In fast read mode
#   the LSB registers are ignored
#   Interrupt register  configuration
#

#register address of relevant registers in the MMA8452Q
#
address  =  0x1D # port number of the MMA8452Q
STATUS   =  0x00 # Status register
OUT_X_MSB=  0x01 # [7:0] are 8 MSB of 12 bit sample
OUT_Y_MSB=  0x01 # [7:0] are 8 MSB of 12 bit sample
OUT_Z_MSB=  0x01 # [7:0] are 8 MSB of 12 bit sample
CTRL_REG1=  0x2A # [] 
BUF      =  0

with I2CMaster () as i2c :
    #set fast read active bit 1 and set active bit 0  page 38 of MMA8452Q 
    i2c.transaction(writing_bytes(address, CTRL_REG1, 0b00000011))
    BUF = i2c.transaction(reading (address, 1))
    print ("CTRL_REG1=" , BUF)

    while True:
        #write address to read OUT_X_MSB
        i2c.transaction(writing_bytes(address, OUT_X_MSB))
        BUF = i2c.transaction(reading (address, 1))
        #
        print (BUF)
        sleep (0.5)

The output is as follows

[b' \xff']
[b' \xff']
[b' \xff']

It is not changing when I move the meter

I think my set up is ok but I am not 100% shure of it

John


Andrew Scheller

unread,
Apr 19, 2013, 11:58:34 AM4/19/13
to quick2wi...@googlegroups.com
> I am starting to get the MMA8452Q to work. It is connected to the i2c
> connector of the interface board and I have now 3 i2cdevices showing up. The
[snip]
> The output is as follows
> [b' \xff']
> [b' \xff']
> [b' \xff']
>
> It is not changing when I move the meter
> I think my set up is ok but I am not 100% shure of it

I thought you'd already determined that this chip wouldn't work with
the RaspPi because of the I2C repeated-start issue??

Andrew

John Nivard

unread,
Apr 19, 2013, 12:27:44 PM4/19/13
to quick2wi...@googlegroups.com
Hai Andrew,

Romilly did think there was nothing to prefent the MMA to communicate with the PI. 
So I am starting to make a try. I am not shure if aI do the write and read in the good way,
The first thing I do is reading the WHO_IAM register but this give thge same result [b' \xff']

#!/usr/bin/python3
#By John Nivard april 2013
import sys
#sys.path.append
from time import sleep
from quick2wire.i2c import I2CMaster, writing_bytes, reading
#
#   reading OUT_X_MSB,OUT_X_LSB   most significant bits MSB,
#           OUT_X(Y,Z)_MSB and OUT_X(Y,Z)_LSB
#   F_READ bit in CTRL_REG1 must be set for a fast read. In fast read mode
#   the LSB registers are ignored
#   Interrupt register  configuration
#

#register address of relevant registers in the MMA8452Q
#
address  =  0x1D # port number of the MMA8452Q
STATUS   =  0x00 # Status register
OUT_X_MSB=  0x01 # [7:0] are 8 MSB of 12 bit sample
OUT_Y_MSB=  0x03 # [7:0] are 8 MSB of 12 bit sample
OUT_Z_MSB=  0x05 # [7:0] are 8 MSB of 12 bit sample
CTRL_REG1=  0x2A # []
SYSMODE  =  0x0B # current system mode
WHO_AM_I =  0x0D #
BUF      =  0

with I2CMaster () as i2c :
    #WHO_AM_I
    i2c.transaction(writing_bytes(address, WHO_AM_I))
    BUF = i2c.transaction(reading (address, 1))
    print ("WHO_AM_I=" , BUF)   
    #read current system mode  
    i2c.transaction(writing_bytes(address, SYSMODE))
    BUF = i2c.transaction(reading (address, 1))
    print ("SYSMODE=" , BUF)   

    
    #set fast read active bit 1 and set active bit 0  page 38 of MMA8452Q 
    #i2c.transaction(writing_bytes(address, CTRL_REG1, 0b00000011))
    #BUF = i2c.transaction(reading (address, 1))
    #print ("CTRL_REG1=" , BUF)
    while True:
        #write address to read OUT_X_MSB
        i2c.transaction(writing_bytes(address, OUT_X_MSB))
        BUF = i2c.transaction(reading (address, 1))
        #
        print (BUF)
        sleep (0.5)

Thomas Preston

unread,
Jan 17, 2014, 9:30:54 AM1/17/14
to quick2wi...@googlegroups.com
Did anyone get anywhere with this? I'm having the same issue. I can read from the chip using SMBus (python-smbus) but this only works in Python 2. I need Python 3 support.

DEFAULT_I2C_ADDRESS = 0x1d
DEFAULT_I2C_BUS = 1

# register addresses
SYSMOD = 0x0B
XYZ_DATA_CFG = 0x0E
CTRL_REG1 = 0x2A


class
MMA8452Q(object):
    """MMA8452Q Accelerometer."""
    def __init__(self, address=DEFAULT_I2C_ADDRESS, bus_num=DEFAULT_I2C_BUS):
        self.i2c_address = address
        self.i2c_bus = smbus.SMBus(bus_num)
        self.g_mul = 0  # G-force multiplier (to get G-force from interval)

    def init(self):
        """Initalises the accelerometer."""
        # Sleep rate 50 Hz, Data rate 800 Hz,
        # No reduced noise mode, Normal read mode, Active
        config = 0x3
        self.i2c_bus.write_byte_data(self.i2c_address, CTRL_REG1, config)

        # Setup range
        # register = 0x0E             # XYZ_DATA_CFG
        # data = 0x00                 # Range 2G, no high pass filtering
        xyz_config = 0x00
        self.i2c_bus.write_byte_data(self.i2c_address,
                                     XYZ_DATA_CFG,
                                     xyz_config)
        self.g_mul = 2.0 / 128       # 2G over 128 counts

        # System state
        # register = 0x0B             # SYSMOD
        sysmod_config = 0x01                 # Awake mode
        self.i2c_bus.write_byte_data(self.i2c_address, SYSMOD, sysmod_config)

        # Reset ready for reading
        self.i2c_bus.write_byte(self.i2c_address, 0x00)

    def get_xyz(self):
        status, x, y, z = self.i2c_bus.read_i2c_block_data(self.i2c_address,
                                                           0,
                                                           4)
        # convert from unsigned to signed values
        bytes = struct.pack('BBB', x, y, z)
        x, y, z = struct.unpack('bbb', bytes)
        # conver to g-force
        return x * self.g_mul, y * self.g_mul, z * self.g_mul
Reply all
Reply to author
Forward
0 new messages