MPU6050 with Attiny85 not reading values correctly

Hello.

I am having some issues with reading correct values of the MPU6050 using the attiny85 processor.

Hardware:

  • The MPU6050 and Attiny work on other projects so they are both functioning.
    They are wired as follows
    MPU: Attiny:
    SDA → Pin 5
    SCL → Pin 7
  • The attiny is set to internal 1MHz clock

Software:
I have thinned the code down to the core parts and shown it below. It essentially waits until it connects to the MPU (it does first try reliably) and then record an ‘optimal value’. In the second part, it continuously compares the current value with the ‘optimal’ value.

I know that the Attiny can’t communicate via I2C so I have used the solution in this post: https://forum.arduino.cc/index.php?topic=318732.0 to using tinywire instead of wire but still hidden as the standard Wire.h library.

#include <Wire.h>
#include <MPU6050.h>        //Accelerometer library

MPU6050 mpu;

int angle;                  //The current reading of the accelerometer
float optimal;              //Optimal value from mode 1
byte mode = 1;              //Run mode 1 - calibration
int output = 3;             //Pin number of output

void setup()
{
  pinMode(output, OUTPUT);

  //initate accelerometer
  while (!mpu.begin(MPU6050_SCALE_2000DPS, MPU6050_RANGE_2G))
  {
    delay(500);
  }
}

void loop()
{
  if (mode == 1){
    beep(1, 1500);
    delay(3000);
    beep(3, 150);
    Vector rawAccel = mpu.readRawAccel();
    optimal = rawAccel.XAxis;
    beep(3,150);
    mode = 2;
  }

  if (mode ==2) {
    Vector rawAccel = mpu.readRawAccel();
    angle = rawAccel.XAxis;
    if (angle == optimal) {
      digitalWrite(3, HIGH);   
    }
    else {
      digitalWrite(3, LOW);
    }
     delay (20);
  }

void beep(int multi, int len) {
  for (int i = 0; i < multi; i++) {
    digitalWrite(output, HIGH);
    delay(len);
    digitalWrite(output, LOW);
    delay(len);
  }
}

Results:
The if statement that compares the current value with the optimal value. They are always exactly the same value and that value is not 0. I think that it is recording a value but then the MPU and Attiny are not communicating properly so I am not getting true values.

Any help would be greatly appreciated.
Thanks

I know that the Attiny can't communicate via I2C so I have used the solution in this post:

The ATtiny85 supports TWI Bus very well through the re-configuration of its USI Bus.

My UNO + MPU6050 is working well!

When posting about third party hardware (anything not an official arduino board) it is critical that you state which third party hardware package (sometimes called a "core") you are using.

I would suggest you use my core (link in sig), making NO modifications to any libraries. My core includes a special version of Wire.h that builds with the appropriate I2C implementation (USI, hardware I2C, or full software implementation) depending on what you're compiling for, so no changes need to be made to any libraries, and it should Just Work.

I would suggest also using a software serial library (like the "Serial" provided by my core) with external serial adapter and print out the values you're getting back from the device, so you can see what it's getting back.