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

How to initialize and get MPU6050 gyro and accelerometer readings using NRF52840 DK.

I want to be able to read the gyro and accelerometer values from the MPU6050 sensor.

 

As a base start I downloaded the  NRF5 SDK V15.3.0 and modified twi_sensor_pca10056 program using editor software Kielu5 to initialize the MPU6050. My current problem is that I am not getting correct values from the gyro and accelerometer. The problem I think is coming from the MPU6050 initialization. Because the TWI supposed to work fine since its a working project and I also tested the UART by sending some initial test strings before operating.

 

I modified the project by including the following MPU related libraries in the main project:

 

    sensor_mpu6050.h

    sensor_mpu6050.c

 

I am connecting the SCL and SDL to the following respective board pins:

 

       SCL    = MPU6050_SCL_PIN,      // PIN 0.27

       SDA    = MPU6050_SDA_PIN,     // PIN 0.26

 

In the main.c , I am initializing the MPU and trying to read the MPU values and displaying them on a serial  terminal window using UART.

 THIS IS THE main.c

Fullscreen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/**
* Copyright (c) 2015 - 2019, Nordic Semiconductor ASA
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form, except as embedded into a Nordic
* Semiconductor ASA integrated circuit in a product or a software update for
* such product, must reproduce the above copyright notice, this list of
* conditions and the following disclaimer in the documentation and/or other
* materials provided with the distribution.
*
* 3. Neither the name of Nordic Semiconductor ASA nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

THIS IS THE sensor_mpu.c

Fullscreen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include "sensor_mpu6050.h"
void MPU6050_Init(void)
{
uint8_t chipid = 0;
uint8_t tmp = 0;
//reset the chip
tmp = 0x80;
i2c_write(MPU6050_I2C_ADDRESS ,MPU6050_PWR_MGMT_1, &tmp, 0x01);
for(int i=0; i<30000; i++){__ASM("nop");}
//wakeup the chip
tmp = 0x00;
i2c_write(MPU6050_I2C_ADDRESS ,MPU6050_PWR_MGMT_1, &tmp, 0x01);
//read chip id
i2c_read(MPU6050_I2C_ADDRESS ,MPU6050_WHO_AM_I, &chipid, 0x01);
for(int i=0; i<30000; i++){__ASM("nop");}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

THIS IS THE sensor_mpu.h

Fullscreen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#ifndef __MPU6050_H__
#define __MPU6050_H__
#include <stdio.h>
#include "boards.h"
#include "app_util_platform.h"
#include "app_error.h"
#include "nrf_drv_twi.h"
#include "nrf_delay.h"
#include "nrf_log.h"
#include "nrf_log_ctrl.h"
#include "nrf_log_default_backends.h"
#define MPU6050_AUX_VDDIO 0x01 // R/W
#define MPU6050_SMPLRT_DIV 0x19 // R/W
#define MPU6050_CONFIG 0x1A // R/W
#define MPU6050_GYRO_CONFIG 0x1B // R/W
#define MPU6050_ACCEL_CONFIG 0x1C // R/W
#define MPU6050_FF_THR 0x1D // R/W
#define MPU6050_FF_DUR 0x1E // R/W
#define MPU6050_MOT_THR 0x1F // R/W
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

I am attaching the current working project main program and the added mpu libraries, the complete project is inside NRF5 v15.3.0 , I left it there since there are certain libraries that are spread across the SDK.

Link to programs (main.c , sensor_mpu.c , sensor_mpu.h)

Document path link to current working project :     nRF5_SDK_15.3.0_59ac345\examples\peripheral\twi_sensor\pca10056\blank\arm5_no_packs

 

 

I would appreciate someone's help since I'm new to this type of technology, don't hesitate to ask for more information.