This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

MPU6050 connected with nrf52840dk : not showing anything on terminal

mpu6050 connections

vcc-->vdd

gnd-->gnd

scl-->pin 27

sda-->pin 26

Main.c


#include <stdio.h>
#include "boards.h"
#include "app_util_platform.h"
#include "app_error.h"
#include "nrf_drv_twi.h"
#include "mpu6050.h"



#include "nrf_log.h"
#include "nrf_log_ctrl.h"
#include "nrf_log_default_backends.h"




// main code

int main(void)
{

// initialize the logger
    APP_ERROR_CHECK(NRF_LOG_INIT(NULL));
    NRF_LOG_DEFAULT_BACKENDS_INIT();
	
	
// create arrays which will hold x,y & z co-ordinates values of acc and gyro
    static int16_t AccValue[3], GyroValue[3];

    bsp_board_init(BSP_INIT_LEDS | BSP_INIT_BUTTONS); // initialize the leds and buttons

    twi_master_init(); // initialize the twi 
    nrf_delay_ms(1000); // give some delay

    while(mpu6050_init() == false) // wait until MPU6050 sensor is successfully initialized
    {
      NRF_LOG_INFO("MPU_6050 initialization failed!!!"); // if it failed to initialize then print a message
      nrf_delay_ms(1000);
    }

   NRF_LOG_INFO("MPU6050 Init Successfully!!!"); 

   NRF_LOG_INFO("Reading Values from ACC & GYRO"); // display a message to let the user know that the device is starting to read the values
   nrf_delay_ms(2000);


  
    
    while (true)
    {
        if(MPU6050_ReadAcc(&AccValue[0], &AccValue[1], &AccValue[2]) == true) // Read acc value from mpu6050 internal registers and save them in the array
        {
          NRF_LOG_INFO("ACC Values:  x = %d  y = %d  z = %d", AccValue[0], AccValue[1], AccValue[2]); // display the read values
        }
        else
        {
          NRF_LOG_INFO("Reading ACC values Failed!!!"); // if reading was unsuccessful then let the user know about it
        }


        if(MPU6050_ReadGyro(&GyroValue[0], &GyroValue[1], &GyroValue[2]) == true) // read the gyro values from mpu6050's internal registers and save them in another array
        {
          NRF_LOG_INFO("GYRO Values: x = %d  y = %d  z = %d", GyroValue[0], GyroValue[1], GyroValue[2]); // display then values
        }

        else
        {
          NRF_LOG_INFO("Reading GYRO values Failed!!!");
        }

       nrf_delay_ms(100); // give some delay 


    }
}

/** @} */

mpu6050.h

#ifndef AT24C02_H__
#define AT24C02_H__
#include "nrf_delay.h"

//I2C Pins Settings, you change them to any other pins
#define TWI_SCL_M           27         //I2C SCL Pin
#define TWI_SDA_M           26         //I2C SDA Pin


#define MPU6050_ADDRESS_LEN  1         //MPU6050
#define MPU6050_ADDRESS     (0xD0>>1)  //MPU6050 Device Address
#define MPU6050_WHO_AM_I     0x68U     //MPU6050 ID


#define MPU6050_GYRO_OUT        0x43
#define MPU6050_ACC_OUT         0x3B

#define ADDRESS_WHO_AM_I          (0x75U) //  WHO_AM_I register identifies the device. Expected value is 0x68.
#define ADDRESS_SIGNAL_PATH_RESET (0x68U) // 

//MPU6050 Registers addresses, see datasheet for more info and each register's function
#define MPU_SELF_TESTX_REG		0x0D	
#define MPU_SELF_TESTY_REG		0x0E	
#define MPU_SELF_TESTZ_REG		0x0F	
#define MPU_SELF_TESTA_REG		0x10	
#define MPU_SAMPLE_RATE_REG		0x19	
#define MPU_CFG_REG                     0x1A	
#define MPU_GYRO_CFG_REG		0x1B	
#define MPU_ACCEL_CFG_REG		0x1C	
#define MPU_MOTION_DET_REG		0x1F	
#define MPU_FIFO_EN_REG			0x23	
#define MPU_I2CMST_CTRL_REG		0x24	
#define MPU_I2CSLV0_ADDR_REG            0x25	
#define MPU_I2CSLV0_REG			0x26	
#define MPU_I2CSLV0_CTRL_REG            0x27	
#define MPU_I2CSLV1_ADDR_REG            0x28	
#define MPU_I2CSLV1_REG			0x29	
#define MPU_I2CSLV1_CTRL_REG            0x2A	
#define MPU_I2CSLV2_ADDR_REG            0x2B	
#define MPU_I2CSLV2_REG			0x2C	
#define MPU_I2CSLV2_CTRL_REG            0x2D	
#define MPU_I2CSLV3_ADDR_REG            0x2E	
#define MPU_I2CSLV3_REG			0x2F	
#define MPU_I2CSLV3_CTRL_REG            0x30	
#define MPU_I2CSLV4_ADDR_REG            0x31	
#define MPU_I2CSLV4_REG			0x32	
#define MPU_I2CSLV4_DO_REG		0x33	
#define MPU_I2CSLV4_CTRL_REG            0x34	
#define MPU_I2CSLV4_DI_REG		0x35	


