# Sensing the Z axis with an ADXL335

Hello Arduino community!

My team and I are currently working on a project where an ADXL335 accelerometer is placed in a (fake) gun to determine where on the screen is the gun aiming at.

However, we don't manage to get the orientation of the accelerometer in the Z axis, thus we can't detect if the user is turning the gun right or left. Is the accelerometer able to detect this kind of movements?

We used some code from builder to convert data from the accelerometer to radian angles.

Here is a video showing what we've done: https://www.youtube.com/watch?v=dkozpru4wZ8&feature=youtu.be

And here is our Arduino code:

``````//////////////////////////////////////////////////////////////////
//Simple code for the ADXL335, prints calculated orientation via serial
//////////////////////////////////////////////////////////////////

const int xPin = 0;
const int yPin = 1;
const int zPin = 2;

//The minimum and maximum values that came from
//the accelerometer while standing still
//You very well may need to change these
int minVal = 265;
int maxVal = 405;

//to hold the caculated values
double x;
double y;
double z;

void setup(){
Serial.begin(9600);
}

void loop(){

//read the analog values from the accelerometer

//convert read values to degrees -90 to 90 - Needed for atan2
double xAng = map(xRead, minVal, maxVal, -90, 90);
double yAng = map(yRead, minVal, maxVal, -90, 90);
double zAng = map(zRead, minVal, maxVal, -90, 90);

//Caculate radian angles like so: atan2(-yAng, -zAng)
//atan2 outputs the value of -π to π (radians)
//Removed: We are then converting the radians to degrees
//The Processing sketch needs value in radians, so we keep the values in radians.
x = atan2(-yAng, -zAng) + PI;
y = atan2(-xAng, -zAng) + PI;
z = atan2(-yAng, -xAng) + PI;

//Output the caculations
Serial.println("X: " + (String) x + "\t Y: " + (String) y + "\t Z: " + (String) z);

delay(100);//just here to slow down the serial output - Easier to read
}
``````

Cheers!

Do the other axes work as expected? What happens if you swap the pins around?

``````int minVal = 265;
int maxVal = 405;
``````

Don't forget Z has a constant DC offset.

Hello, thank you for the reply! What do you mean by "constant DC Offset" ?

Anyway, to answer your questions: the angles x and y eventually displayed are completely accurate. The same can't be said for the displayed z value. We tried what you said: We swap the y and z cables, so the cable linked to the y pin of the accelerometer became connected to analog pin 3 of the arduino board and the cable linked to the z pin of the accelerometer became linked to analog pin 2 of the arduino board. As a result, what was displayed as the z values by the monitor were the accurate y value, and what was displayed as the y values were the not accurate at all z values. The z values are not completely stranger to any moment we make with the accelerometer, but it changes brutally and randomly.

``````  x = atan2(-yAng, -zAng) + PI;
y = atan2(-xAng, -zAng) + PI;
z = atan2(-yAng, -xAng) + PI;
``````

If the Z axis of the accelerometer is more or less vertical, x or y are the pitch or roll angle (and the equation for one of these is actually incorrect). z doesn't tell you anything useful. The complete theory is given here.

To measure yaw (the horizontal angle) you need a magnetometer to give you a North reference.

What do you mean by “constant DC Offset” ?

If you have an accelerometer horizontal and motionless, the x and y axes will register zero (or very near zero), but the z axis will have a 10ms-2 acceleration straight down (or up, depending on the orientation of the device on the board).

Did you account for that?

AWOL:
If you have an accelerometer horizontal and motionless, the x and y axes will register zero (or very near zero), but the z axis will have a 10ms-2 acceleration straight down (or up, depending on the orientation of the device on the board).

Did you account for that?

Hmm… I’m not sure I expressed myself well in my previous posts: By measuring the Z orientation I meant the yaw of the accelerometer (thanks jremington ). Anyway, to be honest I didn’t really understand how the code I took from buildr converts data from the accelerometer to angles. All I know is that it works for x and y, and not for z.

So, jremingtonm do you say that we need a magnetometer to determine the yaw of the gun?

Yes.