ADXL 335 Accelerometer for Leveling?

Hello all,

I'm new to the forum and Arduino and general but I'm very interested and excited in a project I'm trying to complete using an Arduino Uno.

I am trying to build a circuit to automatically level my camper using the electric jacks by running them up or down based on the reading from an accelerometer. I've recently connected my ADXL 335 to my Arduino and monitoring the output thru the serial monitor. I've noticed that you have to move the board more than I expected to get a change in readings.

My question is if the ADXL 335 is sensitive enough to use as a level sensor ir there is another Analog Accelerometer out there more suited for the task. I've played around with bubble level apps on smartphones and they seem to be very sensitive. So I'm pretty sure an accelerometer could do the job.

Thanks in advance.

How level does a camper need to be? Should give you a degree or 2.

Within a few degrees would definitely be acceptable. I guess I was just concerned by not seeing much change tilting the sensor in my hand. Maybe I should temporarily clamp it to my camper frame and see how much change I get at different degrees from level.

Seems like I've seen some sample code around using that accelerometer as an angle finder. Maybe I'll play around with that and see what kind of results I get.

Thanks. :)

I planned to do exactly the same and plugged in an ADXL335 to try. It was a while back but I think I found the same as you.

I still haven't got back to it and can't remember the numbers now but IIRC I decided that the output either needs to be amplified or you heed a higher res ADC.

There are digital output versions now (ADXL345) that would be a better choice these days I think.


It works fine for me.

I am using Adafruit's ADXL335 breakout board for my level project. I am getting 1/2 degree accuracy. It will work fine.

@JD3: Sound interesting, do you use any kind of filtering in your software or amplifiying on the hardware site?


like Darkside40 I would like to hear from you to see if you used any filtering and what type you used. Can you elaborate how you got 0.5° accuracy for the unwashed massses like myself. Thanks wade

I use adafruits ADXL335 board. I have a resistor and capacitor filtering the 3v reference going into the AREF pin, that's it. 3 axis directly to the analog pins. I use a shielded cable to connect the accelerometer to my duemilanove. I think it was from an extra serial cable I had laying around.

No software filtering, very basic.

You need to use 3v reference to get 1/2 degree resolution. If you use the 5v reference it works out to be around 7/8 degree.

I have my board mounted flat. X is left right, Y is forward back, Z is up down. If you have the board mounted different then the LR & FB formulas need to be changed.

I used the AN-1057 application note from analog devices to figure out the dual axis calculations.

float LR = 0.00; // left-right angle float FB = 0.00; // front-back angle int Xaxis = 0; // fresh data from accelerometer int Yaxis = 0; int Zaxis = 0; int Xcal = 506; // calibration data, zero g int Ycal = 506; int Zcal = 518;

void setup() { analogReference(EXTERNAL); // uses 3.3v from accelerometer for reference voltage }

void loop() { Xaxis = analogRead(2); // read analog inputs Yaxis = analogRead(1); // average & filter readings Zaxis = analogRead(0); LR = (atan(((Xaxis-Xcal)/Xres) / ((Zaxis-Zcal)/Zres))) *57.2957; FB = (atan(((Yaxis-Ycal)/Yres) / ((Zaxis-Zcal)/Zres))) *57.2957;

and then print to serial monitor or LCD screen...

How have you verified 0.5° accuracy? Have you calibrated the unit?



wwbrown: How have you verified 0.5° accuracy? Have you calibrated the unit?

I never said 1/2 degree accuracy, I said 1/2 degree resolution. I know this because I can move the accelerometer around and I get 1/2 degree movements. Also I have a granite surface plate with angle blocks. When zeroed on the granite all the angle blocks measure right.

The calibration is in the software. Look at the X,Y, & Z cal values. Those are set to 0 g. When you mount the accelerometer, first write a sketch that outputs only the value of analogRead in all 3 axis. If the object that the accelerometer is mounted to is level then the 3 numbers you get would be the calibration data.

The calibration data should also taken after the sensor is mounted on the board or in the system.

Yup - that's what I wrote before.

"If the object that the accelerometer is mounted to is level then the 3 numbers you get would be the calibration data."

Please use code tags.

Edit the post, select the code, and click the # button on the toolbar.


Can you please tell me how can i measure z axis up,down rotation. Using ADXL335 I could measure pitch and roll angle. But i dont no how to measure yaw angle. If i place a sensor in flat surface and rotate the sensor in yaw angle rotation , sensor does not gives me any change in value.

my robot car instead of driving in straight line, it has angle deviation from straight line while travelling. so i wanted to measure the angle deviation. please any one help me in calculating yaw angle.

Hi Sana, you are looking for a Magnetometer (Compass) Board. Adafruit and Sparkfun offer them.

The accelerometer is using gravity to determine the angles, but you need to use a different force to calculate yaw.

The Magnetometer senses magnetic forces, and hopefully it will sense magnetic north and not anything else. Especially the magnetic fields of the motors. You could set a direction and have the robot go to that heading.

If you are looking just to find out what the angle of deviation is, just send the robot out a known distance along a straight line. mark a starting spot, send it off, mark the finish spot. Then measure how far it's off from the line. Using geometry you can calculate the angle. inverse tangent (tan-1) multiplied by (length the robot traveled along straight line / distance robot veered ). Both measurements must be the same units, ex. inches, feet, etc. (6 inches is .5 feet)

That will give you the angle.....

Thanks for the replies. It's been awhile but I still plan to complete this project by the end of the summer at least. It seems I get easily distracted by other projects. There's so much to create and so little time. LOL. I sometimes drop a project to chase other dreams for several months or sometimes even years, but I usually always some back at some point and work on it until completion.

I'm going to play around with some of the code suggestions in this thread and see if I can get the resolution I need to complete this project. Sounds like it has potential. I'll visit back and update the thread once I have some results. Thanks again. :)