Adxj345

kfjl

Member
AdxL345

I'm probably staring at something I can't see.
Here's my program and its debug output.
When I move the ADXL345 the only value that changes is b3 (from 163 to 164). I was expecting all the values to change.
I've tried 1k, 4.7k, 10k and no pull-up resistances with the same result
.I'm running the picaxe and ADXL345 off two LR14 batteries (3V).
Any idea what I'm missing?


Code:
#PICAXE08M2
#no_data

HI2CSETUP I2CMASTER, 0xA6, i2cslow, i2cbyte
pause 500
HI2COUT 0x31,(0x01) 'Put the ADXL345 into +/- 4G range
pause 500
HI2COUT 0x2D,(0x08) 'Put the ADXL345 into Measurement Mode 
pause 500

main:

HI2CIN 0x32,(b1,b2,b3,b4,b5,b6) 

debug

goto main


;The SDO pin is connected to ground, CS pin is connected to VCC

;SCL is connected to pin C.1, SDA is connected to pin c.2, both are pulled high by 4.7k resistors
Sélection_002.png
 
Last edited:

inglewoodpete

Senior Member
Firstly, I have only used the ADXL335 - not the ADXL345. Your code looks OK to me (but I have no experience with the i2c version). The data showing in the Debug screen looks reasonable too - the PICAXE seems to be communicating with the ADXL345 correctly.

My only question is: when you move the accelerometer chip, are you just moving it or are you turning it over (Eg 90 degrees). From my experience with the ADXL335, the value you read will normally halve or double as you tilt the chip by 90 degrees. This is because of the effect of gravity on the accelerometer element.
 

PieM

Senior Member
I'm probably staring at something I can't see.
ADXL345 data sheet:

There are no internal pull-up or pull-down resistors for any
unused pins; therefore, there is no known state or default state
for the CS or ALT ADDRESS pin if left floating or unconnected.
It is required that the CS pin be connected to V DD I/O and that
the ALT ADDRESS pin be connected to either V DD I/O or GND
when using I 2 C.

and with HI2CIN 0x32,(b2,b3,b4,b5,b6,b7)

Xaxis = w1
Yaxis = w2
Zaxis = w3
 
Last edited:

inglewoodpete

Senior Member
ADXL345 data sheet:

There are no internal pull-up or pull-down resistors for any
unused pins; therefore, there is no known state or default state
for the CS or ALT ADDRESS pin if left floating or unconnected.
It is required that the CS pin be connected to V DD I/O and that
the ALT ADDRESS pin be connected to either V DD I/O or GND
when using I 2 C.
I think kfjl has those points covered:

Code:
;The SDO pin is connected to ground, CS pin is connected to VCC

;SCL is connected to pin C.1, SDA is connected to pin c.2, both are pulled high by 4.7k resistors
 

kfjl

Member
Still no luck.

This is what's in the registers of the ADXL345:

229 is the device ID at register 0x00.

0x01 to 0x1C are reserved so the rest of the first line starts at 0x1D.

The second line starts at 0x26.

The "10" is what's in the BW_RATE register. Default data rate of 100Hz.

The "8" is measure mode.

The third line starts at 0x30.

131 is what's in the INT_SOURCE register.The Overrun, Watermark and Data_Ready bits are set so something's going on because reading from the XYZ registers clears these bits.

3 is the content of the DATA_FORMAT register. Range=+-16g.

The next six registers are DATAX0 to DATAZ1.

The last two zeros concern the FIFO which I don't use.

I'll have to do some more reading.

adxl345_REGS.jpg
 

eggdweather

Senior Member
If you monitor the output at rest the readings should be static in x, y and z, if you then tap or move the module quickly, in one plane say the x plane, you should see the value increase then stabilise returning to 0 unless you can keep it accelerating, which is unlikely. I find a tap on the desk is a good test and you should see all axis respond to that, then of course return to a steady state. Did you think it would give a compass like reading?
 

inglewoodpete

Senior Member
Still no luck.

The next six registers are DATAX0 to DATAZ1.

The last two zeros concern the FIFO which I don't use.

