11 Replies Latest reply on Apr 3, 2017 12:01 AM by user_1377889

    Newb: Reading values from MPU-6050 (Accelerometer + Gyro) using I2C

    viggo.wivestad

       Hi!

         

      I am really fresh when it comes to PSoC programming, I only have a little experience with arduino from some projects in the past.

         

      My project now is to read out the RAW-values from the MPU-6050, which requires the use of I2C. I have never used this before, and I am struggeling with all the new commands, and how/when to use them.

         

      All I want to do so far is to read the RAW-data and save them in different arrays.

         

       

         

      I have attached a schematic of what I've made this far, but I am kind of stumbling in the dark at the moment.

        • 1. Re: Newb: Reading values from MPU-6050 (Accelerometer + Gyro) using I2C
          viggo.wivestad

           Code (far from done):

             

           

             

          #include <device.h>

             

           

             

          void main()

             

          {

             

              /* Place your initialization/startup code here */

             

          uint8 wrData;

             

          uint8 rdData;

             

          uint8 status;

             

          uint8 readFlag;

             

          uint8 breakp;

             

          uint8 masterStatus;

             

          uint8 temp;

             

           

             

          LCD_Char_Start();

             

          LCD_Char_ClearDisplay();

             

           

             

          I2C_1_Start();

             

          I2C_1_EnableInt(); //Enables interrupt, which is required for most I2C operations.

             

           

             

          wrData = 117; //Reg number

             

          uint8 slaveAddress = 0x68;

             

          #define WR_BUFFER_SIZE       (2u)

             

          uint8 sample_segment[WR_BUFFER_SIZE];

             

           

             

           

             

              /* CyGlobalIntEnable; */ /* Uncomment this line to enable global interrupts. */

             

              for(;;)

             

              {

             

                  status = I2C_1_MasterSendStart(slaveAddress, I2C_1_WRITE_XFER_MODE);

             

           

             

          while (stats != I2C_1_MSTR_NO_ERROR);

             

          if(status == I2C_1_MSTR_NO_ERROR) // Check if transfer is completed without errors

             

          {

             

          I2C_1_MasterWriteBuf(slaveAddress,

             

          (uint8 *)sample_segment,

             

          WR_BUFFER_SIZE,

             

          I2C_1_MODE_COMPLETE_XFER);

             

          LCD_Char_PrintString("NO ERROR");

             

          CyDelay(100);

             

          }

             

              }

             

          }

             

           

             

          /* [] END OF FILE */

             
                  
          • 2. Re: Newb: Reading values from MPU-6050 (Accelerometer + Gyro) using I2C
            user_1377889

            Welcome in the fascinating world of PSoCs!

               

            At very first sight:

               

            The I2C is interrupt-driven, so you MUST enable global interrupts.

               

             

               

            It is always advisable to upload the complete project here, so that we all can have a look at. To do so, in Creator "File -> Create Workspace Bundle (minimal)" and then upload the resulting archive here.

               

             

               

            For newbees usually C-language and programming turns out to be a problem, too. When the project grows it becomes more and more complex, so when you start early to divide it into smaller pieces it will help you. so your main() may look something like

               

            main()

               

            {

               

               InitializeHardware();

               

               while(forever)

               

                {

               

                    ReadMPU6050();

               

                    WriteToLCD();

               

                }

               

            }

               

            Remember: a(ny) project gets complicated. And: A function "ReadMPU6050()" can easily be re-used in your next project.

               

             

               

            Happy coding

               

            Bob

            • 3. Re: Newb: Reading values from MPU-6050 (Accelerometer + Gyro) using I2C
              user_14586677

              Assumption made you connected SCL to SDA on accel, not the AUX connections

                 

              which do not  have pullups ?

                 

               

                 

              Also in Creator there is a working example project using I2C you can modify or

                 

              look at.

                 

               

                 

              I see on schematic different verison in notes section switched SDA, SCL, check your

                 

              version. Lastly I see you writing to I2C, but not reading the accel ? Was that config

                 

              commands for the accel in the write buffer ?

                 

               

                 

              Regards, Dana.

              • 4. Re: Newb: Reading values from MPU-6050 (Accelerometer + Gyro) using I2C
                content.librarian

                 Hello

                   

                Maybe a little late, but I'm attaching an example that I did to test MPU-6050 using Cypress PSoC 4 Pionner Kit.

                   

                 

                   

                Hernán Sánchez

                • 5. Re: Newb: Reading values from MPU-6050 (Accelerometer + Gyro) using I2C
                  user_1377889

                  Thank you very much for sharing your project with us!

                     

                  Are you building any real project with that MPU6050?

                     

                   

                     

                  Bob

                  • 6. Re: Newb: Reading values from MPU-6050 (Accelerometer + Gyro) using I2C
                    venkatesh.kuppan

                    Hello, 

                       

                    I am trying the same with PSoC 5 LP097 and MPU 6050. I do not understand as to why I observe no data on the COM port.  There is no data displayed on the COM port.

                       

                    Background:

                       
                          
                    • I am using a hyperterminal on Windows(9600 baud, COM2 used).
                    •     
                    • I have connected  10K resistances each for the SDL and SCL pins.
                    •     
                    • Besides these pins, I used VCC and GND. Rest of the pins are not connected to PSoC.
                    •    
                       

                    I have attached the project bundle here for reference. Kindly help.

                    • 7. Re: Newb: Reading values from MPU-6050 (Accelerometer + Gyro) using I2C
                      user_1377889

                      Your error is not in the UART, but in your I2C handling. The last parameter for I2C_MasterSendStart() and I2C_MasterSendRestart() is the indicator for following reads or writes. Use the defined const from I2C.h : I2C_READ_XFER_MODE, I2C_WRITE_XFER_MODE and when writing to slave I2C_ACK_DATA, I2C_NAK_DATA.

                         

                      The line

                         

                          I2C_MasterSendRestart(MPU6050_DEVICE_ADDRESS, 0x00);

                         

                      should read

                         

                          I2C_MasterSendRestart(MPU6050_DEVICE_ADDRESS, I2C_READ_XFER_MODE);

                         

                      Keep in mind that most I2C APIs return a status which indicates an error. Better check that until program works.

                         

                       

                         

                      Bob

                      • 8. Re: Newb: Reading values from MPU-6050 (Accelerometer + Gyro) using I2C
                        venkatesh.kuppan

                        Hi Bob, 

                           

                        I do not understand how reading out the register "MPU6050_RA_ACCEL_XOUT_H" gives all the 14 bytes including the temp. sensor values in the code provided by Hernán Sánchez. Isn't it supposed to be reached out to individual registers and try reading out the content?

                           

                        And after making the changes suggested by you, I observe the following behaviour - 

                           

                        1. I am able to read the bytes from the MPU6050 registers, when I do not connect the PULLUP  resistors for SDA and SCL. In case of PULL UP registers connected, the command - " I2C_MasterSendStart(MPU6050_DEVICE_ADDRESS, I2C_WRITE_XFER_MODE) " fails with a return error - I2C_MSTR_NOT_READY. Are pull up resistors not required in this case?

                           

                        2. After reading some random bytes (approx. 14*16 or 14*19), the slave device stops responding. The command - " I2C_MasterWriteByte(reg)" fails with a return value -  I2C_MSTR_ERR_LB_NAK. 

                           

                        3. How do I display the values read from the registers to the Hyperterminal. All I can see is some random characters being written on the terminal.

                           

                        Could you please provide some tips on this?

                        • 9. Re: Newb: Reading values from MPU-6050 (Accelerometer + Gyro) using I2C
                          user_1377889

                          Can you post your actual project state, please.

                             

                           

                             

                          Bob

                          • 10. Re: Newb: Reading values from MPU-6050 (Accelerometer + Gyro) using I2C
                            venkatesh.kuppan

                            OK, so I made some more changes to my code handling the return values from the I2C APIs, and now I am able to get constant data from the MPU 6050. However, the data I receive are completely zeroes. Attaching the project bundle for reference.

                            • 11. Re: Newb: Reading values from MPU-6050 (Accelerometer + Gyro) using I2C
                              user_1377889

                              Your conversion of hex values to ASCII is not correct. Consider that a uint8 keeps two hex digits, simply adding 0x30 will not convert them.

                                 

                              You might use sprintf() to convert and format into a buffer which you can send to UART. Do not forget to set the heap size to 0x0200 (System view of design wide resources).

                                 

                               

                                 

                              Bob