MPU6050 Readings

I'm building a angle mete, It all seems to work and transmits the data over NRF24L01's. But the readings seem to go a stray.

On my brought inclinometer and placed on my level bed reading 0,05 Degrees and the MPU6050 read's 0.03/0.05 which is good enough.

Then I placed them both on the 45 digress side of my speed square, brought inclinometer reads 44.9 degrees and MPU6050 reads 45.39 degress again which is not bad but then this is where the troubles starts. Placed then on the 90 degree side of the speed square and the brought inclinometer reads 89.90 and the MPU6050 reads 91.53 degree. which is not that good compared to the other readings and I've tried at various other agngles.

But what I have noticed when I place the MP6050 which is mounted inside a plastic case with flat bottom (where I ran the calibration routine and placed those of set's in the code) and placed on the 45 dgree spped square ithe the reading 45.39 degrees and if I just slight keep it pressed against the 45 degrees side and slightly tilt it back while still flat and flsuh agasint the square I can get it ot read upyo 50 degrees.

So this make me wonder the code I've found from this site may not be suited to what I need, I only need to read one axis and dont need the others.

I'll post the code in next reply has it will be over the limit. I have tried to look for a single axis but the oen that I found is discontinued, Has these are really small footprint I'd like to find a better single/daul axis inclinometer already mounted on a breakout board tp make it easier to connect to.

I only need to read up to 90 degrees in either direction as my brought inclinometer reads up to 90 degrees even if I was to rotate it 360 degrees the readings just change so that you can read it while it's up side down.

This the calibration code

// Arduino sketch that returns calibration offsets for MPU6050 
//   Version 1.1  (31th January 2014)
// Done by Luis Ródenas <>
// Based on the I2Cdev library and previous work by Jeff Rowberg <>
// Updates (of the library) should (hopefully) always be available at

// These offsets were meant to calibrate MPU6050's internal DMP, but can be also useful for reading sensors. 
// The effect of temperature has not been taken into account so I can't promise that it will work if you 
// calibrate indoors and then use it outdoors. Best is to calibrate and use at the same room temperature.

/* ==========  LICENSE  ==================================
 I2Cdev device library code is placed under the MIT license
 Copyright (c) 2011 Jeff Rowberg
 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:
 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.

// I2Cdev and MPU6050 must be installed as libraries
#include "I2Cdev.h"
#include "MPU6050.h"
#include "Wire.h"

///////////////////////////////////   CONFIGURATION   /////////////////////////////
//Change this 3 variables if you want to fine tune the skecth to your needs.
int buffersize=1000;     //Amount of readings used to average, make it higher to get more precision but sketch will be slower  (default:1000)
int acel_deadzone=8;     //Acelerometer error allowed, make it lower to get more precision, but sketch may not converge  (default:8)
int giro_deadzone=1;     //Giro error allowed, make it lower to get more precision, but sketch may not converge  (default:1)

// default I2C address is 0x68
// specific I2C addresses may be passed as a parameter here
// AD0 low = 0x68 (default for InvenSense evaluation board)
// AD0 high = 0x69
//MPU6050 accelgyro;
MPU6050 accelgyro(0x68); // <-- use for AD0 high

int16_t ax, ay, az,gx, gy, gz;

int mean_ax,mean_ay,mean_az,mean_gx,mean_gy,mean_gz,state=0;
int ax_offset,ay_offset,az_offset,gx_offset,gy_offset,gz_offset;

///////////////////////////////////   SETUP   ////////////////////////////////////
void setup() {
  // join I2C bus (I2Cdev library doesn't do this automatically)
  TWBR = 24; // 400kHz I2C clock (200kHz if CPU is 8MHz). Leonardo measured 250kHz.

  // initialize serial communication

  // initialize device

  // wait for ready
  while (Serial.available() &&; // empty buffer
  while (!Serial.available()){
    Serial.println(F("Send any character to start sketch.\n"));
  while (Serial.available() &&; // empty buffer again

  // start message
  Serial.println("\nMPU6050 Calibration Sketch");
  Serial.println("\nYour MPU6050 should be placed in horizontal position, with package letters facing up. \nDon't touch it until you see a finish message.\n");
  // verify connection
  Serial.println(accelgyro.testConnection() ? "MPU6050 connection successful" : "MPU6050 connection failed");
  // reset offsets

///////////////////////////////////   LOOP   ////////////////////////////////////
void loop() {
  if (state==0){
    Serial.println("\nReading sensors for first time...");

  if (state==1) {
    Serial.println("\nCalculating offsets...");

  if (state==2) {
    Serial.print("\nSensor readings with offsets:\t");
    Serial.print("Your offsets:\t");
    Serial.println("\nData is printed as: acelX acelY acelZ giroX giroY giroZ");
    Serial.println("Check that your sensor readings are close to 0 0 16384 0 0 0");
    Serial.println("If calibration was succesful write down your offsets so you can set them in your projects using something similar to mpu.setXAccelOffset(youroffset)");
    while (1);

///////////////////////////////////   FUNCTIONS   ////////////////////////////////////
void meansensors(){
  long i=0,buff_ax=0,buff_ay=0,buff_az=0,buff_gx=0,buff_gy=0,buff_gz=0;

  while (i<(buffersize+101)){
    // read raw accel/gyro measurements from device
    accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
    if (i>100 && i<=(buffersize+100)){ //First 100 measures are discarded
    if (i==(buffersize+100)){
    delay(2); //Needed so we don't get repeated measures

void calibration(){

  while (1){
    int ready=0;



    if (abs(mean_ax)<=acel_deadzone) ready++;
    else ax_offset=ax_offset-mean_ax/acel_deadzone;

    if (abs(mean_ay)<=acel_deadzone) ready++;
    else ay_offset=ay_offset-mean_ay/acel_deadzone;

    if (abs(16384-mean_az)<=acel_deadzone) ready++;
    else az_offset=az_offset+(16384-mean_az)/acel_deadzone;

    if (abs(mean_gx)<=giro_deadzone) ready++;
    else gx_offset=gx_offset-mean_gx/(giro_deadzone+1);

    if (abs(mean_gy)<=giro_deadzone) ready++;
    else gy_offset=gy_offset-mean_gy/(giro_deadzone+1);

    if (abs(mean_gz)<=giro_deadzone) ready++;
    else gz_offset=gz_offset-mean_gz/(giro_deadzone+1);

    if (ready==6) break;

The code used to display and transmit data

#include "I2Cdev.h"
#include "MPU6050_6Axis_MotionApps20.h"
#include "Wire.h"
#include <SPI.h>   // Comes with Arduino IDE
#include "RF24.h"  // Download and Install (See above)
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);  // Set the LCD I2C address
MPU6050 mpu;
const long interval = 750;
byte addresses[][6] = { "1Node" }; // Create address for 1 pipe.
RF24 myRadio(9, 10); // "myRadio" is the identifier you will use in following methods
// Creates 3 custom characters for the menu display
byte downArrow[8] = {
  0b00100, //   *
  0b00100, //   *
  0b00100, //   *
  0b00100, //   *
  0b00100, //   *
  0b10101, // * * *
  0b01110, //  ***
  0b00100  //   *

byte upArrow[8] = {
  0b00100, //   *
  0b01110, //  ***
  0b10101, // * * *
  0b00100, //   *
  0b00100, //   *
  0b00100, //   *
  0b00100, //   *
  0b00100  //   *
#define DEBUG
#ifdef DEBUG
//#define DPRINT(args...)  Serial.print(args)             //OR use the following syntax:
#define DPRINTSTIMER(t)    for (static unsigned long SpamTimer; (unsigned long)(millis() - SpamTimer) >= (t); SpamTimer = millis())
#define  DPRINTSFN(StrSize,Name,...) {char S[StrSize];Serial.print("\t");Serial.print(Name);Serial.print(" "); Serial.print(dtostrf((float)__VA_ARGS__ ,S));}//StringSize,Name,Variable,Spaces,Percision
#define DPRINTLN(...)      Serial.println(__VA_ARGS__)

#define DPRINTSTIMER(t)    if(false)
#define DPRINTSFN(...)     //blank line
#define DPRINTLN(...)      //blank line

#define LED_PIN 13 // 

// supply your own gyro offsets here, scaled for min sensitivity use MPU6050_calibration.ino
// -4232  -706  1729  173 -94 37
//                       XA      YA      ZA      XG      YG      ZG
int MPUOffsets[6] = { -445,  -3208,   785,    241,    23,    9 };
void i2cSetup() {
  // join I2C bus (I2Cdev library doesn't do this automatically)
  TWBR = 24; // 400kHz I2C clock (200kHz if CPU is 8MHz)
  Fastwire::setup(400, true);

volatile bool mpuInterrupt = false;     // indicates whether MPU interrupt pin has gone high
void dmpDataReady() {
  mpuInterrupt = true;


int FifoAlive = 0; // tests if the interrupt is triggering
int IsAlive = -20;     // counts interrupt start at -20 to get 20+ good values before assuming connected
// MPU control/status vars
uint8_t mpuIntStatus;   // holds actual interrupt status byte from MPU
uint8_t devStatus;      // return status after each device operation (0 = success, !0 = error)
uint16_t packetSize;    // expected DMP packet size (default is 42 bytes)
uint16_t fifoCount;     // count of all bytes currently in FIFO
uint8_t fifoBuffer[64]; // FIFO storage buffer
unsigned long previousMillis = 0;
// orientation/motion vars
Quaternion q;           // [w, x, y, z]         quaternion container
VectorInt16 aa;         // [x, y, z]            accel sensor measurements
VectorInt16 aaReal;     // [x, y, z]            gravity-free accel sensor measurements
VectorInt16 aaWorld;    // [x, y, z]            world-frame accel sensor measurements
VectorFloat gravity;    // [x, y, z]            gravity vector
float euler[3];         // [psi, theta, phi]    Euler angle container
float ypr[3];           // [yaw, pitch, roll]   yaw/pitch/roll container and gravity vector
byte StartUP = 100; // lets get 100 readings from the MPU before we start trusting them (Bot is not trying to balance at this point it is just starting up.)

void MPU6050Connect() {
  static int MPUInitCntr = 0;
  // initialize device
  mpu.initialize(); // same
  // load and configure the DMP
  devStatus = mpu.dmpInitialize();// same

  if (devStatus != 0) {
    // ERROR!
    // 1 = initial memory load failed
    // 2 = DMP configuration updates failed
    // (if it's going to break, usually the code will be 1)

    char * StatStr[5] { "No Error", "initial memory load failed", "DMP configuration updates failed", "3", "4" };


    Serial.print(F("MPU connection Try #"));
    Serial.print(F("DMP Initialization failed (code "));

    if (MPUInitCntr >= 10) return; //only try 10 times
    MPU6050Connect(); // Lets try again

  Serial.println(F("Enabling DMP..."));
  // enable Arduino interrupt detection
  Serial.println(F("Enabling interrupt detection (Arduino external interrupt pin 2 on the Uno)..."));
  Serial.print("mpu.getInterruptDrive=  "); Serial.println(mpu.getInterruptDrive());
  attachInterrupt(0, dmpDataReady, RISING); //pin 2 on the Uno
  mpuIntStatus = mpu.getIntStatus(); // Same
  // get expected DMP packet size for later comparison
  packetSize = mpu.dmpGetFIFOPacketSize();
  delay(1000); // Let it Stabalize
  mpu.resetFIFO(); // Clear fifo buffer

  mpuInterrupt = false; // wait for next interrupt

void GetDMP() { // Best version I have made so far
  // Serial.println(F("FIFO interrupt at:"));
  // Serial.println(micros());
  static unsigned long LastGoodPacketTime;
  mpuInterrupt = false;
  // sendDATA(); //send data out over RF
  FifoAlive = 1;
  fifoCount = mpu.getFIFOCount();
  if ((!fifoCount) || (fifoCount % packetSize)) { // we have failed Reset and wait till next time!
    digitalWrite(LED_PIN, LOW); // lets turn off the blinking light so we can see we are failing.
    mpu.resetFIFO();// clear the buffer and start over
    //sendDATA(); //send data out over RF
  else {
    while (fifoCount >= packetSize) { // Get the packets until we have the latest!
      mpu.getFIFOBytes(fifoBuffer, packetSize); // lets do the magic and get the data
      fifoCount -= packetSize;
    LastGoodPacketTime = millis();
    MPUMath(); // <<<<<<<<<<<<<<<<<<<<<<<<<<<< On success MPUMath() <<<<<<<<<<<<<<<<<<<
    digitalWrite(LED_PIN, !digitalRead(LED_PIN)); // Blink the Light

float Yaw, Pitch, Roll;
void MPUMath() {
  mpu.dmpGetQuaternion(&q, fifoBuffer);
  mpu.dmpGetGravity(&gravity, &q);
  mpu.dmpGetYawPitchRoll(ypr, &q, &gravity);
  Yaw = (ypr[0] * 180.0 / M_PI);
  Pitch = (ypr[1] * 180.0 / M_PI);
  Roll = (ypr[2] * 180.0 / M_PI);
  // sendDATA();

    DPRINTSFN(15, "\tYaw:", Yaw, 6, 1);
    DPRINTSFN(15, "\tPitch:", Pitch, 6, 1);
    DPRINTSFN(15, "\tRoll:", Roll, 6, 1);


void setup() {

  lcd.begin(16, 2);
  myRadio.begin();  // Start up the physical nRF24L01 Radio
  myRadio.setChannel(108);  // Above most Wifi Channels
  myRadio.setPALevel(RF24_PA_MAX);  // Uncomment for more power
  myRadio.setDataRate(RF24_250KBPS); // Fast enough.. Better range
  myRadio.openWritingPipe(addresses[0]); // Use the first entry in array 'addresses' (Only 1 right now)
  Serial.begin(115200); //115200
  while (!Serial);
  Serial.println("Setup complete");
  pinMode(LED_PIN, OUTPUT);
  // Creates the byte for the 3 custom characters
  lcd.createChar(1, upArrow); //up arrow
  lcd.createChar(2, downArrow);//down arrow
  //  u8g2.begin();

void loop() {
  if (mpuInterrupt) { // wait for MPU interrupt or extra packet(s) available
    // sendDATA();
  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;

    lcd.setCursor(0, 0);
    lcd.print("Inclinometer ");
    lcd.setCursor(5, 2);
    lcd.print(Roll, 2);
    lcd.print("  ");
    lcd.setCursor(12, 1);

    if (Roll >= 0.01) { // print the up arrow
      lcd.setCursor(15, 2);
    else {
      lcd.setCursor(15, 2);
      lcd.print(" ");
    if (Roll < 0.00) { //print the down arrow
      lcd.setCursor(0, 2);
    else {
      lcd.setCursor(0, 2);
      lcd.print(" ");

void sendDATA() {
  myRadio.write(&Roll, sizeof(Roll)); //  Transmit the data

See the OP's other post on the same topic, already answered in some detail.

The OP refuses to understand that he needs to properly calibrate the accelerometer.

I will add this, though: it is a complete waste of time and effort to use such complex code, intended for general 3D orientation in a moving object, for a tilt sensor.

This is all you need, provided that you have properly calibrated the accelerometer.

See the OP's other post on the same topic, already answered in some detail.

The OP refuses to understand that he needs to properly calibrate the accelerometer.

I will add this, though: it is a complete waste of time and effort to use such complex code, intended for general 3D orientation in a moving object, for a tilt sensor.

This is all you need, provided that you have properly calibrated the accelerometer.

Sorry I do undestand, I thought I was calibrating it correctly, Most of the readings are uniform unitl I get above 75 degrees.

So from what your saying I've got the wrong/incorrect accelerometer ?

Those links talk about the Lsm303 or ADXL345 if I've understood correctly I will carry out more research on those pages(good bed time reading).

Th end goal that I'm trying to make is basic digital inclinometer read angles from 0- to 100 degrees top's so that I can make it wireless as the small screens on them can't be read when there in position as the arm moves up in the air.

Which accelerometer would anybody recommend ?

Thanks for you time and input.

I thought I was calibrating it correctly

No, you are not, as is completely obvious from the code you posted.

The calibration links are in your other post. You not only have to read the material, you actually have to perform the calibration procedure, and apply the results in the code.

If that is too much trouble, there are other, less challenging hobbies.