#define MPU_PWR_MGMT1_REG		0x6B	
#define MPU_PWR_MGMT2_REG		0x6C	

#define MPU_I2CMST_STA_REG		0x36	
#define MPU_INTBP_CFG_REG		0x37	
#define MPU_INT_EN_REG			0x38	
#define MPU_INT_STA_REG			0x3A	

#define MPU_I2CMST_DELAY_REG            0x67	
#define MPU_SIGPATH_RST_REG		0x68	
#define MPU_MDETECT_CTRL_REG            0x69	
#define MPU_USER_CTRL_REG		0x6A	
#define MPU_PWR_MGMT1_REG		0x6B	
#define MPU_PWR_MGMT2_REG		0x6C	
#define MPU_FIFO_CNTH_REG		0x72	
#define MPU_FIFO_CNTL_REG		0x73	
#define MPU_FIFO_RW_REG			0x74	
#define MPU_DEVICE_ID_REG		0x75	

void twi_master_init(void); // initialize the twi communication
bool mpu6050_init(void);    // initialize the mpu6050

/**
  @brief Function for writing a MPU6050 register contents over TWI.
  @param[in]  register_address Register address to start writing to
  @param[in] value Value to write to register
  @retval true Register write succeeded
  @retval false Register write failed
*/
bool mpu6050_register_write(uint8_t register_address, const uint8_t value);

/**
  @brief Function for reading MPU6050 register contents over TWI.
  Reads one or more consecutive registers.
  @param[in]  register_address Register address to start reading from
  @param[in]  number_of_bytes Number of bytes to read
  @param[out] destination Pointer to a data buffer where read data will be stored
  @retval true Register read succeeded
  @retval false Register read failed
*/
bool mpu6050_register_read(uint8_t register_address, uint8_t *destination, uint8_t number_of_bytes);

/**
  @brief Function for reading and verifying MPU6050 product ID.
  @retval true Product ID is what was expected
  @retval false Product ID was not what was expected
*/
bool mpu6050_verify_product_id(void);


bool MPU6050_ReadGyro(int16_t *pGYRO_X , int16_t *pGYRO_Y , int16_t *pGYRO_Z );
bool MPU6050_ReadAcc( int16_t *pACC_X , int16_t *pACC_Y , int16_t *pACC_Z );

#endif


mpu6050.c

#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include "nrf_drv_twi.h"
#include "mpu6050.h"




//Initializing TWI0 instance
#define TWI_INSTANCE_ID     0

// A flag to indicate the transfer state
static volatile bool m_xfer_done = false;


// Create a Handle for the twi communication
static const nrf_drv_twi_t m_twi = NRF_DRV_TWI_INSTANCE(TWI_INSTANCE_ID);




//Event Handler
void twi_handler(nrf_drv_twi_evt_t const * p_event, void * p_context)
{
    //Check the event to see what type of event occurred
    switch (p_event->type)
    {
        //If data transmission or receiving is finished
	case NRF_DRV_TWI_EVT_DONE:
        m_xfer_done = true;//Set the flag
        break;
        
        default:
        // do nothing
          break;
    }
}



//Initialize the TWI as Master device
void twi_master_init(void)
{
    ret_code_t err_code;

    // Configure the settings for twi communication
    const nrf_drv_twi_config_t twi_config = {
       .scl                = TWI_SCL_M,  //SCL Pin
       .sda                = TWI_SDA_M,  //SDA Pin
       .frequency          = NRF_DRV_TWI_FREQ_400K, //Communication Speed
       .interrupt_priority = APP_IRQ_PRIORITY_HIGH, //Interrupt Priority(Note: if using Bluetooth then select priority carefully)
       .clear_bus_init     = false //automatically clear bus
    };


    //A function to initialize the twi communication
    err_code = nrf_drv_twi_init(&m_twi, &twi_config, twi_handler, NULL);
    APP_ERROR_CHECK(err_code);
    
    //Enable the TWI Communication
    nrf_drv_twi_enable(&m_twi);
}



/*
   A function to write a Single Byte to MPU6050's internal Register
*/ 
bool mpu6050_register_write(uint8_t register_address, uint8_t value)
{
    ret_code_t err_code;
    uint8_t tx_buf[MPU6050_ADDRESS_LEN+1];
	
    //Write the register address and data into transmit buffer
    tx_buf[0] = register_address;
    tx_buf[1] = value;

    //Set the flag to false to show the transmission is not yet completed
    m_xfer_done = false;
    
    //Transmit the data over TWI Bus
    err_code = nrf_drv_twi_tx(&m_twi, MPU6050_ADDRESS, tx_buf, MPU6050_ADDRESS_LEN+1, false);
    
    //Wait until the transmission of the data is finished
    while (m_xfer_done == false)
    {
      }

    // if there is no error then return true else return false
    if (NRF_SUCCESS != err_code)
    {
        return false;
    }
    
    return true;	
}




/*
  A Function to read data from the MPU6050
*/ 
bool mpu6050_register_read(uint8_t register_address, uint8_t * destination, uint8_t number_of_bytes)
{
    ret_code_t err_code;

    //Set the flag to false to show the receiving is not yet completed
    m_xfer_done = false;
    
    // Send the Register address where we want to write the data
    err_code = nrf_drv_twi_tx(&m_twi, MPU6050_ADDRESS, &register_address, 1, true);
	  
    //Wait for the transmission to get completed
    while (m_xfer_done == false){}
    
    // If transmission was not successful, exit the function with false as return value
    if (NRF_SUCCESS != err_code)
    {
        return false;
    }

    //set the flag again so that we can read data from the MPU6050's internal register
    m_xfer_done = false;
	  
    // Receive the data from the MPU6050
    err_code = nrf_drv_twi_rx(&m_twi, MPU6050_ADDRESS, destination, number_of_bytes);
		
    //wait until the transmission is completed
    while (m_xfer_done == false){}
	
    // if data was successfully read, return true else return false
    if (NRF_SUCCESS != err_code)
    {
        return false;
    }
    
    return true;
}



/*
  A Function to verify the product id
  (its a basic test to check if we are communicating with the right slave, every type of I2C Device has 
  a special WHO_AM_I register which holds a specific value, we can read it from the MPU6050 or any device
  to confirm we are communicating with the right device)
*/ 
bool mpu6050_verify_product_id(void)
{
    uint8_t who_am_i; // create a variable to hold the who am i value


    // Note: All the register addresses including WHO_AM_I are declared in 
    // MPU6050.h file, you can check these addresses and values from the
    // datasheet of your slave device.
    if (mpu6050_register_read(ADDRESS_WHO_AM_I, &who_am_i, 1))
    {
        if (who_am_i != MPU6050_WHO_AM_I)
        {
            return false;
        }
        else
        {
            return true;
        }
    }
    else
    {
        return false;
    }
}


/*
  Function to initialize the mpu6050
*/ 
bool mpu6050_init(void)
{   
  bool transfer_succeeded = true;
	
  //Check the id to confirm that we are communicating with the right device
  transfer_succeeded &= mpu6050_verify_product_id();
	
  if(mpu6050_verify_product_id() == false)
    {
	return false;
      }

  // Set the registers with the required values, see the datasheet to get a good idea of these values
  (void)mpu6050_register_write(MPU_PWR_MGMT1_REG , 0x00); 
  (void)mpu6050_register_write(MPU_SAMPLE_RATE_REG , 0x07); 
  (void)mpu6050_register_write(MPU_CFG_REG , 0x06); 						
  (void)mpu6050_register_write(MPU_INT_EN_REG, 0x00); 
  (void)mpu6050_register_write(MPU_GYRO_CFG_REG , 0x18); 
  (void)mpu6050_register_write(MPU_ACCEL_CFG_REG,0x00);   		

  return transfer_succeeded;
}



/*
  Read the Gyro values from the MPU6050's internal Registers
*/ 
bool MPU6050_ReadGyro(int16_t *pGYRO_X , int16_t *pGYRO_Y , int16_t *pGYRO_Z )
{
  uint8_t buf[6]; 
  
  bool ret = false;	
	
  if(mpu6050_register_read(MPU6050_GYRO_OUT,  buf, 6) == true)
  {
    *pGYRO_X = (buf[0] << 8) | buf[1];
    if(*pGYRO_X & 0x8000) *pGYRO_X-=65536;
		
    *pGYRO_Y= (buf[2] << 8) | buf[3];
    if(*pGYRO_Y & 0x8000) *pGYRO_Y-=65536;
	
    *pGYRO_Z = (buf[4] << 8) | buf[5];
    if(*pGYRO_Z & 0x8000) *pGYRO_Z-=65536;
		
    ret = true;
	}

  return ret;
}	




