<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Want MPU6050 Value through BLE</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/89399/want-mpu6050-value-through-ble</link><description>I get MPU6050 Accelerometer value zero when I using BLE. MAy be I am not abIing to read the register value.Here I attached my code</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Tue, 28 Jun 2022 13:52:20 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/89399/want-mpu6050-value-through-ble" /><item><title>RE: Want MPU6050 Value through BLE</title><link>https://devzone.nordicsemi.com/thread/374569?ContentTypeID=1</link><pubDate>Tue, 28 Jun 2022 13:52:20 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:097bd2c6-cf2d-4a73-933b-2b36910409b9</guid><dc:creator>Einar Thorsrud</dc:creator><description>&lt;p&gt;That is the only function you have that reads accelerometers values, so yes - that must be called.&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Want MPU6050 Value through BLE</title><link>https://devzone.nordicsemi.com/thread/374550?ContentTypeID=1</link><pubDate>Tue, 28 Jun 2022 13:06:49 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:10e64d96-c475-4c4e-8fc5-092fc4505c82</guid><dc:creator>ARIJIT PATRA</dc:creator><description>&lt;p&gt;Can you please tell what to modify in my code...or what function should I call...is it&amp;nbsp;&amp;nbsp;MPU6050_ReadAcc() function?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Want MPU6050 Value through BLE</title><link>https://devzone.nordicsemi.com/thread/374521?ContentTypeID=1</link><pubDate>Tue, 28 Jun 2022 11:29:05 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:422303ba-e6f3-48c3-a86c-824cd48692ca</guid><dc:creator>Einar Thorsrud</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;Looking at your code in the original post, the array&amp;nbsp;AccValue that you print the members of are never set. It is 0 as that is the default value for static variables, and as you never change it, that is what you write. You need to actually read the sensor data and copy that to&amp;nbsp;AccValue before logging it. (in fact, the only&amp;nbsp;mpu6050_* function you call in the code from the original post is&amp;nbsp;mpu6050_init(), and you never try to read as far as I can see.)&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Want MPU6050 Value through BLE</title><link>https://devzone.nordicsemi.com/thread/374496?ContentTypeID=1</link><pubDate>Tue, 28 Jun 2022 10:31:36 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:10e77151-ed91-46b4-9e04-3219a8558681</guid><dc:creator>ARIJIT PATRA</dc:creator><description>&lt;p&gt;here I attached my main file&amp;nbsp;&lt;pre class="ui-code" data-mode="c_cpp"&gt;
#include &amp;lt;stdbool.h&amp;gt;
#include &amp;lt;stdint.h&amp;gt;
#include &amp;lt;string.h&amp;gt;
#include &amp;quot;nrf_drv_twi.h&amp;quot;
#include &amp;quot;mpu6050.h&amp;quot;




//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-&amp;gt;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(&amp;amp;m_twi, &amp;amp;twi_config, twi_handler, NULL);
    APP_ERROR_CHECK(err_code);
    
    //Enable the TWI Communication
    nrf_drv_twi_enable(&amp;amp;m_twi);
}



/*
   A function to write a Single Byte to MPU6050&amp;#39;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(&amp;amp;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(&amp;amp;m_twi, MPU6050_ADDRESS, &amp;amp;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&amp;#39;s internal register
    m_xfer_done = false;
	  
    // Receive the data from the MPU6050
    err_code = nrf_drv_twi_rx(&amp;amp;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, &amp;amp;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 &amp;amp;= 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); //setting sample rate
  (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&amp;#39;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] &amp;lt;&amp;lt; 8) | buf[1];
    if(*pGYRO_X &amp;amp; 0x8000) *pGYRO_X-=65536;
		
    *pGYRO_Y= (buf[2] &amp;lt;&amp;lt; 8) | buf[3];
    if(*pGYRO_Y &amp;amp; 0x8000) *pGYRO_Y-=65536;
	
    *pGYRO_Z = (buf[4] &amp;lt;&amp;lt; 8) | buf[5];
    if(*pGYRO_Z &amp;amp; 0x8000) *pGYRO_Z-=65536;
		
    ret = true;
	}

  return ret;
}	




/*
  A Function to read accelerometer&amp;#39;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] &amp;lt;&amp;lt; 8) | buf[1];
    if(*pACC_X &amp;amp; 0x8000) *pACC_X-=65536;

    *pACC_Y= (buf[2] &amp;lt;&amp;lt; 8) | buf[3];
    if(*pACC_Y &amp;amp; 0x8000) *pACC_Y-=65536;

    *pACC_Z = (buf[4] &amp;lt;&amp;lt; 8) | buf[5];
    if(*pACC_Z &amp;amp; 0x8000) *pACC_Z-=65536;
		
    ret = true;
    }
  
  return ret;
}





&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Want MPU6050 Value through BLE</title><link>https://devzone.nordicsemi.com/thread/374415?ContentTypeID=1</link><pubDate>Mon, 27 Jun 2022 21:08:07 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:ff3b366b-9e55-43b4-9959-c2ecdfc6a0ba</guid><dc:creator>Tai</dc:creator><description>&lt;p&gt;Hi there,&lt;/p&gt;
&lt;p&gt;You should include your MPU code so that everyone can see if anything happened in that code. For the attached code, at the first glance I didn&amp;#39;t see anything wrong as you&amp;#39;re using ble_nus example to send MPU values through BLE.&lt;/p&gt;
&lt;p&gt;I would highly recommend checking this source which provides you codes for MPU and how it was combined with BLE code. Gluck!&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;a href="https://github.com/Martinsbl/nrf5-mpu-examples"&gt;Martinsbl/nrf5-mpu-examples (github.com)&lt;/a&gt;&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>