- Connect Metro or Arduino 5V to board VIN
- Connect Metro or Arduino GND to board GND
- Connect Metro or Arduino SCL to board SCL
- Connect Metro or Arduino SDA to board SDA
The final results should resemble the illustration above, showing an Adafruit Metro development board.
Installation
We've written a sensor library for you for the MSA301 which uses our Adafruit_Sensor and BusIO libraries. You can install both the Adafruit_MSA301 Library and the Adafruit BusIO Library for Arduino using the Library Manager in the Arduino IDE:
Click the Manage Libraries ... menu item, search for Adafruit MSA301, and select the Adafruit_MSA301 library:
Then follow the same process for the Adafruit BusIO library:
Finally do the same for the Adafruit Unified Sensor library
Accelerometer Demo
This first demo will show you how to get readings of what an accelerometer does best: measure acceleration!
Open up File -> Examples -> Adafruit MSA301 -> acceldemo and upload to your Arduino wired up to the sensor.
Upload the sketch to your board and open up the Serial Monitor (Tools -> Serial Monitor) at 115200 baud. You should see the the values for the current configuration settings printed on startup, followed by acceleration readings for the X, Y, and Z axes similar to this:
You can tilt the sensor in different directions to see the values for the different axes change.
Tap Demo
This next demo will demonstrate how to use the tap detection feature of the MSA301. By tapping the accelerometer or the PCB or something the PCB is attached to you can create a type of interface.
Open up File -> Examples -> Adafruit MSA301 -> tapdemo and upload to your Arduino wired up to the sensor.
Once the sketch is loaded, open up the Serial Monitor (Tools -> Serial Monitor) at 115200 baud. When you tap, you should see the sketch report that a tap was detected:
The tap detection works by looking for when one of the axes has an acceleration higher than a certain threshold followed by a quiet period where there are no more acceleration spikes above the threshold.
After an initial "spike" of acceleration above the threshold, any additional spikes within a window of time defined by the shock parameter are ignored.
For double tap detection, after an initial tap has been detected, another tap event within the window of time specified by the duration parameter will trigger a double tap interrupt if they are enabled.
The tap detection parameters are set by calling setClick:
msa.setClick(false, false, MSA301_TAPDUR_250_MS, 25); // Method signature: // void setClick(bool tap_quiet, bool tap_shock, msa301_tapduration_t tapduration, uint8_t tapthresh);
The threshold is in 'raw' values so you have to adjust it based on the scale/range you've configured for the sensor. Larger numbers are less sensitive, you'll really need to just tweak as necessary for your project.
The duration parameter only applies to double tap detection. It is specified by passing an msa301_tapduration_t. A shorter duration will require a quicker double tap to be detected.
The quiet duration is either 20ms if true or 30ms if false. Similarly the shock duration is either 70ms if true or 50ms if false.
Additionally after configuring tap detection, you will need to enable the tap detection interrupts:
msa.enableInterrupts(true, true); // enable single, double tap
If you wish to have the tap detection activate the interrupt pin, you will have to enable that as well:
msa.mapInterruptPin(true, true); // enable the INT pin for single, double tap
Acceleration Example Code
The following code is part of the standard library and illustrates the basic function of measuring acceleration:
// Basic demo for accelerometer readings from Adafruit MSA301 #include <Wire.h> #include <Adafruit_MSA301.h> #include <Adafruit_Sensor.h> Adafruit_MSA301 msa; void setup(void) { Serial.begin(115200); while (!Serial) delay(10); // will pause Zero, Leonardo, etc until serial console opens Serial.println("Adafruit MSA301 test!"); // Try to initialize! if (! msa.begin()) { Serial.println("Failed to find MSA301 chip"); while (1) { delay(10); } } Serial.println("MSA301 Found!"); //msa.setDataRate(MSA301_DATARATE_31_25_HZ); Serial.print("Data rate set to: "); switch (msa.getDataRate()) { case MSA301_DATARATE_1_HZ: Serial.println("1 Hz"); break; case MSA301_DATARATE_1_95_HZ: Serial.println("1.95 Hz"); break; case MSA301_DATARATE_3_9_HZ: Serial.println("3.9 Hz"); break; case MSA301_DATARATE_7_81_HZ: Serial.println("7.81 Hz"); break; case MSA301_DATARATE_15_63_HZ: Serial.println("15.63 Hz"); break; case MSA301_DATARATE_31_25_HZ: Serial.println("31.25 Hz"); break; case MSA301_DATARATE_62_5_HZ: Serial.println("62.5 Hz"); break; case MSA301_DATARATE_125_HZ: Serial.println("125 Hz"); break; case MSA301_DATARATE_250_HZ: Serial.println("250 Hz"); break; case MSA301_DATARATE_500_HZ: Serial.println("500 Hz"); break; case MSA301_DATARATE_1000_HZ: Serial.println("1000 Hz"); break; } //msa.setPowerMode(MSA301_SUSPENDMODE); Serial.print("Power mode set to: "); switch (msa.getPowerMode()) { case MSA301_NORMALMODE: Serial.println("Normal"); break; case MSA301_LOWPOWERMODE: Serial.println("Low Power"); break; case MSA301_SUSPENDMODE: Serial.println("Suspend"); break; } //msa.setBandwidth(MSA301_BANDWIDTH_31_25_HZ); Serial.print("Bandwidth set to: "); switch (msa.getBandwidth()) { case MSA301_BANDWIDTH_1_95_HZ: Serial.println("1.95 Hz"); break; case MSA301_BANDWIDTH_3_9_HZ: Serial.println("3.9 Hz"); break; case MSA301_BANDWIDTH_7_81_HZ: Serial.println("7.81 Hz"); break; case MSA301_BANDWIDTH_15_63_HZ: Serial.println("15.63 Hz"); break; case MSA301_BANDWIDTH_31_25_HZ: Serial.println("31.25 Hz"); break; case MSA301_BANDWIDTH_62_5_HZ: Serial.println("62.5 Hz"); break; case MSA301_BANDWIDTH_125_HZ: Serial.println("125 Hz"); break; case MSA301_BANDWIDTH_250_HZ: Serial.println("250 Hz"); break; case MSA301_BANDWIDTH_500_HZ: Serial.println("500 Hz"); break; } //msa.setRange(MSA301_RANGE_2_G); Serial.print("Range set to: "); switch (msa.getRange()) { case MSA301_RANGE_2_G: Serial.println("+-2G"); break; case MSA301_RANGE_4_G: Serial.println("+-4G"); break; case MSA301_RANGE_8_G: Serial.println("+-8G"); break; case MSA301_RANGE_16_G: Serial.println("+-16G"); break; } //msa.setResolution(MSA301_RESOLUTION_14 ); Serial.print("Resolution set to: "); switch (msa.getResolution()) { case MSA301_RESOLUTION_14: Serial.println("14 bits"); break; case MSA301_RESOLUTION_12: Serial.println("12 bits"); break; case MSA301_RESOLUTION_10: Serial.println("10 bits"); break; case MSA301_RESOLUTION_8: Serial.println("8 bits"); break; } } void loop() { msa.read(); // get X Y and Z data at once // Then print out the raw data Serial.print("X: "); Serial.print(msa.x); Serial.print(" \tY: "); Serial.print(msa.y); Serial.print(" \tZ: "); Serial.print(msa.z); delay(100); /* Or....get a new sensor event, normalized */ sensors_event_t event; msa.getEvent(&event); /* Display the results (acceleration is measured in m/s^2) */ Serial.print("\t\tX: "); Serial.print(event.acceleration.x); Serial.print(" \tY: "); Serial.print(event.acceleration.y); Serial.print(" \tZ: "); Serial.print(event.acceleration.z); Serial.println(" m/s^2 "); Serial.println(); delay(100); }
// 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); }
Page last edited January 22, 2025
Text editor powered by tinymce.