Home » Raspberry Pi and MSA301 accelerometer python example

Raspberry Pi and MSA301 accelerometer python example

by shedboy71

In this article we look at an accelerometer – this time its the MSA301 and we will connect it to our Raspberry Pi and show how to get readings from it using python

Lets look at some information regarding the sensor, this is from the datasheet

Sensor Information

MSA301 is a triaxial, low-g accelerometer with I2C digital output for consumer applications.

It has dynamical user selectable full scales range of ±2g/±4g/±8g/±16g and allows acceleration measurements with output data rates from 1Hz to 500Hz

FEATURES

User selectable range, ±2g, ±4g, ±8g, ±16g
1.62V to 3.6V supply voltage,
1.2V to 3.6V IO supply voltage
User selectable data output rate
I2C Interface
One interrupt pin
14 bits resolution

 

Parts Required

Here are the parts required with some alternative sources

Name Link
Raspberry Pi 4 Model B Raspberry Pi 4 Model B Development Board

Amazon. com link

Ebay search

MSA-301 https://shop.pimoroni.com/products/msa301-3dof-motion-sensor-breakout
Connecting wire Free shipping Dupont line 120pcs 20cm male to male + male to female and female to female jumper wire

Amazon.com link

Ebay link

Schematic/Connection

The pimoroni board actually can plug straight into the 5 pins on your Raspberry Pi’s GPIO header (pins 1, 3, 5, 6, 9).

Raspberry Pi Sensor
3v3 – Pin 1 2-5v
SDA – Pin 3 SDA
SCL – Pin 5 SCL
Gnd – Pin9 Gnd

Code Example

This uses a library from Pimoroni where I bought the sensor from which you can install like this from the terminal – https://github.com/pimoroni/msa301-python

sudo pip3 install msa301

I then opened the Mu editor and entered the following code example, there are a couple of other examples in the repo that are worth looking at

[codesyntax lang=”python”]

import msa301
import time

print("""detect-axis-move.py - Detect movement and print delta.
Press Ctrl+C to exit.
""")

accel = msa301.MSA301()
accel.reset()
accel.set_power_mode('normal')
accel.enable_interrupt([
    'x_active_interrupt',
    'z_active_interrupt',
    'y_active_interrupt'
    ])

try:
    while True:
        first_x, first_y, first_z = accel.get_measurements()
        print('Move sensor to trigger axis move detect.')
        print (' {0} Detected '.format(
            accel.wait_for_interrupt(
                'active_interrupt')))

        last_x, last_y, last_z = accel.get_measurements()
        print('Movement delta : {:+06.2f}g : {:+06.2f}g : {:+06.2f}g\n'.format(
            first_x-last_x,
            first_y-last_y,
            first_z-last_z))

        time.sleep(0.5)

except KeyboardInterrupt:
    pass

[/codesyntax]

 

 

Output

Run this example and you should see the following, move your Raspberry PI (or tap the sensor)

detect-axis-move.py – Detect movement and print delta.
Press Ctrl+C to exit.

Move sensor to trigger axis move detect.
[‘active_interrupt’] Detected
Movement delta : -00.38g : +00.10g : +00.06g

Move sensor to trigger axis move detect.
[‘active_interrupt’] Detected
Movement delta : +00.14g : -00.13g : -00.03g

Move sensor to trigger axis move detect.
[‘active_interrupt’] Detected
Movement delta : -00.18g : -00.10g : -00.13g

Move sensor to trigger axis move detect.
[‘active_interrupt’] Detected
Movement delta : +00.08g : +00.04g : +00.02g

Links

 

You may also like

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More