I'll have to do some more reading.
I think it's working fine. You have 3 readings: 123 255; 233 255 and 232 255. Each value's bytes are reversed, which you can correct in the hi2cIn command. Anyway, expressed in hexadecimal you have values $FF7B; $FFE9 and $FFE8 or -133, -23 and -24.

Due to the last two values almost being equal makes me think that it's working. The values you are reading are the earth's gravity. With the logging running, turn the chip over by 90, 180 then 270 degrees. I think you'll find that the values change in a predictable way.
 

kfjl

Member
I've made some "progress".

Up until my latest try, only one axe reading moved and then it just flickered between several values with a difference of a few units. Whatever I did. (turned it upside-down and every which way, tapped it on the desk)

With 0x0F in the DATA_FORMAT register I've finally got all three axis readings changing, but now they never settle down (they change from 0 to anything up to 65536), even with the chip held flat on the desk.

If I go back to 0x01 in the DATA_FORMAT register the only value that changes (independetly of how I move the ADXL chip) is the Z-axis reading, by a few units (out of 65536, so just noise). The X and Y readings stay blocked at 511, the Z reading flickers between 65527 and 65532. Whatever I do with the ADXL chip.

Maybe it's something to do with data clipping or overrun? Or the choice of pull-up resistors? Or I need decoupling capacitors?

Anyway, thank you all for your replies.
 

hippy

Technical Support
Staff member
Part of the problem may be in using DEBUG; that can be a little slow to update the display and the numbers are not in a convenient format. Using SERTXD and the Terminal display may give you a more readily understandable means of seeing what is happening ...

Code:
#Picaxe 08M2
#Terminal 4800

HI2cSetup I2CMASTER, 0xA6, i2cslow, i2cbyte
Pause 500
HI2cOut 0x31,(0x01) 'Put the ADXL345 into +/- 4G range
Pause 500
HI2cOut 0x2D,(0x08) 'Put the ADXL345 into Measurement Mode 
Pause 500

