From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

Academic Hardware Products (myDAQ, myRIO)

cancel
Showing results for 
Search instead for 
Did you mean: 

Communicating to the ADXL345 via the myRIO

Hello folks,

 

I was wondering if perhaps I could get some assistance in regards in communicating with the sparkfun ADXL345 accelerometer through the myRIO. So far, my attempts to communicate with the accelerometer has not gone very well, and I'm getting an error message saying that I'm receiving a NACK from the accerlometer. I have tried it only through the I2C as that seems to be the most simple setup according to the accelerometer's datasheet. I believe my main issue is how to handle the slave address and then point to the proper register locations. Below, is a link to the accelerometer datasheet (Page 10 shows the I2C setup and a figure showing how data is sent from the control device and theaccelerometer).

 

https://www.sparkfun.com/datasheets/Sensors/Accelerometer/ADXL345.pdf

 

I've only tried writing to the accelerometer as some registers need an initialized value.

 

 

0 Kudos
Message 1 of 13
(11,423 Views)

Hey DragoTheWise,

 

Here is a quick shot at the code to read from the ADXL345.

 

ADXL345.PNG

  • The I2C VI is set to Read / Write
  • The Slave address is set to 0x1D (the Express VI will set the read / write bit for you) this assumes the ALT pin is high.
  • We first write the register pointer to 0x32 to point to the X0 register
  • Then read 6 consecutive registers and join the low and high bytes of X, Y, Z.  I didn't take the time to convert them from 2's compliment.
  • Make sure you connect pull up resistors to the SDA and SCL lines.

I don't have the sensor to test with so let us know if that works or if you have additional questions.

 

 

-Sam K

LabVIEW Hacker

Join / Follow the LabVIEW Hacker Group on google+

 

0 Kudos
Message 2 of 13
(11,403 Views)

Hey Sammy,

 

Thanks for the help; however, I was still unable to read data from the device. With the setup you gave me, I was getting a NACK from the accelerometer. I tried toying with the setup on both software and with the hardware, but I didn't get anything different. Any other suggestions?

 

Thanks,

 

~Drago

0 Kudos
Message 3 of 13
(11,370 Views)

Hey Drago,

 

What value pull up resistors are you using?

 

It also looks like you'll have to bring the device out of standby mode by writing 0b00001000 to register 0x2D.

 

Capture.PNG

 

Let us know if that works (Sorry, I can't test it since I don't have the device)

 

 

-Sam K

LabVIEW Hacker

Join / Follow the LabVIEW Hacker Group on google+

 

0 Kudos
Message 4 of 13
(11,360 Views)

Hey Sammy,

 

I'm currently using 2.2k ohmn resistors. I did play around with the values some just to see if anything different would happen. It seems like anything below 1k causes issues. Now, concerning the code. I first checked the first code you gave me once again and somehow it works to an extent (maybe a wire was out of place before). It'll run and not spit out any errors; however, I consistently get 0's for the data. So it seems like it is able to fully communicate with the device, but can't collect the data properly which is what the latest code was to fix. Now, for the recent one, I tried it out and I end up getting a different error : -363002. It says that the channel that was specified is incorrect. This happens on the part of the code trying to write to the 0x2D register to go into "Measure" mode. I'm not sure why that is an incorrect channel as that is the correct slaveaddress of the device and the register. I looked through the accelerometer data sheet again, and I don't see an issue with the current setup. Perhaps we are overlooking something? Thank you again for the time and effort for helping me.

 

~Drago

0 Kudos
Message 5 of 13
(11,352 Views)

Hey Drago,

 

If you open the express VIs you'll notice that the channel drop down is empty.  That is because I opened and saved it outside of the myRIO context without thinking about it.  Just open each express VI and choose the appropriate I2C channel and run it again.

 

-Sam K

LabVIEW Hacker

Join / Follow the LabVIEW Hacker Group on google+

Message 6 of 13
(11,341 Views)

Hey Sammy!

 

Thanks for pointing that out. On my side, it showed the correct channel, but I suppose it didn't generate the code quite right. So after I switched to B and then back to A, everything started to work perfectly. Thank you for all your help!

 

 

~Drago

0 Kudos
Message 7 of 13
(11,321 Views)

Happy to help.  The data sheet has everything you need to know, but it can be a challenge to read them some times.

 

If you have any questions about how / why any of the code works the way it does let me know.

 

Thanks!

 

-Sam K

LabVIEW Hacker

Join / Follow the LabVIEW Hacker Group on google+

0 Kudos
Message 8 of 13
(11,316 Views)

hi Sam,

 

The accel read VI is working but data is insignificant. there is't any nagative data. how can i convert to +g and -g data type? 

 

Thank you..

Harun TURAN

0 Kudos
Message 9 of 13
(11,134 Views)

The sensor returns data in 2's Compliment as noted in the data sheet.  Check out the wikipedia page if you're not famliar with 2's Compliment.

 

I have a VI in the LabVIEW Hacker  toolbox that converts from 2's Compliment to decimal (for U16s only, but thats what you need in this case).

 

You can grab it from the LVH GitHub repository (direct download here)

 

Let us know if that works.

 

-Sam K

LabVIEW Hacker

Join / Follow the LabVIEW Hacker Group on google+

0 Kudos
Message 10 of 13
(11,117 Views)