Pyboard's On-board Peripherals - Accelerometer

Contents

Introduction to Pyboard's Accelerometer

The Pyboard has an onboard accelerometer; an MMA7660 from NXP Semiconductors. It has been listed as end-of-life since around May 2109.

The product datasheet describes it:

The MMA7660FC is a ±1.5 g 3-axis accelerometer with digital output (I2C).

It is a very low power, low profile capacitive MEMS sensor featuring a low pass filter, compensation for 0g offset and gain errors, and conversion to 6-bit digital values at a user configurable samples per second.

The device can be used for sensor data changes, product orientation, and gesture detection through an interrupt pin (INT).

At 6-bit data output this sensor is very low resolution. When it is in operation it uses I2C('X') on pins X9 and X10.

A MicroPython program gains access to the MMA7660FC using methods from the pyb.Accel class.

MicroPython pyb.Accel Class

The pyb.Accel() class is simple and primitive i.e. it only returns raw acceleration values.

Methods

Syntax:
pyb.Accel()
Returns an object that controls the accelerometer.

Syntax:
Accel.x()
Returns the x-axis raw acceleration.
Raw values are between -32 and 31. 

Syntax:
Accel.y()
Returns the y-axis raw acceleration.
Raw values are between -32 and 31.

Syntax:
Accel.z()
Returns the z-axis raw acceleration.
Raw values are between -32 and 31.

Syntax:
Accel.tilt()
Returns the sensor's TILT register.

This register's bits code the sensor's
orientation, tap and shake status.

For an interpretation refer to the
manufacturer's datasheet.
          
Example:

from pyb import Accel

# Instantiate an Accel object
accel = Accel()

# Get x, y, and z acceleration.
x, y, z = accel.x(), accel.y(), accel.z()
print('Raw acceleration values (x, y, z):', (x, y, z))

# Get TILT register.
tilt = accel.tilt()
tilt_str = '0b' + '{:08b}'.format(tilt)
print('TILT register:', tilt_str)

Typical Output:
Raw acceleration values (x, y, z): (-1, -1, 21)
TILT register: 0b00000001
          

The datasheet's explanation for the TILT register's value:

  • Front: Equipment is lying on its front

Extending the 'pyb.Accel' class

The raw acceleration values returned by the methods of the pyb.Accel() class are not particularly helpful. It's usual for acceleration to be reported in units of g  where 1g is the acceleration due to the Earth's gravity.

The following MicroPython code extends the pyb.Accel() to do the acceleration units conversion.


Code:
from pyb import Accel

# Sourced from the datasheet
SENSITIVITY = 21.33 # counts/g

# This class extends the 'pyb.Accel' class.
# It returns x, y, and z axis acceleration
# in units of g.
class Accel_ext(Accel):
    def Xg(self):
        return round(self.x() / SENSITIVITY, 3)
    
    def Yg(self):
        return round(self.y() / SENSITIVITY, 3)
    
    def Zg(self):
        return round(self.z() / SENSITIVITY, 3)    
       
# Test the extended class.
accel = Accel_ext()

x, y, z = accel.x(), accel.y(), accel.z()
xg, yg, zg = accel.Xg(), accel.Yg(), accel.Zg()

print('Raw acceleration (x, y, z):', (x, y, z))
print('Acceleration (g) (x, y, z):', (xg, yg, zg))

Typical Output:
Raw acceleration (x, y, z): (0, -1, 20)
Acceleration (g) (x, y, z): (0.0, -0.047, 0.938)
          

With the Pyboard lying flat on the workbench the x and y axis acceleration should be very close to 0g. The z axis acceleration should be very close to 1g.

This class could be further extended to interpret the meaning of the TILT register bits.