# Memsic 2125 Accelerometer

Learn how to read data from the Memsic 2125 Two-axis accelerometer.

The Memsic 2125 (datasheet) is a two-axis accelerometer capable of measuring acceleration up to plus or minus 2g. It has a simple digital interface: two pins (one for each axis) emit pulses whose duration corresponds to the acceleration of that axis. By measuring the length of that pulse, in microseconds, using the pulseIn() function, it is possible to determine the rate of acceleration and to use that data for your purposes.

### Hardware Required

• Arduino Board

• Memsic 2125 Accelerometer

• hook-up wires

### Circuit

Use the small triangle on the Memsic to properly orient the sensor on your breadboard. Connect the 5V and GND pins of the Memsic 2125 to the power and ground ports on the board. Connect digital pin 2 of the board to the X out pin of the accelerometer, and digital pin 3 to the Y out pin.

Your Arduino must be connected to your computer in order for it to transmit serial data.

### Code

Open the Serial Monitor of the Arduino Software (IDE) to see the values read from the accelerometer position.

``````1/*2
3  Memsic21254
5  Read the Memsic 2125 two-axis accelerometer. Converts the pulses output by the6
7  2125 into milli-g's (1/1000 of Earth's gravity) and prints them over the8
9  serial connection to the computer.10
11  The circuit:12
13    - X output of accelerometer to digital pin 214
15    - Y output of accelerometer to digital pin 316
17    - +V of accelerometer to +5V18
19    - GND of accelerometer to ground20
21  created 6 Nov 200822
23  by David A. Mellis24
25  modified 30 Aug 201126
27  by Tom Igoe28
29  This example code is in the public domain.30
31  https://www.arduino.cc/en/Tutorial/Memsic212532
33*/34
35// these constants won't change:36
37const int xPin = 2;     // X output of the accelerometer38
39const int yPin = 3;     // Y output of the accelerometer40
41void setup() {42
43  // initialize serial communications:44
45  Serial.begin(9600);46
47  // initialize the pins connected to the accelerometer as inputs:48
49  pinMode(xPin, INPUT);50
51  pinMode(yPin, INPUT);52}53
54void loop() {55
56  // variables to read the pulse widths:57
58  int pulseX, pulseY;59
60  // variables to contain the resulting accelerations61
62  int accelerationX, accelerationY;63
64  // read pulse from x- and y-axes:65
66  pulseX = pulseIn(xPin, HIGH);67
68  pulseY = pulseIn(yPin, HIGH);69
70  // convert the pulse width into acceleration71
72  // accelerationX and accelerationY are in milli-g's:73
74  // Earth's gravity is 1000 milli-g's, or 1 g.75
76  accelerationX = ((pulseX / 10) - 500) * 8;77
78  accelerationY = ((pulseY / 10) - 500) * 8;79
80  // print the acceleration81
82  Serial.print(accelerationX);83
84  // print a tab character:85
86  Serial.print("\t");87
88  Serial.print(accelerationY);89
90  Serial.println();91
92  delay(100);93}``````

You can find more basic tutorials in the built-in examples section.

You can also explore the language reference, a detailed collection of the Arduino programming language.

Last revision 2015/07/29 by SM

#### Suggest changes

The content on docs.arduino.cc is facilitated through a public GitHub repository. If you see anything wrong, you can edit this page here.