In this article we look at an accelerometer – this time its the MSA301 and we will connect this to an Adafruit Feather M0 running Circuitpython
Lets look at some information regarding the sensor, this is from the datasheet
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
Schematic/Connection
Code Example(s)
I used Mu for development
The following is based on a library , I copied the adafruit_msa301 library for this device to the lib folder on my Feather M0 Express – https://circuitpython.org/libraries
This is the basic example which comes with the library for tap detection
Example 1
You may have to play about with the time.sleep(0.05) value, the original value was 0.01 but it seemed to be picking up multiple taps
[codesyntax lang=”python”]
import time import board import busio import adafruit_msa301 i2c = busio.I2C(board.SCL, board.SDA) msa = adafruit_msa301.MSA301(i2c) msa.enable_tap_detection() while True: if msa.tapped: print("Single Tap!") time.sleep(0.05)
[/codesyntax]
Here is what I saw in Mu REPL window
Auto-reload is on. Simply save files over USB to run them or enter REPL to disable.
main.py output:
Single Tap!
Single Tap!
Single Tap!
Example 2
This is a simple accelerometer example
[codesyntax lang=”python”]
import time import board import busio import adafruit_msa301 i2c = busio.I2C(board.SCL, board.SDA) msa = adafruit_msa301.MSA301(i2c) while True: print("%f %f %f" % msa.acceleration) time.sleep(0.5)
[/codesyntax]
Here is what I saw in Mu REPL window
main.py output:
-3.758647 1.862565 6.937935
-3.744283 1.876929 6.990604
-3.744283 1.872141 6.928360
-5.898922 1.220962 10.567303
-1.570492 -3.160136 4.744993
Links