Thursday, 20 November 2008

XYZ - Time to play with the 3 Axis Accelerometer

What could possibly go wrong!

Being a bit more cautious now, for a while I thought I had cooked a couple of the outputs of the Arduino, I left the LEDs running while I was playing with other bits and pieces, tidying code etc. Ran another test and only 1 LED was working! The board had been shuffled about the desk a bit, and two of the resistors leads were crossed. I was a bit gutted, took a while before I checked my code changes and found a bug.

Components:
Arduino Decimila
LIS3LV02DQ Triple Axis Accelerometer

First of all I had to solder some connector pins to the Accelerometer breakout board.
So out came the soldering kit, another recent purchase since getting into this game, had a bit of a practice to get my eye in before attacking the breakout board, soldered an 8 pin male header to the board.

Found the following FANTASTIC article which showed me exactly what I needed to do to get this up and running, along with some sample code.

Initially the WHO_IS command was returning FF not the 0×3A expected, a bit of a wiggle soon sorted that problem out. Then added the code to send the reading from each axis to the laptop. One axis was giving sensible readings, Was a little concerned as the other two were giving me -32768, Integer overflow! From what I understood, I should have been getting (-1024... 1024).Checked the variable types they were, so next thought was I had cooked the chip when I soldered the header pins.

Double checked the wiring as described in the article, reset the accelerometer into the bread board, powered it back up and all was well, sensible readings from X,Y, and Z :) happy days.

Then cobbled some code into the Arduino to covert each axis reading into a value to represent a RGB colour. For now I decided to use absolute values and map into 0..255 range.
the readings from the accelerometer vaired quite a bit even while resting on the desk. So I decided to use an average of the previous 10 readings so the colour would not flicker. I am guessing the Smoothing function idea was a result of reading through the many great samples on the Arduino site.

To visualise, I added some code to the ColourChooser component I had written to test the LEDs. Just the code parses the data into RGB values and updates the background of the ColourChooser app.

The result, BIG GRINS when that all came together and worked :)
Am very pleased to have progressed this far so quickly. If I had not found examples online I would probably still be reading the data sheet for the ATmega168, trying to work out how to make an LED blink. Who would have thought such a tiny bit of plastic, silicon or what ever it is made of could have so much going on inside it, 376 pages is the Data sheet that explains its innards!

Its about now I am regretting not buying all the bits at once....

No comments: