wgvanveen
Posts: 5
Joined: Wed Jan 09, 2013 7:09 pm

Java + Pi4J + I2C + AXL345

Fri Oct 04, 2013 2:02 pm

Hey guys,

I have been struggeling with Pi4J for some time now, and I wonder if there is a tutorial somewhere to get the AXL345 working. I tried to translate the Adafruit library (http://www.adafruit.com/products/1231), to java. However i do get a response, but the values just don't make any sense. If i move the sensor it changes the values, but not to a good value. I think i did something wrong with the combination of the bytes and ints. I used the following code:

Code: Select all

private static final byte ADRESS = 0x53;
	
	private static final byte ADXL345_REG_POWER_CTL = 0x2D;
	
	private static final byte ADXL345_REG_DATAX0 = 0x32; //X-axis data 0
	private static final byte ADXL345_REG_DATAY0 = 0x34; //Y-axis data 0
	private static final byte ADXL345_REG_DATAZ0 = 0x36; //Z-axis data 0
	
	private static final double ADXL345_MG2G_MULTIPLIER = 0.04; // 4mg per lsb
	private static final float SENSOR_GRAVITY_STANDARD = 9.80665F;
	
	private static final int ADXL345_REG_OFSY   =  0x1F;
	
	public Accel(I2CBus bus) throws IOException{
		System.out.println("Starting Accel");
		device = bus.getDevice(ADRESS);
		device.write(ADXL345_REG_POWER_CTL, (byte)0x08);
	}
	public void readAxis() throws IOException{
		//device.write(ADXL345_REG_DATAY0); //send data to register to read
		byte[] buff = new byte[2];
		device.read(ADXL345_REG_DATAY0,buff,0,2);
		int response = (buff[0] | buff[1]<< 8);
		double y = (double)response * ADXL345_MG2G_MULTIPLIER;
		System.out.println(y);
	}
	public int asInt(byte b){
		int i = b;
		if(i<0){
			i = i +256;
		}
		return i;
	}

I hope somebody can see my mistake!

User avatar
joan
Posts: 14661
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Java + Pi4J + I2C + AXL345

Fri Oct 04, 2013 2:14 pm

Looks OK.

Could you post a sample of the raw responses? i.e. the hex value of response as you move the sensor.

wgvanveen
Posts: 5
Joined: Wed Jan 09, 2013 7:09 pm

Re: Java + Pi4J + I2C + AXL345

Sat Oct 05, 2013 9:25 am

I have put the sensor first at rest with the y-axis down. Expecting an awnser between 0G and 1G.

Code: Select all

-3.72
-3.7600000000000002
-3.7600000000000002
-3.84
-3.84
-3.84
-3.84
-3.88
-3.7600000000000002
-3.7600000000000002
-3.72
-3.56
-3.6
-3.6
-3.6
-3.52
-3.6
-3.6
-3.7600000000000002
-3.7600000000000002
-3.64
-3.64
-3.64
-3.72
-3.68
-3.68
-3.64
-3.6
-3.68
-3.68
-3.68
-3.68
-3.68
-3.6
-3.68
-3.68
-3.68
-3.68
-3.72
-3.68
-3.68
-3.84
-3.7600000000000002
Now i put the sensor with the y-axis up, hence expecting a result between -1G and 0G.

Code: Select all

-3.56
-3.56
-3.6
-3.6
-3.6
-3.56
-3.6
-3.56
-3.56
-3.72
-3.64
-3.56
-3.6
-3.6
-3.68
-3.6
-3.56
-3.6
-3.6
-3.6
-3.6
-3.64
-3.64
-3.64
-3.64
-3.56
-3.56
-3.6
-3.68
-3.6
-3.6
-3.6
-3.6
-3.56
-3.6
-3.6
-3.6

wgvanveen
Posts: 5
Joined: Wed Jan 09, 2013 7:09 pm

Re: Java + Pi4J + I2C + AXL345

Sat Oct 05, 2013 9:36 am

Oke, just after i posted the previous results i managed to fix the problem. I made an error in the constant ADXL345_MG2G_MULTIPLIER, i forgot an extra zero. Further both bytes should be converted to (int) however the first should be corrected if the byte is negative. For this i used asInt(byte b). The whole code is written below:

Code: Select all

package sensor;

import java.io.IOException;
import java.nio.ByteBuffer;

import com.pi4j.io.i2c.I2CBus;
import com.pi4j.io.i2c.I2CDevice;

public class Accel {

	/**
	 * Trying to implement the I2C connection 
	 * implemented on ADXL345 Adafruit code
	 */
	
	
	private I2CDevice device;
	
	

	/*
	 * Adress uit Arduino code
	 * 
	 * 
	 */
	/*
	 *  REGISTERS
    -----------------------------------------------------------------------
    #define ADXL345_REG_DEVID               (0x00)    // Device ID
    #define ADXL345_REG_THRESH_TAP          (0x1D)    // Tap threshold
    #define ADXL345_REG_OFSX                (0x1E)    // X-axis offset
    #define ADXL345_REG_OFSY                (0x1F)    // Y-axis offset
    #define ADXL345_REG_OFSZ                (0x20)    // Z-axis offset
    #define ADXL345_REG_DUR                 (0x21)    // Tap duration
    #define ADXL345_REG_LATENT              (0x22)    // Tap latency
    #define ADXL345_REG_WINDOW              (0x23)    // Tap window
    #define ADXL345_REG_THRESH_ACT          (0x24)    // Activity threshold
    #define ADXL345_REG_THRESH_INACT        (0x25)    // Inactivity threshold
    #define ADXL345_REG_TIME_INACT          (0x26)    // Inactivity time
    #define ADXL345_REG_ACT_INACT_CTL       (0x27)    // Axis enable control for activity and inactivity detection
    #define ADXL345_REG_THRESH_FF           (0x28)    // Free-fall threshold
    #define ADXL345_REG_TIME_FF             (0x29)    // Free-fall time
    #define ADXL345_REG_TAP_AXES            (0x2A)    // Axis control for single/double tap
    #define ADXL345_REG_ACT_TAP_STATUS      (0x2B)    // Source for single/double tap
    #define ADXL345_REG_BW_RATE             (0x2C)    // Data rate and power mode control
    #define ADXL345_REG_POWER_CTL           (0x2D)    // Power-saving features control
    #define ADXL345_REG_INT_ENABLE          (0x2E)    // Interrupt enable control
    #define ADXL345_REG_INT_MAP             (0x2F)    // Interrupt mapping control
    #define ADXL345_REG_INT_SOURCE          (0x30)    // Source of interrupts
    #define ADXL345_REG_DATA_FORMAT         (0x31)    // Data format control
    #define ADXL345_REG_DATAX0              (0x32)    // X-axis data 0
    #define ADXL345_REG_DATAX1              (0x33)    // X-axis data 1
    #define ADXL345_REG_DATAY0              (0x34)    // Y-axis data 0
    #define ADXL345_REG_DATAY1              (0x35)    // Y-axis data 1
    #define ADXL345_REG_DATAZ0              (0x36)    // Z-axis data 0
    #define ADXL345_REG_DATAZ1              (0x37)    // Z-axis data 1
    #define ADXL345_REG_FIFO_CTL            (0x38)    // FIFO control
    #define ADXL345_REG_FIFO_STATUS         (0x39)    // FIFO status
	 */
	
	private static final byte ADRESS = 0x53;
	
	private static final byte ADXL345_REG_POWER_CTL = 0x2D;
	
	private static final byte ADXL345_REG_DATAX0 = 0x32; //X-axis data 0
	private static final byte ADXL345_REG_DATAY0 = 0x34; //Y-axis data 0
	private static final byte ADXL345_REG_DATAZ0 = 0x36; //Z-axis data 0
	
	private static final double ADXL345_MG2G_MULTIPLIER = 0.004; // 4mg per lsb
	private static final float SENSOR_GRAVITY_STANDARD = 9.80665F;
	
	private static final int ADXL345_REG_OFSY   =  0x1F;
	
	public Accel(I2CBus bus) throws IOException{
		System.out.println("Starting Accel");
		device = bus.getDevice(ADRESS);
		device.write(ADXL345_REG_POWER_CTL, (byte)0x08);
	}
	public void readAxis() throws IOException{
		//device.write(ADXL345_REG_DATAY0); //send data to register to read
		byte[] buff = new byte[2];
		device.read(ADXL345_REG_DATAY0,buff,0,2);
		int response = (asInt(buff[0]) | ((int)buff[1])<< 8);
		double y = (double)response * ADXL345_MG2G_MULTIPLIER * SENSOR_GRAVITY_STANDARD;
		System.out.println(y);
	}
	public int asInt(byte b){
		int i = b;
		if(i<0){
			i = i +256;
		}
		return i;
	}
	
}

I used the GY-80 as sensor, which i know a lot of people use for their home projects. Is there a place where i can submit my code so others can use it as well?

Thank you for the response!

User avatar
savageautomate
Posts: 225
Joined: Thu Aug 16, 2012 3:20 pm
Location: USA
Contact: Website

Re: Java + Pi4J + I2C + AXL345

Mon Oct 07, 2013 2:24 pm

Maybe it could be made into a component implementation in the Pi4J components/devices library.
https://github.com/Pi4J/pi4j/tree/devel ... /component

If so, please do a pull request with the changes, and we can pull them into Pi4J. (Make sure to use the "develop" branch.)

Thanks, Robert
Robert Savage | Follow me @savageautomate
http://www.pi4j.com | http://www.pislices.com
http://www.savagehomeautomation.com

User avatar
clicky
Posts: 424
Joined: Thu Oct 25, 2012 7:34 am

Re: Java + Pi4J + I2C + AXL345

Mon Oct 14, 2013 12:01 pm

It is already here:

https://github.com/Pi4J/pi4j/blob/devel ... L345.java

It might not be at the final place but it can be good starting point.

Return to “Java”