ESP32 and MSA301 accelerometer example

esp32 and MSA301 layout
esp32 and MSA301 layout

In this article we look at the MSA301 accelerometer, I originally bought this in the UK and was looking at this for connecting to a Raspberry Pi and using a python library.

There is an Arduino based library as well so we will use the Arduino IDE and connect this up to an ESP32 board.

First of all the sensor

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.

The MSA301 is available in an ultra small (2mm x 2mm,height 1mm) LGA package and is guaranteed to operate over -40°C to +85°C.

FEATURES

  • Ultra small package 2x2x0.91 mm LGA-12 pins
  • 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 (address 0x26)
  • One interrupt pins
  • 14 bits resolution
  • Low power consumption

 

Parts Required

 

Name Link
ESP32 WeMos Mini D1 LOLIN32 ESP32
MSA301
Connecting wire Free shipping Dupont line 120pcs 20cm male to male + male to female and female to female jumper wire

 

Schematic/Connection

esp32 and MSA301 layout
esp32 and MSA301 layout

 

Code Example

You need the Adafruit MSA 301 library and another one called the Bus Register library. Once you have these installed there are a couple of default examples

This is the tap example

// Basic demo for tap/doubletap readings from Adafruit MSA301
 
#include <Adafruit_MSA301.h>
 
Adafruit_MSA301 msa;
 
void setup() {
  Serial.begin(115200);
  while (!Serial) { delay(10); }
 
  // Try to initialize!
  if (! msa.begin()) {
    Serial.println("Failed to find MSA301 chip");
    while (1) { delay(10); }
  }
  Serial.println("Found MSA301!");
 
  msa.setPowerMode(MSA301_NORMALMODE);
  msa.setDataRate(MSA301_DATARATE_1000_HZ);
  msa.setBandwidth(MSA301_BANDWIDTH_500_HZ);
  msa.setRange(MSA301_RANGE_2_G);
  msa.setResolution(MSA301_RESOLUTION_14 );
 
  msa.setClick(false, false, MSA301_TAPDUR_250_MS, 25);
  msa.enableInterrupts(true, true);  // enable single/double tap
}
 
void loop() {
 
  uint8_t motionstat = msa.getMotionInterruptStatus();
  if (motionstat) {
    Serial.print("Tap detected (0x"); Serial.print(motionstat, HEX); Serial.println(")");
    if (motionstat & (1<<5)) {
      Serial.println("\t***Single tap");
    }
    if (motionstat & (1<<4)) {
      Serial.println("\t***Double tap");
    }
    Serial.println("");
  }
  delay(10);
 
}

There is an accelerometer demo and an example which plots data and one which displays readings on an OLED

Output

Open the serial monitor and you should see something like this when you tap the sensor

Tap detected (0x10)
***Double tap

Tap detected (0x10)
***Double tap

Tap detected (0x20)
***Single tap

Tap detected (0x20)
***Single tap

Links

 

 

 

 

SHARE
Previous articleESP32 and TMP006 Infrared Thermopile Sensor example