Showing posts with label Accelerometer. Show all posts
Showing posts with label Accelerometer. Show all posts

Tuesday, August 27, 2013

Arduino IMU: Pitch & Roll from an ADXL345

Introduction

The ADXL345 accelerometer measures the X,Y,Z components of the device's acceleration but in order to use this information we need to manipulate the data to a more convenient format. First of all, even if it's not mandatory, I prefer to scale the raw data from the sensor in a International System measure (g). There is this simple equation that binds the two kind of measurements:


Moreover, since I want to use the IMU for the construction of a quadcopter, I will need the estimation of pitch and roll. As you can easily imagine, there's no way to determine the yaw just trough the accelerometer's data. Indeed, if you imagine an airplane laying on the flat surface of the airport with the Z-axis perpendicular to its wing-plane, there are no change in the gravity (static acceleration) if you rotate it. But you can calculate its tilt!  That's what we're gonna do.




With a little bit of math you can see how pitch and roll can be estimated with just the three x,y and z "native" accelerometer's outputs. Indeed: 


Where φ is the roll angle and θ is the pitch angle. On Arduino you can use atan2() to semplify your integrity check (denominator must not be zero of course) which eliminate the ambiguity of the angle depending on the quadrant.


Roll & Pitch estimation

In this tutorial I'll use the same configuration as the last post but the ADXL345 is mounted on a GY80 made-in-china IMU I bought to bring on my test without spend a lot. It mounts 4 different sensors, of which I'll write on my next post.




In the snippet above I've inserted just the code relative to the post's arguments, if you want the whole code leave a comment with a valid email. The serial output is shown in the image below.


Wednesday, July 17, 2013

ADXL345 + Arduino!

As many Arduino enthusiast, I bought a ADXL345 accelerometer from RS components and I set it up to communicate with my board in order to use it in a wider electronic project. I did want to share my experience. If you want to try the accelerometer yourself, you have two choises: buy the sparkfun BOB, which comes ready to be used, or buy the accelerometer and a couple of passive components from your preferred distributor and make your own Break Out Board.
I opted for the second one! ;)
Of course you'll need some hardware stuff, and the results will not be as good as the assembled one, but the satisfaction will be over 9000. For the breakout board you will need some bypass capacitors to solder as close as possible to the VCC pin of the SMD, which is a LGA. After some hours of super focused, high level, ultra professional layout design with EAGLE, and after a couple of iron stains on my jeans, the result is the following:

ADXL345 on a DIY breakout board.

As you can see the result is quite obscene if compared to the red-fashioned ultra-small sparkfun edition, but this is a DIY and I love it!  In the image above the wire needed to provide power supply and I2C communication are already connected to my Arduino UNO. The second step of course was to google for some lines of code to read the accelerometer data. Internet is full of code for Arduino and this IC. Basically the code aims to: 
  1. Initialize the I2C communication setting the Arduino as Master & the IC as Slave.
  2. Wake up the device from its low power mode (sleep mode)
  3. Read out the X-Y-Z acceleration value. 




I left out the function code for "readFrom" and "writeTo" to keep the post as compact as possible. If you need it, just leave a comment and I will send you the entire project. The image below is a snapshot from the IC's datashhet and it shows the meanings of the bits in the Power Control Register, whose addres is 0x2D. As you can see from the code above, we first reset the register writing to 0 every bit, and than we set to 1 the bit "Auto Sleep Mode" and "Measure". The latter is the one who let the device start measuring. Of course we could set the bit with a single instruction, but since this is kind a tutorial, I thought it was better to leave the three different tokens.
Next step: Calculate position from acceleration. If I reach 10 comments, I promise I'll do it in one day! ;)

ADXL345 POWER_CTL Register configuration byte