/*
  A Function to read accelerometer's values from the internal registers of MPU6050
*/ 
bool MPU6050_ReadAcc( int16_t *pACC_X , int16_t *pACC_Y , int16_t *pACC_Z )
{
  uint8_t buf[6];
  bool ret = false;		
  
  if(mpu6050_register_read(MPU6050_ACC_OUT, buf, 6) == true)
  {
    mpu6050_register_read(MPU6050_ACC_OUT, buf, 6);
    
    *pACC_X = (buf[0] << 8) | buf[1];
    if(*pACC_X & 0x8000) *pACC_X-=65536;

    *pACC_Y= (buf[2] << 8) | buf[3];
    if(*pACC_Y & 0x8000) *pACC_Y-=65536;

    *pACC_Z = (buf[4] << 8) | buf[5];
    if(*pACC_Z & 0x8000) *pACC_Z-=65536;
		
    ret = true;
    }
  
  return ret;
}





i also tried in putty, but nothing is showing in the terminal

any answers, without hesitation ask any question.

Parents
  • Hello,

    i also tried in putty, but nothing is showing in the terminal

    any answers, without hesitation ask any question.

    Which backend are you using for your logger module, UART or RTT?
    The Segger Embedded Studio's Debug Terminal ( not the terminal emulator ), uses RTT - and you will therefore need to configure your logger to use RTT if you want anything to show on this terminal.
    Additionally, you will need to make sure that NRF_FPRINTF_FLAG_AUTOMATIC_CR_ON_LF_ENABLED is defined to 0.
    Could you also verify for me whether you are using deferred logging or not?

    You are including a lot of code regarding your SPI, while the problem seems to be related to displaying logger outputs.
    To verify that your logger problems are resolved, please add a logger output immediately following the successful initialization of the logger module. I.e "Logger initialized", or similar.

    Looking forward to resolving this issue together!

    Best regards,
    Karl

  • Sorry for the late reply

    i think based on the code it is uart, then i have enabled RTT and also  NRF_FPRINTF_FLAG_AUTOMATIC_CR_ON_LF_ENABLED , i am not using defferred logger .

     I.e "Logger initialized", or similar. 

    i have added this but nothing is showing on the putty or terminal( DEBUG AND TERMINAL EMULATOR)

  • Hello,

    saver said:
    Sorry for the late reply

    No problem at all.

    saver said:
    i think based on the code it is uart, then i have enabled RTT and also  NRF_FPRINTF_FLAG_AUTOMATIC_CR_ON_LF_ENABLED , i am not using defferred logger .

    I am not sure I understand what you meant by this. You were using UART backend, but now you have disabled it and instead enabled RTT backend - also remembering to set *_CR_ON_LF_ENABLED to 0?

    saver said:
     I.e "Logger initialized", or similar. 

    Please elaborate what you mean by this.

    saver said:
    i have added this but nothing is showing on the putty or terminal( DEBUG AND TERMINAL EMULATOR)

    What is your LOG level configured to?
    As far as I know putty does not support RTT logging. You will need to either use Segger Embedded Studio's Debug Terminal, or the standalone RTT Viewer application for desktop, provided by Segger.
    Could you verify for me that you are using either of these two application when you say that you are not seeing anything outputted to your terminal?

    Best regards,
    Karl

  • i want to display the gyro values in the the putty terminal.

    i have enabled the log at sdk_config file

    Actually i have referred to this video to do take the values from mpu6050 , but nothing is displaying on the putty terminal, i have done everything as described in the video, any idea why it is not displaying in the putty terminal, in the below video ,he has taken for pca10040 and i have done the program in pca10056

    https://www.youtube.com/watch?v=fH59DQQNOjU&t=5s

    ///////////////////////////////////////////////////////////////////////////////////////////////////////

    when i try to debug, then the nrf_log_info command is not displaying any reading in the debug terminal too

    ///////////////////////////////////////////////////////////////////////////////////////////////////////

    actually i need to see the gyro values in the putty terminal for my project.

  • saver said:
    i want to display the gyro values in the the putty terminal.
    saver said:
    i need to see the gyro values in the putty terminal for my project.

    As I said, the putty terminal does not support RTT. In the youtube tutorial you linked, he used the loggers UART backend to display the values on the putty terminal.
    My understanding was that you wanted to use RTT, but if you would like to user UART backend instead, please change the defines in your sdk_config to reflect this. Set RTT_BACKEND back to 0, and UART_BACKEND back to 1.
    Make sure NRF_LOG_ENABLED is defined to 1 also.

    Make these changes, and let me know if you are seeing anything in your putty terminal following this.

    Best regards,
    Karl

    P.S The complete code for the tutorial you referenced is available in the video description - you should take a look there, and see how you sdk_config file is different from the one provided with the project. I think this will give you some clarity as to why it did not work as intended right away.

