Read data from MPU-6050 accelerometer using I2C

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 6.

Moderator: Benj

Post Reply
Richard
Posts: 9
Joined: Sun Jan 10, 2016 7:18 pm
Has thanked: 2 times
Been thanked: 4 times
Contact:

Read data from MPU-6050 accelerometer using I2C

Post by Richard »

Hi, I am not new to flowcode but this is my first post and I am new to I2C.

I am trying to connect an MPU-6050 accelerometer to a pic 16f1829. The application is I have the accelerometer attached to a platform, that we drop a weight onto. I need to be able to measure the impact G forces and display them on an LCD display. The impact time is very small, and we need to capture the max G-force spike. ultimately, I want to set up a loop that is reading the data from the accel as fast as possible, compare it to the previous reading, and if it is larger, save it to the DataMax variable (or whatever variable I choose), and then display it. The problem I am having is I can't get the uC to talk to the accel. I have included my flowchart. I don't have any of the Saving, Comparing, Replacing code as that should be fairly straight forward, I need to be able to read the data first. Any help is appreciated. Thank you.

Richard

EDIT: The MPU-6050 device address and register address, are included in the file.
Attachments
16f1829 new Accel.fcfx
(9.63 KiB) Downloaded 497 times

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Read data from MPU-6050 accelerometer using I2C

Post by Benj »

Hello Richard,

Here is a generic guide on getting started with I2C and SPI.

http://www.matrixtsl.com/blog/simplifie ... c-and-spi/


Here is a blog I wrote specifically around the MPU-6050 module.

http://www.matrixtsl.com/blog/quadrocop ... -and-gyro/

Richard
Posts: 9
Joined: Sun Jan 10, 2016 7:18 pm
Has thanked: 2 times
Been thanked: 4 times
Contact:

Re: Read data from MPU-6050 accelerometer using I2C

Post by Richard »

Thanks for the response.

I was able to get the 6050 to communicate with the uC, but it is only sampling approximately 46 times per second. I have included my code again so you can see what I have done. In my sampling loop, I am turning an output on, and then immediately off, and using my oscilliscope to see how often this happens. That's how I know how fast it is sampling. I need to get this quite a bit faster (ideally 1K samples/sec). If I set the internal oscillator too high, the I2C part doesn't work, so I had to slow it down. Is I2C only capable of approximately 50 requests per second? I'm not sure where to go from here. I only need 1 axis in the device, so is there a way to get it to sample faster if it is only checking 1 axis? I am using the same module (GY-521) as you used in your example. Suggestions? Thanks

Richard

Here is the link to my uC data sheet.
http://ww1.microchip.com/downloads/en/D ... 41440A.pdf

MPU-6050 data sheet
http://www.invensense.com/products/moti ... /mpu-6050/
Attachments
16f1829 led.fcfx
(7.88 KiB) Downloaded 422 times

Richard
Posts: 9
Joined: Sun Jan 10, 2016 7:18 pm
Has thanked: 2 times
Been thanked: 4 times
Contact:

Re: Read data from MPU-6050 accelerometer using I2C

Post by Richard »

I figured it out. In the MPU-6050 component in Flowcode, I was using the "channel 1" setting instead of "software". My sample rate instantly jumped from 47 to 117 samples/sec. I then tried changing the uC clock back up to 8Mhz, and my sample rate went up to 937 samples/sec. Once I take my test output pulse out it should be right around 1K samples/sec which will be perfect. Thanks for the help.

Richard

User avatar
mikn
Posts: 209
Joined: Mon Mar 03, 2014 10:11 pm
Has thanked: 54 times
Been thanked: 41 times
Contact:

Re: Read data from MPU-6050 accelerometer using I2C

Post by mikn »

Hi, Benj

I have downloaded your example from your blog with MPU workflow.
All things seems to be working at my side until I try to use FilterPItch and FilterRoll
Here are values which I get

Code: Select all

x:-464 y:832 z:7368 R:-69.334846 P:-169199.828125
x:-492 y:832 z:7356 R:-63.843418 P:-169270.093750
x:-460 y:764 z:7480 R:-66.999824 P:-169343.187500
x:-452 y:848 z:7428 R:-64.908462 P:-169414.328125
x:-440 y:744 z:7452 R:-67.478927 P:-169487.828125
x:-564 y:812 z:7368 R:-63.472702 P:-169557.750000
x:-384 y:788 z:7376 R:-68.230735 P:-169631.828125x
x,y,z are plain data from getaccelreading
R is FilterRoll, P is FilterPitch
The board is laying on the table flat without move.

I suspect that I may be wrong with Accel1G, ResponseTime and GyroTime constants. What do they mean and how are they calculated?
FC 6.1.3.2 (18.02.2016)

Post Reply