Accelerometer
Last updated
Last updated
The triple-axis accelerometer included in your Photon kit can be used to detect changes in motion or orientation in 3 dimensions (x, y, z).
Smartphones and tablets use accelerometers to sense the orientation of the device, in order to change the screen orientation to match. Fitness trackers use accelerometers to count steps and measure other activity. In fact, your accelerometer has built-in functions to detect portrait vs. landscape orientation (useful for device screens), as well as taps (useful for activity trackers).
Like the name implies, an accelerometer works by detecting acceleration – a change in motion. If a device with an accelerometer experiences a change in motion (i.e., speeding up, slowing down, or changing direction), the accelerometer can sense this change and measure the amount of acceleration in each of the 3 dimensions (x, y, z).
Even if a device with an accelerometer is not moving, the accelerometer can detect the orientation (tilt) of the device by measuring the acceleration due to Earth's gravity, which is a constant downward force acting on all objects. The accelerometer can determine if the object is parallel to the Earth's surface or if it is tilted – and can measure the amount of tilt for each of the 3 dimension (x, y, z).
Line up the accelerometer's pins with different numbered rows on a breadboard, and push down to insert the accelerometer pins. Then use jumper wires to connect these rows to the Photon.
The accelerometer has 6 pins, but only 4 of them need to be connected.
Experiment 8 of the online SparkFun Photon Experiment Guide shows how to connect the accelerometer. Here is the connection diagram for Experiment 8 (ignore the wiring for the push button):
Your Photon app must include a code library with special functions that will allow you to use the the accelerometer.
In Particle Build, click on the bookmark icon to open the Libraries sidebar.
Type "mma8452q" into the search field. Select the result called: SPARKFUNMMMA8452Q
Click the button to "Include in Project"
Select the name of your Photon app, and then click the "Confirm" button
Particle Build will automatically insert an #include
statement at the top of your app code
In your global variables, you need to declare a MMA8452Q object variable that you will use to run the special functions in the library. The example below declares an object variable called "accel" (but you could use a different variable name).
You will probably also want to declare global variables for the data from the accelerometer. You only need to include variables for the types of data that your Photon app will actually use. If you want, use can use different names for these variables.
You have to include an accel.begin()
statement within the setup()
function to initialize the accelerometer by setting its scale range (SCALE) and output data rate (ODR).
The accelerometer scale (SCALE) can be set to ± 2, 4, or 8 g (where g is a unit representing Earth's gravity, which is about 9.8 m/s2). The options for setting the scale are designated as: SCALE_2G
, SCALE_4G
, or SCALE_8G
. A higher scale can detect larger changes in acceleration but is less precise. A lower scale is more precise. For your application, using SCALE_2G
is the probably the best choice because it will be the most precise.
The accelerometer output data rate (ODR) determines how frequently it gathers new measurements. The ODR is set in Hz (number of times per second). The options for setting the ODR are designated as: ODR_1
, ODR_6
, ODR_12
, ODR_50
, ODR_100
, ODR_200
, ODR_400
, or ODR_800
. A higher ODR is faster (more frequent), while a lower ODR is slower (less frequent). For your application, using ODR_50
is probably a good choice because it's not too fast and not too slow.
Code for reading the accelerometer data would most likely be placed within the loop()
function or within a custom function.
Your accelerometer can provide three different types of data:
Acceleration Measurements (x, y, z)
Orientation Detection (portrait vs. landscape)
Tap Detection (good for activity tracking)
Every time you want to get new acceleration measurements, you first have to call a special function called accel.available()
. If new measurements are available (which depends on the ODR), the function will return a value of 1 (true). If so, then you can read the new measurements by using an accel.read()
statement.
You can detect the orientation (portrait vs. landscape) of your device by using an accel.readPL()
statement.
The possible values returned for accel.readPL()
are:
PORTRAIT_U
(Portrait Up)
PORTRAIT_D
(Portrait Down)
LANDSCAPE_R
(Landscape Right)
LANDSCAPE_L
(Landscape Left)
LOCKOUT
("Flat" - neither portrait or landscape)
NOTE: You may need to determine the proper physical arrangement of the accelerometer on your device, so that you can accurately read the device orientation.
If your app needed to do something different for each possible orientation value, the code below shows one way to accomplish this. (Another way is to use a switch()
statement instead of if-else statements.)
More info coming...
Accelerometer Pin
Photon Pin
3.3V
3.3V
SDA
SDA/D0
SCL
SCL/D1
I2
(none)
I1
(none)
GND
GND