Page 1 of 1

ADXL345 3 Axis Accelerometer air mouse and joystick

Posted: Thu Dec 22, 2011 10:46 pm
by Gary Freegard
Hi
Here is my Christmas project, to create a USB joystick using an accelerometer.

The accelerometer I used is an ADXL345 (http://www.analog.com/static/imported-f ... DXL345.pdf ) bought on ebay. This device outputs its data via a serial bus, either I2C or SPI, I have used I2C.
The USB joystick settings came from http://www.matrixmultimedia.com/mmforum ... =26&t=6536

Here is a breakdown of the program:
Main, initializes USB Hid, calls macro to setup the sensor and then has an infinite loop it which it reads the accelerometer every 10ms.

setup_adxl345 which does the following:
  • Register 0x2C is used to set the data rate (200Hz)
    Register 0x31 is used to set the range and resolution (16g and full,13bit)
    Register 0x2D is used to start the measurement.
These values can be changed via the first calculation box.

read_adxl345 this does the following:
  • Reads in 6 bytes of data.
    Combine 2 bytes of data for each channel to give a signed integer.
    Use adjustReading macro to add offset and scale the channels, this uses floating point calculation.
    Use limiter macro to limit the channel output to ±120.
The reason I have created the adjustReading macro, was to compensate for any offset errors and to scale the output so that when an accelerometer is experiencing 1g the output will be 255. So the values that I have used may not be suitable for other accelerometers. The scale values were halved so that the max it ±127. But this could be changed to suit individual needs.

Gary

PS Have just modified it so it works as a mouse, so apart from changing the USB Hid properties the only other change was to reduce the x and y values by dividing them by 10, this value is used for the speed value. My next version will use a 3 axis gyro, as this should work a lot more intuitively for user.

Re: ADXL345 3 Axis Accelerometer air mouse and joystick

Posted: Mon Dec 26, 2011 1:26 pm
by Gary Freegard
Hi

This version has a macro that carries out a multi read on I2C, it requires the device address, the register address and number of bytes. It returns a string (20 bytes), so it can only read at most 20 registers in one go, hopefully that should be enough for most people.
  • Start I2C
    Write Device Address(W)
    Write Register Address
    Reset I2C
    Write Device Address(Read)
    Start Loop Count less then number of bytes
    • read I2C
      increment count
    read I2C + NAK
The returned string is copied to variable results (string[20]), from which I then extract the require data.

Gary