-
Connect Vin to the power supply, 3-5V is fine. Use the same
voltage that the microcontroller logic is based off of. For most
Arduinos, that is 5V
- Connect GND to common power/data ground
- Connect the SCL pin to the I2C clock SCL pin on your Arduino. On an UNO & '328 based Arduino, this is also known as A5, on a Mega it is also known as digital 21 and on a Leonardo/Micro, digital 3
- Connect the SDA pin to the I2C data SDA pin on your Arduino. On an UNO & '328 based Arduino, this is also known as A4, on a Mega it is also known as digital 20 and on a Leonardo/Micro, digital 2
Download Libraries
To begin reading sensor data, you will need to download the Adafruit_MMA8451 library and the Adafruit_Sensor library from the Arduino library manager.
Open up the Arduino library manager:
Search for the Adafruit MMA8451 library and install it
Search for the Adafruit Sensor library and install it
We also have a great tutorial on Arduino library installation at:
http://learn.adafruit.com/adafruit-all-about-arduino-libraries-install-use
Load Demo
Open up File->Examples->Adafruit_MMA8451->MMA8451demo and upload to your Arduino wired up to the sensorThere's three lines of output from the sensor.
Example for line 1:
X: 45 Y: -672 Z: 734
This is the "raw count" data from the sensor, its a number from -8192 to 8191 (14 bits) that measures over the set range. The range can be set to 2G, 4G or 8G
Example for line 2:
X: -0.07 Y: 0.09 Z: 9.8 m/s^2
This is the Adafruit_Sensor'ified nice output which is in m/s*s, the SI units for measuring acceleration. No matter what the range is set to, it will give you the same units, so its nice to use this instead of mucking with the raw counts. (Note that the screenshot above has the m/s^2 divided by 10, you can ignore that typo :)
Example for line 3:
Portrait Up Front
This is the output of the orientation detection inside the chip. Since inexpensive accelerometers are often used to detect orientation and tilt, this sensor has it built in. The orientation can be Portrait or Landscape, then Up/Down or Left/Right and finally tilted forward or tilted back. Note that if the sensor is tilted less than 30 degrees it cannot determine the forward/back orientation. If you play with twisting the board around you'll get the hang of it.
Library Reference
The library we have is simple and easy to useYou can create the Adafruit_MMA8451 object with:
Adafruit_MMA8451 mma = Adafruit_MMA8451();
Then initialize the sensor with:
mma.begin()
if (! mma.begin()) { Serial.println("Couldnt start") while (1); } Serial.println("MMA8451 found!");
mma.setRange(MMA8451_RANGE_2_G); mma.setRange(MMA8451_RANGE_4_G); mma.setRange(MMA8451_RANGE_8_G);
mma.getRange()
mma.read();
All three are read in one transaction.
Reading Normalized Adafruit_Sensor data
We recommend using the Adafruit_Sensor interface which allows reading into an event structure. First create a new event structuresensors_event_t event;
mma.getEvent(&event);
Read Orientation
The sensor has built in tilt/orientation detection. You can read the current orientation with
mma.getOrientation();
- 0: Portrait Up Front
- 1: Portrait Up Back
- 2: Portrait Down Front
- 3: Portrait Down Back
- 4: Landscape Right Front
- 5: Landscape Right Back
- 6: Landscape Left Front
- 7: Landscape Left Back
Text editor powered by tinymce.