Do
  HI2cIn 0x32,(b2,b3,b4,b5,b6,b7)
  If w1 < $8000 Then
    SerTxd("X = +", #w1, 9 )
  Else
    w1 = -w1
    SerTxd("X = -", #w1, 9 )
  End If
  If w2 < $8000 Then
    SerTxd("Y = +", #w2, 9 )
  Else
    w2 = -w2
    SerTxd("Y = -", #w2, 9 )
  End If
  If w3 < $8000 Then
    SerTxd("Z = +", #w3, CR, LF )
  Else
    w1 = -w1
    SerTxd("Z = -", #w3, CR, LF )
  End If
Loop
 

kfjl

Member
This is what I get using hippy's code above (image left). I added a pause in the loop to have time to change the chip's position between two readings. As you can see, nothing changes. The chip was held composants down, composants up and perpendicuar to the desk on each edge. The -6 on the last line is when I switched off the power.


With 0x0F in the DATA_FORMAT register (FULL_RES, left-justify,16g range) and just printing the X values, I get the (image right) output WITHOUT MOVING THE CHIP.


My raspberry pi couldn't get any sense out of it either.

I must have a dud chip.

Thanks again for your replies.
 

Attachments

eggdweather

Senior Member
The chip is not a compass, it will only give a change of reading when there is some movement/acceleration. If you move it up and down in the X plane, you should see the values vary, does that happen?
 

inglewoodpete

Senior Member
This is what I get using hippy's code above (image left). I added a pause in the loop to have time to change the chip's position between two readings. As you can see, nothing changes. The chip was held composants down, composants up and perpendicuar to the desk on each edge. The -6 on the last line is when I switched off the power.


With 0x0F in the DATA_FORMAT register (FULL_RES, left-justify,16g range) and just printing the X values, I get the (image right) output WITHOUT MOVING THE CHIP.


My raspberry pi couldn't get any sense out of it either.

I must have a dud chip.

Thanks again for your replies.
Firstly, that "Z = " data column is out-of-range. There is an error in hippy's code (a rare event!) - his 3rd last line should read "w3 = -w3", not "w1 = -w1"

With the exception of the 16G range, the range of values should be -512 to 511. The 13-bit resolution (or representation) of the 16G range would be -4096 to 4095, so the numbers you are getting in the right-hand screen do not make sense.
 

kfjl

Member
Hi eggdweather:

I know it's not a compass. I expect to get a positive reading (the Earth's gravity) with the chip held face down, and the same value, but negative, with the chip held face up, or vice-versa. I expect the face-up value divided by 1.14 if I hold it at 45°. I also expected it to be a lot easier to get working.....


@inglewoodpete:

The joys of cut-and-paste. I didn't see that error. I'm not seeing much at all at the moment. A bit like scuba-diving in a cesspool. I've put it right and below is the program's output. I agree that the right-hand screen doesn't make sense. If I'm right in what I expect to get, the left-hand screen doesn't make sense either.

With Hippy's program the X and Y values never change, even when I tap the chip on the desk or wizz it about on the end of its wires. The Z value changes all the time, even when I hold the chip still.

In my next experiment I might try treading on it.
Hippy2.jpg
 

hippy

Technical Support
Staff member
Firstly, that "Z = " data column is out-of-range. There is an error in hippy's code (a rare event!) - his 3rd last line should read "w3 = -w3", not "w1 = -w1"
Oops. Sorry about that!

And I can't really help here as I am not familiar with the device, how to configure it, or what to expect.

When I used an analogue accelerometer; bashing that all over the place at least showed changing results in all axis which confirmed things were happening, and what I got correlated with what was expected.

Not getting any meaningful results or only getting static ones would seem to suggest the right data isn't being read, the attenuation of results is too high, or it's simply not set to update results.
 

hippy

Technical Support
Staff member
Reading the ADXL435 datasheet and it all looked very familiar. I am honestly not sure if I have ever used it myself or just recall helping with numerous other PICAXE users who have used the ADXL435.

Trawling through forum posts it should be working, and should be easy enough to get working. Most issues seem to have been in getting the I2C Device Address wrong or missing pull-ups but there were some cases of people getting odd results depending on power supplies.

As everything else looks right, it is returning data, just that the data is not as expected, it might be worth providing details on exactly what hardware you have, post photographs of that, and I would definitely say make sure you are using fresh batteries, perhaps run it all off a bench PSU at 3V.

Of course, it's always possible you have a faulty or damaged component. Where was the ADXL435 sourced from, and do you have another you could try?
 

kfjl

Member
With a raspberry pi as 3.3V power source and using 2k pull-ups (ULM10204 I2C bus specification manual, page 55) I get the same result. I have two adxl345 chips. One gives the following values:

x=+511
y=+511
z=-9 to -17

The other one gives:

x=-512
y=-93 to -94
z=-94 to -95

Neither of them responds to changes in its position.

I bought the second one when I couldn't get the first to work, from a different supplier but identical chip. Both from China.
Photos of the circuit below.

adxl_chip.jpg

breadboard.jpg
 

hippy

Technical Support
Staff member
The wiring all looks okay. SDO to 0V and /CS to V+ matches the datasheet for I2C operation. The PICAXE is using the correct pins for I2C. The pull-ups are less than the 4K7 commonly used but should be okay for 3V. Getting results would seem to suggest the actual I2C side of things is working.

The only thing I can think of is putting in a PAUSE 250 before the HI2CIN. Maybe it's reading the data too fast to allow the chip to actually update the data ? Can't imagine that being the case though.

Those 511's / -512 are 'all bits set' values, so perhaps that's showing the readings limiting at some 9-bit max ? Perhaps they need some calibration before being used, or some calibration or configuration has been done which is now messing with the results of chip operation.

Added: Do you have details of the GY-291 module, or at least a photo of the component side ?

It appears such modules commonly have a voltage regulator included, operate from 4V to 6V, so it could come down to a voltage issue if using 3V.

There may also be pull-ups fitted on the board for the I2C bus.
 

eggdweather

Senior Member
I think Hippy has determined what the problem is, extract from GY-291 board specification:

Module supply voltage range: 4.0 V to 6 V
I/O voltage range: 1.7 V to 3.6V

The board has a 3.3v regulator and depending if it has input pull-up resistors or not will determine if it can operate from 5v inputs.

Try running the PICAXE at ~3.5volts.
 

kfjl

Member
Back again.

The doc says the GY291 can take from 4V to 6V so there was no need to mess with 3V. I don't know what all the components are but I'd bet the pull-ups are included on the board. Maybe I fried mine from the outset with the external pull-ups.

I tried what I suppose to be "data-clipping" to get the information down to 10 bits, but as you can see from the terminal output, the result makes no sense. I might have fried the Picaxe as well.

GY291.jpg

Code:
#Picaxe 08M2
#Terminal 4800

HI2cSetup I2CMASTER, 0xA6, i2cfast, i2cbyte
Pause 500
HI2cOut 0x31,(0x01) 'Put the ADXL345 into +/- 4G range 
Pause 500
HI2cOut 0x2D,(0x08) 'Put the ADXL345 into Measurement Mode 
Pause 500

Do
	
  HI2cIn 0x32,(b2,b3,b4,b5,b6,b7)
  b3=b3 and 3
  b5=b5 and 3
  b7=b7 and 3

  If w1 < 512 Then
    SerTxd("X = +", #w1, 9 )
  Else
    w1 = -w1
    SerTxd("X = -", #w1, 9 )
  End If
  If w2 < 512 Then
    SerTxd("Y = +", #w2, 9 )
  Else
    w2 = -w2
    SerTxd("Y = -", #w2, 9 )
  End If
  If w3 < 512 Then
    SerTxd("Z = +", #w3, CR, LF )
  Else
    w3 = -w3
    SerTxd("Z = -", #w3, CR, LF )
  End If
  Pause 1000
Loop
[CODE/]

[ATTACH=CONFIG]19360.vB[/ATTACH]

@eggdweather:

 I tried 3.3V from my rapberry pi. Nothing doing. I'm using 5V now without pull-ups. That gives the rubbish terminal output above.
 

eggdweather

Senior Member
Many boards have a 3v3 regulator and will run off either 3v3 or 5 volts, but their inputs still need to be 3v3volts as distinct from 5v from a PICAXE and whatever supply voltage your running. It depends if there are pull-up resistors fitted and of what value or not.
 

hippy

Technical Support
Staff member
I tried what I suppose to be "data-clipping" to get the information down to 10 bits, but as you can see from the terminal output, the result makes no sense. I might have fried the Picaxe as well.
I doubt the PICAXE has been fried and probably not the ADXL either. The data-clipping won't work as done and will probably just confuse things further.

I would suggest going back to the earlier code which doesn't clip and report what that is showing.

Your X value seems to actually be alternating between around +11 and -11 which is perhaps encouraging. Could well be what one would expect to see with a little bit of vibration.
 

inglewoodpete

Senior Member
Back again.

The doc says the GY291 can take from 4V to 6V so there was no need to mess with 3V. I don't know what all the components are but I'd bet the pull-ups are included on the board. Maybe I fried mine from the outset with the external pull-ups.

I tried what I suppose to be "data-clipping" to get the information down to 10 bits, but as you can see from the terminal output, the result makes no sense. I might have fried the Picaxe as well.

I tried 3.3V from my rapberry pi. Nothing doing. I'm using 5V now without pull-ups. That gives the rubbish terminal output above.
Something's not right there. The code you posted does not match the so-called "rubbish" screenshot. With the code you posted, there should be no logged values like 6553x.

Also, I think you are focussing on pull-up resistors unnecessarily. The integrity of the i2c is not in question here. Your concern should be the accelerometer readings.

PS your html should read [/CODE], not [CODE/] - then your code would appear in a neat code box.
 

kfjl

Member
@Hippy:

Here's the output from your original program and what's in the configuration registers when I run it. With 10-bit two's compliment output I would have expected you to test for +ve or -ve values with $200, not $8000, but the output gives some -ve numbers. I don't understand.

The output is without moving the module. The last values in the terminal window are when I swich off.

@Inglewoodpete:

I called it a "rubbish" screenshot because as you say, there should be no logged values like 6553x. That didn't stop me getting them.

adxl_regs.jpg

Terminal.jpg
 

hippy

Technical Support
Staff member
With 10-bit two's compliment output I would have expected you to test for +ve or -ve values with $200, not $8000, but the output gives some -ve numbers. I don't understand.
The way I understand it is that all results are 16-bit two's complement, with 10 bits of data, 9-bits excluding sign, so ...

%0000 000n nnnn nnnn = 0 to +511
%1111 111n nnnn nnnn = -1 to -512
 

hippy

Technical Support
Staff member
It's hard to tell what is going on with your readings. There seems to be no reason they should jump to +511, which would imply a sudden +2G ( perhaps 4G ) force occurring.

Perhaps there is still some issue with power supply. Perhaps add some additional reservoir and decoupling capacitors.

Ignoring those odd readings, is it responding how it would be expected to when you are moving the module and actually accelerating it ?
 

kfjl

Member
By continuously tapping the module I can occasionally catch higher numbers in the terminal, but the static measurements don't change with the module's position.

Something seems to be "sticking" and needs a lot of force to make it move.
 

hippy

Technical Support
Staff member
Something seems to be "sticking" and needs a lot of force to make it move.
Could that not simply be the nature of it being an accelerometer ?

If you were dragging it down a road at 80 kph it would not actually be accelerating as such.

I admit it's not my field of expertise and I have little experience of accelerometers, but when playing with the analogue accelerometers I had I recall large signals were often quite short lived and were difficult to observe and visualise. I recall mapping an axis to an LCD bar graph so I could get a better visualisation of something happening when I moved the sensor.
 

kfjl

Member
I was under the impression that an accelerometer could measure the Earth's gravitational force exerted on its different axes and so give a tilt reading. I've seen videos of people tilting what I thought was an accelerometer to make an image move on a computer screen or make servos move. They were being moved slowly.

Anyway, I think I've hogged the forum enough. I'll let you know if I ever get it to work as I expected.

Thanks again to everyone who replied.
 

inglewoodpete

Senior Member
Could that not simply be the nature of it being an accelerometer ?

If you were dragging it down a road at 80 kph it would not actually be accelerating as such.

I admit it's not my field of expertise and I have little experience of accelerometers, but when playing with the analogue accelerometers I had I recall large signals were often quite short lived and were difficult to observe and visualise. I recall mapping an axis to an LCD bar graph so I could get a better visualisation of something happening when I moved the sensor.
The ADXL3xx chips are actually extremely sensitive. I know you were only joking but dragging the chip along a road would have the reading jumping all over the place.

Have a look at a graph I made a couple of years ago, using an ADXL335 (3-axis analogue accelerometer +/-3g FSD). I strapped the accelerometer board to a pendulum with a rubber band, with wires dangling - this has a lot to do with the noise generated on the Y and Z axes. Note that the Z axis hardly moves because its only detection was the tilt of the accelerometer as the pendulum swung. You can see the effect of gravity on the chip too: X and Y are roughly centred on ~250, while Z is around 500.
Accelerometer Plot.png
I used a 20X2 running at 64MHz, reading 4 x 10-bit ADC channels. Each channel was fed through an op-amp to amplify the 3.3-volt swing from the accelerometer to the 5-volt ADC on my 20X2. The 'X1' channel was direct from the accelerometer, while X was filtered with a 1000 ohm resistor and 10uF Tantalum and a small DC offset was added. The ripples on X1 were probably 50Hz mains hum - I can't remember for sure.

PS. The ADXL335 chip is tiny: 4mm x 4mm with 16 pads hidden underneath. I had to mount 250 of them onto custom-made PCBs by hand - a test of anyone's patience! To test each one, I used the method I have described previously: when connected to the PICAXE for logging/testing, rotate the board every-which-way through 90 degree steps, noting that each axis reading switched as gravity affected it. About one in four needed resoldering (the manufacturer does not recommend this) but I never had a single failure.
 
Last edited:

eggdweather

Senior Member
I was under the impression that an accelerometer could measure the Earth's gravitational force exerted on its different axes and so give a tilt reading. I've seen videos of people tilting what I thought was an accelerometer to make an image move on a computer screen or make servos move. They were being moved slowly.

Anyway, I think I've hogged the forum enough. I'll let you know if I ever get it to work as I expected.

Thanks again to everyone who replied.
If you want to do that (measure position in 3-axis) use something like an HMC5883 digital compass
 
Top