This article describes how to start with BeagleBone and Sparkfun MMA8452 accelerometer.
For this example you need:
- MikroBUS Cape
- Sparkfun MMA8452 accelerometer
To prepare BeagleBone you need to install python-smbus package. Adafruit_I2C library use this package.
SparkFun MMA8452 Accelerometer
This breakout board makes it easy to use the tiny MMA8452Q accelerometer in your project. The MMA8452Q is a smart low-power, three-axis, capacitive MEMS accelerometer with 12 bits of resolution. This accelerometer is packed with embedded functions with flexible user programmable options, configurable to two interrupt pins. Embedded interrupt functions allow for overall power savings relieving the host processor from continuously polling data.
- Standby and active mode
Accelerometer should be in standby mode before you change configuration. Library provide two methods for setting state.
def standby(self): reg_val = self.i2C.readU8(self.CTRL_REG1) reg_val = reg_val & 0xFE # clear the active bit self.i2C.write8(self.CTRL_REG1,reg_val) def active(self): reg_val = self.i2C.readU8(self.CTRL_REG1) reg_val = reg_val | 0x01 # set the active bit self.i2C.write8(self.CTRL_REG1,reg_val)
The MMA8452Q has user selectable full scales of ±2g/±4g/±8g.
def setScale(self, scale): # must be in standby mode reg_val = self.i2C.readU8(self.XYZ_DATA_CFG) reg_val = reg_val & 0xFC scale = scale >> 2 reg_val = reg_val | scale self.i2C.write8(self.XYZ_DATA_CFG,reg_val)
- Output data rate
Output Data Rates (ODR) from 1.56 Hz to 800 Hz
def setODR(self,odr): reg_val = self.i2C.readU8(self.CTRL_REG1) reg_val = reg_val & 0xCF # clear the active bit odr = odr << 3 reg_val = reg_val | odr self.i2C.write8(self.CTRL_REG1,reg_val)
- Configuration method
In configuration method, first put MMA8452 in standby mode. Two parameters should be defined : scale & output data rate. Last command is putting accelerometer in active state.
def config(self, fsr = 2, odr = 0): self.scale = fsr self.standby() self.setScale(self.scale) self.setODR(odr) self.active()
- Reading data
There is two point of interest : availability and reading registers. You should check if any new data is available before reading data. For every axis, data are stored in two register (MSB, LSB). MMA8452 provide 12-bit resolution in two’s complement format.
def available(self): reg_val = self.i2C.readU8(self.STATUS) reg_val = reg_val & 0x08 reg_val = reg_val >> 3 return reg_val def readX(self): reg_XH = self.i2C.readU8(self.OUT_X_MSB) reg_XL = self.i2C.readU8(self.OUT_X_LSB) reg_x = (reg_XH << 8) | (reg_XL) res = reg_x >> 4 res = twos_comp(res,12) return res
For testing you can play with several combinations. The most important option is scale. Change scale parameter from SCALE_2G to SCALE_8G to see effect. Next you can try to change reading methods for x,y,z axis. You have 12-bit resolution, but you could decrease that with shifting with more bits ( >>4 to >>6) and change second parameters in twos_comp function to 10. Do it in readX,Y,Z methods.
Let’s play with acceloremeter!
Comments are not currently available for this post.