Posts about... All (17) Android (2) Hooking Up Parts (10) Game Controllers (3) Robots (2)

How to Hook Up > Accelerometers (Memsic 2125)

Every one knows accelerometers from Wii controllers and smart phones. Being fairly inexpensive, they’re a prime candidate for including in a DIY project. I’ll detail how to hook up a Memsic 2125 (most common) dual-axis accelerometer and measure its tilt in this post.

Parts

Steps

  1. The Memsic 2125 accelerometer has 6 leads. Place it into your breadboard, over the gap to prevent a short. I will referring to the leads assuming that the accelerometer is rotated so that you can read “Mx2125″ normally on the mini pcb. Make sure you don’t have it upside-down.
  2. The UPPER-RIGHT lead is positive and should be wired (red) to 5V on the Arduino.
  3. The LOWER-LEFT lead is negative and should be wired (black) to Ground on the Arduino.
  4. The MIDDLE pin on the RIGHT will provide the X values and should be wired (yellow) to a Digital Pin (8) on the Arduino.
  5. The MIDDLE pin on the LEFT will provide the Y values and should be wired (orange) to a Digital Pin (9) on the Arduino.
  6. accelerometer

  7. Connect the Arduino to your computer with the USB cable (A to B).
  8. The code below simply reads in the X and Y of the accelerometer and outputs those values to the Serial Monitor within the Arduino software. Once uploaded to your board, open the Serial Monitor. You should be able to tilt the accelerometer and see the respective X and Y values change.

Code

const int xPin = 8; // X input from accelerometer
const int yPin = 9; // Y input accelerometer

int xPulse, yPulse; // read from accelerometer
int xVal, yVal; // converted value

void setup() {
Serial.begin(9600);
pinMode(xPin, INPUT);
pinMode(yPin, INPUT);
}

void loop() {

// read x & y values
xPulse = pulseIn(xPin,HIGH);
yPulse = pulseIn(yPin,HIGH);

xVal = ((xPulse / 10) - 500) * 8;
yVal = ((yPulse / 10) - 500) * 8;

Serial.print(xVal);
Serial.print("\t");
Serial.print(yVal);
Serial.println()

 

delay(100);
}

Conclusion

Now that the values are being read in properly, it becomes easy to integrate for your own purpose by using basic if-else statements and comparison operators.