4 Replies Latest reply on Nov 29, 2018 12:28 AM by MoTa_728816

    MPU6050 Need help for the direction and speed

      hello, here I attached my project, im using mpu6050 sensor to run 2 motors. I should control the speed and the direction of the motors but Im struggling with writing the loop and getting the values for the gyroscope and the acceleration. I really need helps with that so please check the file that I attached. Thank you.

        • 2. Re: MPU6050 Need help for the direction and speed

          The file i2cfunctions.c and probably .h are missing from the project, so I cannot compile successfully.

          You will need to put reads from the MPU and writes to the motor-drivers into the for-loop just before the CyDelay()

          Check whether A0 of MPU is tied high, otherwise use I2C-address 0x68h



          • 3. Re: MPU6050 Need help for the direction and speed



            It sounds like a fun project!


            Although I don't have access to MPU650

            and I don't know what kind of motors you are using,

            I tried to write a main.c skeleton.


            I used a uint16_t for command, but only the first 4 bit is used for determine what to do,

            so remaining 12 bit can be used to specify the speed etc.

            Needless to say, there are infinite methods to implement what you are saying,

            so this is just a sample idea from me.



            #include <project.h>

            #include <mpu6050.h>

            #include <stdio.h>


            /* sample command flag */

            /* right motor on  0x0001 */

            /* left  motor on  0x0002 */

            /* right motor back 0x0004 */

            /* left  motor back 0x0008 */

            #define RIGHT_FORWARD  0x0001

            #define RIGHT_BACKWARD 0x0004

            #define LEFT_FORWARD  0x0002

            #define LEFT_BACKWARD  0x0008

            #define STOP          0x0000

            #define FORWARD    (RIGHT_FORWARD  | LEFT_FORWARD)


            #define TURN_RIGHT (RIGHT_FORWARD)

            #define TURN_LEFT  (LEFT_FORWARD)




            //    RMotor_WriteCompare(100);

            //    LMotor_WriteCompare(100);


                //OFFSET VALUES

                float accelXOFF,accelYOFF,accelZOFF;

                float gyroXOFF,gyroYOFF,gyroZOFF;

                //REAL VALUES

                float  ax, ay, az; //acceleration floats

                float  gx, gy, gz; //gyroscope floats


            void init_hardware(void)

















            * get_sensor_data

            * acquire data from MPU650


            void get_sensor_data(void)


                //RAW VALUES

                int16_t accelX, accelY, accelZ; // variables for accelerometer raw data

                int16_t gyroX, gyroY, gyroZ; // variables for gyro raw data


                accelX = MPU6050_getAccelerationX();

                accelY = MPU6050_getAccelerationY();

                accelZ = MPU6050_getAccelerationZ();


                gyroX = MPU6050_getRotationX();

                gyroY = MPU6050_getRotationY();

                gyroZ = MPU6050_getRotationZ();


                accelXOFF = accelXOFF + accelX;

                accelYOFF = accelYOFF + accelY;

                accelZOFF = accelZOFF + accelZ;


                gyroXOFF = gyroXOFF + gyroX;

                gyroYOFF = gyroYOFF + gyroY;

                gyroZOFF = gyroZOFF + gyroZ;  


                accelXOFF = accelXOFF/100;

                accelYOFF = accelYOFF/100;

                accelZOFF = accelZOFF/100;


                gyroXOFF = gyroXOFF/100;

                gyroYOFF = gyroYOFF/100;

                gyroZOFF = gyroZOFF/100;




            * calc_what_do_do

            * calculate the direction etc

            * and decide what to command to the motors


            uint16_t calc_what_to_do(void)


                uint16_t what_to_do = STOP ;


                /* calc and decide what to let the motors do */

                /* forward, backward, turn etc */

                /* turn can be slow moving turn

                * or spin at current place




                return( what_to_do ) ;



            void do_motors(uint16_t command)


                switch(command & 0x000F) { /* currently using only 4 bit */

                case FORWARD:

                    /* turn both motors to forward direction */

                    break ;

                case BACKWARD:

                    /* turn both motors to backword direction */

                    break ;

                case TURN_RIGHT:

                    /* turn right motor forward direction */

                    /* stop left motor or slower forward direction  */

                    break ;

                case TURN_LEFT:

                    /* stop right motor or slower forward direction */

                    /* turn left motor forward direction */

                    break ;

                case SPIN_RIGHT:

                    /* turn right motor forward */

                    /* turn left motor backward */

                    break ;

                case SPIN_LEFT:

                    /* turn right motor backward */

                    /* turn left motor forward */

                    break ;

                case STOP:


                    /* stop both motors */

                    break ;




            int main()


                uint32_t command ;


                init_hardware() ;




                    get_sensor_data() ;

                    command = calc_what_to_do() ;

                    do_motors(command) ;

                    CyDelay(15); /* 15ms delay */