Reply
  • saver said:
    i want to display the gyro values in the the putty terminal.
    saver said:
    i need to see the gyro values in the putty terminal for my project.

    As I said, the putty terminal does not support RTT. In the youtube tutorial you linked, he used the loggers UART backend to display the values on the putty terminal.
    My understanding was that you wanted to use RTT, but if you would like to user UART backend instead, please change the defines in your sdk_config to reflect this. Set RTT_BACKEND back to 0, and UART_BACKEND back to 1.
    Make sure NRF_LOG_ENABLED is defined to 1 also.

    Make these changes, and let me know if you are seeing anything in your putty terminal following this.

    Best regards,
    Karl

    P.S The complete code for the tutorial you referenced is available in the video description - you should take a look there, and see how you sdk_config file is different from the one provided with the project. I think this will give you some clarity as to why it did not work as intended right away.

Children
  • Set RTT_BACKEND back to 0, and UART_BACKEND back to 1.
    Make sure NRF_LOG_ENABLED is defined to 1 also.

    i have done what you have said above.

    Make these changes, and let me know if you are seeing anything in your putty terminal following this.

    i have made the changes, but nothing is shown in putty terminal.

    i have only changed the pin numbers to 27 and 26 from 22,23 from the mpu6050.h, from the files in the video.

    //I2C Pins Settings, you change them to any other pins
    #define TWI_SCL_M 27 //I2C SCL Pin
    #define TWI_SDA_M 26 //I2C SDA Pin

    ///////////////////////////////////////////////////////////////////////////

    putty terminal is just showing a blank screen,

    can you suggest me some solution

  • Hello,

    Sorry for my late reply.

    saver said:
    i have done what you have said above.

    Great. Could you share your sdk_config file, so I may take a look? I just want to rule out the possibility that we are overlooking a define here.

    saver said:
    i have made the changes, but nothing is shown in putty terminal.

    Did you do as I suggested, and take a look at the code supplied with the tutorial you are following? The tutorial's author has included the source code in the video description, you should take a look at the authors sdk_config file, to see how it is different to your own.

    Looking forward to resolving this issue together!

    Best regards,
    Karl

  • Great. Could you share your sdk_config file, so I may take a look? I just want to rule out the possibility that we are overlooking a define here.

    the sdk file

    https://drive.google.com/drive/folders/1jlCYiiSo4Ez7Fjs5j_bELG4sxPdK5bZf?usp=sharing

    the above link takes you to twi_mpu6050 folder pca10056--> ses file

    Did you do as I suggested, and take a look at the code supplied with the tutorial you are following? The tutorial's author has included the source code in the video description, you should take a look at the authors sdk_config file, to see how it is different to your own.

    i put the same main.c file and followed all the instructions told in the video, there was no sdk file in the description of the video only main.c , mpu6050.c, mpu6050.h file.

  • saver said:

    the sdk file

    https://drive.google.com/drive/folders/1jlCYiiSo4Ez7Fjs5j_bELG4sxPdK5bZf?usp=sharing

    the above link takes you to twi_mpu6050 folder pca10056--> ses file

    saver said:
    i put the same main.c file and followed all the instructions told in the video, there was no sdk file in the description of the video only main.c , mpu6050.c, mpu6050.h file.

    I see now that you are using the twi_scanner example as a base for your application. The example uses deferred logging, which means that you will need to call NRF_LOG_PROCESS() at some point, to have the logs be processed.
    Could you try to define NRF_LOG_DEFERRED to 0, and see if you then see the logger output in your putty terminal?
    Alternatively, you should call NRF_LOG_PROCESS at some point in your main loop.

    Let me know if this resolves your issue,

    Best regards,
    Karl

  • Could you try to define NRF_LOG_DEFERRED to 0, and see if you then see the logger output in your putty terminal?

    Thanks, now it is showing the gyro meter values, very much Thank you!!!

    Alternatively, you should call NRF_LOG_PROCESS at some point in your main loop.

    I don't understand what you mean by this, can you please elaborate.

    Let me know if this resolves your issue,

    yes, but can you also tell me how to calibrate the sensor.

Related