Introduction
This Module is a 3-axis Xyz accelerometer using Freescale MMA7361L micromachined ±1.5g or ±6g sensitivity. It’s great for a motion sensing or stability control project e.g in robotics, anti-theft devices or even game pads. It’s simple to interface to any microcontroller as the XYZ axes outputs analog values.
Pinout
Description
– 0g-Detect pin is used to detect free fall, that is, if all the three axes simultaneiusly detect 0g.
– The X, Y and Z axis ouputs analog volatge centered at VDD/2, positive accelerations will results in a value higher than VDD/2 and negative accelerations will result in a value lower than VDD/2.
– Sleep pin is used to put the sensor into low-power mode which is crucial especially in battery powered applications. This pin is internally pulled low and you must pull it high when using the sensor either by connecting it directly to VDD or to microcontroller pin if you want to control it.
– The g-Select pin is used to set the sensitivity of the sensor. When pulled down its ±1.5g (800 mV/g) or pulled up its ±6g (206 mV/g).
– VDD is usually powered from 2.7V to 3.6V power supply therefore the board is not 5V tolerant.
Part List
We’ll connect the module to an Arduino board. Part list needed.
– MMA7316lL accelerometer module
– Arduino Pro Mini
– FTDI FT232RL USB-Serial (for uploading code to pro mini)
– Mini breadboard
– Jumper packs (Male-Male)
Wiring
We are connecting the MMA7316 module to an Arduino pro mini running at 8Mhz, 3.3V. Since our power supply is 3.3V we dont need a level converter I.C for this tutorial. To upload code to the Arduino pro mini, we’ll use FTDI FT232RL USB-Serial module.
Sensor-Arduino pin connections
Module | Arduino Pro Mini |
---|---|
0g-Detect | NC |
X | A0 |
Y | A1 |
Z | A2 |
Ground | Ground |
Vcc | Vcc(3v) |
Ground | Ground |
Sleep | Vcc (3V) |
g-Select | VCC (6g) or Ground (1.5g) |
Self Test | NC |
Breadboard Connections
Code
Here’s the Arduino code for our tutorial
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
/* * * Connections * ------------------- * Sensor | Pro Mini * ------------------- * X | A0 * Y | A1 * Z | A2 * VCC | 3.3V * GND | GND * SLEEP | 3.3V * ------------------- * */ //macro definnitions #define a_x A0 #define a_y A1 #define a_z A2 //calibrate your readings,(VDD/2), to get ± values const uint16_t a_x_cal = 518; const uint16_t a_y_cal = 530; const uint16_t a_z_cal = 556; //variables to hold axes values uint16_t acc_X = 0; uint16_t acc_Y = 0; uint16_t acc_Z = 0; //setup method void setup() { //set serial baudrate at 9600 Serial.begin(9600); } //loop method void loop() { //print accelerometer values PrintAccValues(); //delay 200 ms delay(200); } //print axes method void PrintAccValues() { //print X axis values acc_X = analogRead(a_x) - a_x_cal; Serial.print("X : "); Serial.print(acc_X); Serial.print(" "); //print Y axis values acc_Y = analogRead(a_y) - a_y_cal; Serial.print("Y : "); Serial.print(acc_Y); Serial.print(" "); //print Y axis values acc_Z = analogRead(a_z) - a_z_cal; Serial.print("Z : "); Serial.print(acc_Z); Serial.println(); } |
How the Code Works
In the macro definitions we define analog (A0, A1 and A2) pins used on the Pro Mini for the sensor’s XYZ axes.
1 2 3 |
#define a_x A0 #define a_y A1 #define a_z A2 |
The sensor readings are either above (positive g) or below (negative g) VDD/2, so we set constants for each axis for VDD/2 values. To get the constants, make sure the sensor is placed on a flat surface.
1 2 3 |
const uint16_t a_x_cal = 518; const uint16_t a_y_cal = 530; const uint16_t a_z_cal = 556; |
Finally we have the ‘PrintAccValues()’ funtions being called continously in the loop method. Its main function is to get and print XYZ axes values to the serial port. e.g for X axis, we have;
1 2 |
acc_X = analogRead(a_x) - a_x_cal; Serial.print("X : "); Serial.print(acc_X); Serial.print(" "); |
Running the Code
First select port, then select ‘Arduino Pro or Pro Mini (3.3V, 8 MHz) w/ ATmega328’ in the boards dropdown list and finally click ‘Run on Arduino’ to upload code to your Arduino pro mini.
Serial Monitor
We’ll use the serial monitor to view XYZ axes values. Set baudrate to 9600 and click ‘connect’ (Use the FTDI FT232R USB-Serial module to connect the board to the computer) to enable serial communication.