Only negative numbers with Memsic 2125?

Hi,

I just got an Arduino and have been running example programs/circuits. Eventually I got to a point where I wanted to try out the Memsic 2125 so I bought one from Radio Shack (didn't want to wait for shipping). So I wired up the example from arduino.cc, uploaded the code and viewed the serial console. I compared my serial console output to this video (in HD) and my results are only negative numbers in the -1100 to -2300 range about. I'm wondering if I have a bad Memsic 2125? I would really appreciate any feedback.

Thanks,
Daniel

Does the output change consistently with orientation?
If you point one of the axes straight down, it will be subject to acceleration of 1g due to gravity.
Check the output for this axis.
Now rotate the board so that the same axis is reversed, ie the side of the board that was lowermost is now uppermost.
Check the output.
Does the result look consistent?

Can you post your sketch?
(Please use the code button!)

Thanks for the reply AWOL.

First let me just get this out of the way. Even when the Memsic/Arduino is flat on the table the numbers on the serial console can fluctuate by maybe 2 to 8 degrees. Is this normal?

Now I've chosen to tilt it along the y axis, and when I do I've found that the numbers are indeed consistent.

By the way, I am using an Arduino Diecimila.

int xpin = 2;
int ypin = 3;

void setup()
{
  Serial.begin(9600);
  pinMode(xpin, INPUT);
  pinMode(ypin, INPUT);
}

void loop()
{
  int pulseX, pulseY;
  int accX, accY;

  // read pulse from x- and y-axes
  pulseX = pulseIn(xpin,HIGH);  
  pulseY = pulseIn(ypin,HIGH);

  // convert the pulse width into acceleration
  // accX and accY are in milli-g's: earth's gravity is 1000.
  accX = ((pulseX / 10) - 500) * 8;
  accY = ((pulseY / 10) - 500) * 8;

  // print the acceleration
  Serial.print(accX);
  Serial.print(" ");
  Serial.print(accY);
  Serial.println();

  delay(100);
}

the numbers on the serial console can fluctuate by maybe 2 to 8 degrees

How are you calculating that? I don't see any trig in your code.

Do you mean "counts" and not "degrees"?

If so, yes, this is probably quite normal.

I've not used the Memsic device, but I have used MEMS accelerometers quite a lot.

What happens if you use "long"s instead of "int"s for the pulse measurement?

PING: Yes, "long" should do it - 50:50 duty cycle is 50mS == 50000uS, but 50000 in a 16 bit "int" is negative.

Yes I'm sorry I believe I meant counts. Here's an example of a few seconds of the console output:

-1672 -1680

-1664 -1680

-1664 -1680

-1672 -1680

-1672 -1680

-1664 -1680

-1664 -1680

-1664 -1680

-1672 -1680

-1672 -1680

-1664 -1680

-1672 -1680

-1664 -1680

-1672 -1688

-1672 -1680

-1664 -1680

-1672 -1680

-1664 -1680

-1672 -1680

-1672 -1680

-1664 -1680

-1664 -1680

-1664 -1680

-1672 -1680

I tried changing the pulse variables from ints to longs and I get the same output.

I tried changing the pulse variables from ints to longs and I get the same output.

You need to change

int pulseX, pulseY;
int accX, accY;

to
long pulseX, pulseY;
long accX, accY;

not just the interval but the variables you are using in the calculation.

Hi Grumpy Mike, thanks for your reply.

Actually I tried both. Changing the pulse to long, and changing both. I still get the same results.

Mike beat me to it.
Though I was wrong in stating that the pulse length was 50mS - it's only 5mS at 100Hz.

Have you tried printing the raw pulse values? That'd be where I'd start.

Thanks again for your help guys.

I think it's a bad chip.

I'm printing out the raw pulses and I'm getting illogical results. All the numbers are in the 2900 range.

My understanding from the video is that if the Arduino/Memsic was flat on a table I'd get 0, and then it would increase or decrease as I tilted it.

I'm glad I kept my receipt.

Someone else posted here a while back saying that they'd got bad results with this device, but I don't think the post went anywhere.

Just out of interest, could you measure the PWM frequency by measuring both the high and the low portions?

I know that it shouldn't be anything other than 100Hz, but if it is consistent, you may be able to still get useful results by re-jigging the arithmetic until you can get back to RadioShack.

My multimeter says it's 100.2 Hz on both high and low. Does that mean that there's nothing wrong with the chip?

My understanding from the video is that if the Arduino/Memsic was flat on a table I'd get 0,

No this is defiantly wrong. Just think about it, how would it know it was pointing in the zero direction. As AWOL says start by printing out the raw pulse numbers. By the way how do you get a multimeter to measure frequency?

The numbers out of these things are not steady values but constantly change over a small range with noise.

Hey Mike

Yeah, I guess I just didn't know what to expect out of this thing. Maybe I bit off more than I can chew?

I just came back from Radio Shack with a new Memsic and I'm getting the same results (laugh out loud).

I have an Extech 330 and it can test frequencies.

I still don't get why that Make video has the Arduino outputting different results than mine.

My understanding from the video is that if the Arduino/Memsic was flat on a table I'd get 0,

Yes that's definitely right.

If it's a two axis device, AND the device is immobile on a firm, level surface, then the acceleration in both axes should be zero (or very nearly so), so the output should be around about zero (50% duty cycle).
If you've got the arithmetic right, you should see the acceleration wobble around the zero mark.
Tilting it allows a component of the acceleration due to gravity to affect one or both axes.

My multimeter says it's 100.2 Hz on both high and low

The frequency should remain constant (and 100Hz is the right ball-park), but its unlikely your multimeter measures duty cycle.
You really need to get the arduino to print the raw high and low pulse lengths.
Try this level, and then tilt the sensor. See what happens.

Ok, so I modified the code some I'm only dealing with the X axis. My thought in doing that was that it would be easier to work with only one.

Here is an example of a few seconds of the raw pulses. High is the first column and low is the second column:

2962 2951

2962 2949

2960 2949

2962 2949

2961 2951

2959 2949

2960 2949

2960 2949

2961 2950

2962 2949

2961 2947

2962 2947

2962 2949

2960 2949

2961 2952

2964 2949

2960 2950

2961 2949

2966 2950

2960 2952

2961 2949

2964 2947

This output was the sensor being level.

I've tilted it as well and it's definitely showing that in the console. So that must mean the math is wrong? The thing that's bothering is that it's just the example code from the Arduino site. Also the fact that the Make video uses the same exact code, but he's getting logical results.

I don't see how that gives 100Hz, unless "pulseIn" is returning the wrong values for some reason.
If those timings are in uSec, then the frequency should be more like 170Hz.

Can you check your meter by generating a 100Hz square wave:

while (1) {
  digitalWrite (out, HIGH);
  delay (5);
  digitalWrite (out, LOW);
  delay (5);
}

Here's a thought - what voltage are you running this off, and is the PWM frequency proportional to supply voltage?
If the supply is specced at 3v, then 5/3*100 = 166Hz.

AWOL,

I'm getting 98.89 hertz at 49.8%. I'm running this via the AC adapter, which I understand regulates down to 5 volts? I'm not sure about the PWM frequency.

OK, so your meter does measure mark:space!
Cool.
Ok, so you're saying the arduino-generated 100Hz is pretty much correct.
That suggests clocks are all correct.

This is puzzling.
Can you recheck the PWM from the accelerometer with the meter?
Maybe even copy it to the same output pin as the generated 100Hz.

digitalWrite (out, digitalRead (accelPin));

I'm running out of ideas >:(

I'm sorry, how do I check the pulse width?

Edit: Oh wait a second, the pulse width is how "wide" the frequency is right?

Still, I'm not quite sure how to go about what you're proposing.

Can you try running the accelerometer off a couple of AA batteries (common the grounds) instead of 5V?
Or maybe the 3.3V output?
Can you tell me what your meter is capable of? (make/model number)

Frequency is the inverse (1/x) of the period, and the period is measured as the time from one negative-going (or positive-going) edge until the next negative-going (or positive-going) edge, i.e. one complete cycle.

[EDIT] This is driving me crazy - the Memsic site doesn't list a 2125 device, in fact, it doesn't list any 3g PWM devices. Grrr.
Found this:
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1175526074