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.
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!)
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);
}
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 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.
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:
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.
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.
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.