Computers, Development Boards & Shields
Educational & Robotics Kits
Electronic Sensors
Motors & Controllers
Communication & Display
Electronic Components
Soldering & Lab Tools

0
Accelerometer output inaccurate – are these pinouts correct?

Hi,

Starting with your guide, “Converting Analog Readings to G Forces” I modified it slightly and load it on the Arduino.

However, the output readings did not seem to be correct. I say this because the readings seem to repeat duplicate numbers regardless of how I move the accelerometer. Following is your code as I’ve modified it, and then an abbreviated example of the output get.

 
Pins are wire as:

X = A0
Y = A1
Z = A2
SL= 3
OG = ~5
5V = 5V
3V = —
GND = GND
GS = ~4
ST = —

When I switched to the following pinouts, the output is more realistic.

X = A0
Y = A1
Z = A2
SL= 4
OG = ~5
5V = 5V
3V = —
GND = GND
GS = ~6
ST = —

This second pinout seems better, is it correct?

Thanks,
Ben

//***Code****
const static boolean GFORCE = true;
const static boolean METERS = false;

byte _sleepPin= 4;
byte _GS= 5;
byte _xpin= A0;
byte _ypin = A1;
byte _zpin = A2;

int _gc = 92;//gravity constant (inverted for proper sinage)

int _0z = 660; //defaults to read values (this is how you would hardcode a
calibration)
int _0x = 660; //defaults to read values
int _0y = 677; //defaults to reasonable values
boolean _calibrated=false;

void setup()
{
Serial.begin(9600);
begin();//runs setup
calibrate();
}

void loop()
{
delay(1000); //Delay for readability
Serial.println(“In G-FORCES:”);
readGx();
readGy();
readGz();
Serial.println();
Serial.println(“In Meters Per Second-Squared:”);
readMx();
readMy();
readMz();
}

void begin()
{
pinMode(_sleepPin, OUTPUT);//output mode
pinMode(_GS, OUTPUT);//output mode
pinMode(_xpin, INPUT);//input mode
pinMode(_ypin, INPUT);//input mode
pinMode(_zpin, INPUT);//input mode

digitalWrite(_GS, LOW);//sets GS mode
digitalWrite(_sleepPin, HIGH);//turns off sleep mode and activates device
digitalWrite(_xpin, HIGH);//turn on pull up resistor
digitalWrite(_ypin, HIGH);//turn on pull up resistor
digitalWrite(_zpin, HIGH);//turn on pull up resistor
}

void calibrate()
{
Serial.println(“Please place device right-side up on a flat surface.”);
delay(2000);//give user two seconds to comply.
Serial.println(“Calibrating”);

_0z = readAverage(_zpin, 10) – _gc;
Serial.print(“Z Axis Calibrated:”); Serial.println(_0z);
_0x = readAverage(_xpin, 10);
Serial.print(“X Axis Calibrated:”); Serial.println(_0x);
_0y = readAverage(_ypin, 10);
Serial.print(“Y Axis Calibrated:”); Serial.println(_0y);

_calibrated = true;
}
int readAverage(int pin, int samples)
{
long readings = 0;

for(int i = 0; i < samples; i++)
{
readings += analogRead(pin);
}

return (readings/samples);
}

float convert(float myReading, float my0G)
{
return convert(myReading, my0G, GFORCE);//defaults to G-Forces
}

float convert(float myReading, float my0G, bool myUnits)
{
float myResult=((my0G-myReading)/(_gc));
if(myUnits)
return myResult; //G-Forces
else
return (myResult * 9.80665);
//converts to m/s^2 using conventional value for gravity.
//if you are on a different planet you may need to use a
//value for your conversions.
}

void readGx()
{
Serial.print(“X Reading: “);
Serial.println(convert(analogRead(_xpin), _0x), DEC);
}

void readGy()
{
Serial.print(“Y Reading: “);
Serial.println(convert(analogRead(_ypin), _0y), DEC);
}

void readGz()
{
Serial.print(“Z Reading: “);
Serial.println(convert(analogRead(_zpin), _0z), DEC);
}

void readMx()
{
Serial.print(“X Reading: “);
Serial.println(convert(analogRead(_xpin), _0x, METERS), DEC);
}

void readMy()
{
Serial.print(“Y Reading: “);
Serial.println(convert(analogRead(_ypin), _0y, METERS), DEC);
}

void readMz()
{
Serial.print(“Z Reading: “);
Serial.println(convert(analogRead(_zpin), _0z, METERS), DEC);
Serial.println(” “);
Serial.println(“New Reading”);
}

//****End Code****

Output

Please place device right-side up on a flat surface.
Calibrating
Z Axis Calibrated:700
X Axis Calibrated:792
Y Axis Calibrated:792

In G-FORCES:
X Reading: 0.0000000000
Y Reading: 0.0000000000
Z Reading: -1.0000000000

In Meters Per Second-Squared:
X Reading: 0.0000000000
Y Reading: 0.0000000000
Z Reading: -9.8066501617

New Reading
In G-FORCES:
X Reading: 0.0000000000
Y Reading: 0.0000000000
Z Reading: -1.0000000000

In Meters Per Second-Squared:
X Reading: 0.0000000000
Y Reading: -0.1065940260
Z Reading: -9.8066501617

New Reading
In G-FORCES:
X Reading: 0.0000000000
Y Reading: -0.0108695650
Z Reading: -1.0000000000

1 Answer

0
accepted
Ben,
After reviewing your setup I noticed that you had the GS placed on a non coded pin, and before you had the sleep on the wrong pin as well. The reason your output looks better is it is no longer sleeping, but to have control of your mode you want to tie the GS pin the pin 5, and the Sleep to pin 4 as per the code. By the way 0G doesn’t need to be connected at all.
Let me know if that fixes things for you.
Respectfully,
Joseph Dattilo

Your Answer

No comments yet.

Leave a Reply