CxD Archive
Internet of Things Project
Internet of Things Project
  • IoT Project Background
  • IoT Project Challenge
  • IoT Project Outline
    • 1-1 Smartphone Research
    • 1-2 Smart Thermostat Research
    • 1-3 Smart Device Research
    • 1-4 Introduction to Photon Kit
    • 1-5 Build Smart Lightbulb
    • 1-6 Generate Project Ideas
    • 1-7 Build Smart Thermostat
    • 2-1 Evaluate Project Ideas
    • 2-2 Plan User Research
    • 2-3 Compile Research Data
    • 2-4 Construct Personas
    • 2-5 Construct Journey Map
    • 2-6 Define Value Proposition
    • 2-7 Define Design Requirements
    • 3-1 Generate Solution Ideas
    • 3-2 Evaluate and Refine Solution Ideas
    • 3-3 Create System Model of Solution
    • 3-4 Create Wireflow for Web App
    • 3-5 Create Storyboard for Solution
    • 3-6 Evaluate Solution Design
    • 3-7 Present Project Proposal
    • 4-1 Track Progress and Issues
    • 4-2 Build Smart Device
    • 4-3 Create Photon App
    • 4-4 Create Web App
    • 4-5 Conduct Integration Testing
    • 4-6 Create Product Demo Video
    • 4-7 Create Product Marketing Website
    • 5-1 Evaluate Smart Device and Web App
    • 5-2 Evaluate Product Marketing Website
    • 5-3 Improve Solution Based on Evaluations
    • 6-1 Create Project Poster
    • 6-2 Present Project to Public
    • 6-3 Write Personal Reflection
  • References for Wiring & Coding
    • Creating Circuits
    • Wiring Programming Language
    • Particle Cloud
    • Hello World App for Photon
    • LED Light
    • Push Button
    • Micro OLED Display
    • Humidity and Temp Sensor
    • Speaker
    • Servo Motor
    • Motion Sensor
    • Magnetic Switch
    • Potentiometer
    • Photocell
    • Moisture Sensor
    • Accelerometer
    • RFID Reader
    • Fingerprint Scanner
    • GPS Receiver
    • Camera
    • Microphone
    • Force Sensitive Resistor
    • Keypad
    • Continuous Rotation Servo Motor
  • Glossary
  • Updated Project Guidebook
  • Updated Code Guidebok
Powered by GitBook
On this page
  • Code for Accelerometer
  • #Include Library
  • Global Variables
  • setup( ) function
  • Read Accelerometer Data
Export as PDF
  1. References for Wiring & Coding

Accelerometer

PreviousMoisture SensorNextRFID Reader

Last updated 6 years ago

The 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).

CHANGE IN MOTION

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).

ORIENTATION (TILT)

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.

Accelerometer Pin

Photon Pin

3.3V

3.3V

SDA

SDA/D0

SCL

SCL/D1

I2

(none)

I1

(none)

GND

GND

The accelerometer has 6 pins, but only 4 of them need to be connected.

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):

Code for Accelerometer

#Include Library

Your Photon app must include a code library with special functions that will allow you to use the the accelerometer.

  1. In Particle Build, click on the bookmark icon to open the Libraries sidebar.

  2. Type "mma8452q" into the search field. Select the result called: SPARKFUNMMMA8452Q

  3. Click the button to "Include in Project"

  4. Select the name of your Photon app, and then click the "Confirm" button

  5. Particle Build will automatically insert an #include statement at the top of your app code

// This #include statement was automatically added by the Particle IDE.
#include <SparkFunMMA8452Q.h>

Global Variables

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).

// declare MMA8452Q accelerometer object variable called "accel"
MMA8452Q accel;

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.

// acceleration measurements (raw data)
short accelX, accelY, accelZ;

// acceleration measurements (converted to units of gravity)
float gravityX, gravityY, gravityZ;

// device orientation (portrait vs. landscape)
byte orientation;

// tap count and step count (for activity tracking)
int tapCount, stepCount;

setup( ) function

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).

// Set accelerometer scale to high-resolution (2g) and output data rate to 50Hz
accel.begin(SCALE_2G, ODR_50);

SCALE

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.

ODR

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.

Read Accelerometer Data

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)

ACCELERATION MEASUREMENTS

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.

// if accelerometer measurements available, read data
if (accel.available()) {

    accel.read();

    accelX = accel.x;
    accelY = accel.y;
    accelZ = accel.z;

    gravityX = accel.cx;
    gravityY = accel.cx;
    gravityZ = accel.cx;    

    // add code to do something with measurements

}

ORIENTATION DETECTION

You can detect the orientation (portrait vs. landscape) of your device by using an accel.readPL() statement.

orientation = accel.readPL();

// add code to do something with orientation

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.)

if (orientation == PORTRAIT_U) {

    // add code to do something if "Portrait Up"

}
else if (orientation == PORTRAIT_D) {

    // add code to do something if "Portrait Down"

}
else if (orientation == LANDSCAPE_R) {

    // add code to do something if "Landscape Right"

}
else if (orientation == LANDSCAPE_L) {

    // add code to do something if "Landscape Left"

}
else if (orientation == LOCKOUT) {

    // add code to do something if "Flat"
}

TAP DETECTION

More info coming...

triple-axis accelerometer
Experiment 8
Accelerometer
Accelerometer Axes