Interface both MPU6050 and 16x2LCD with arduino

Hey there,

I'm new to I2C technology, MPU6050 (accelerometer cum gyroscope) is an I2C based device, which is connected with my arduino.....

When I connect my 16x2 LCD display with it, the MPU6050 stops working. It is something related to pull down resistor (stuffs like that).

So I'm unable to combine them together in my project.

Kindly help me out to use both device simultaneously.

Libraries using:

MPU6050 -> wire.h, MPU6050.h, I2Cdev.h

LCD (16x2) -> liquidCrystal.h

/////////////////////////////
My code is
/////////////////////////////

#include <EEPROM.h>
#include <LiquidCrystal.h>
#include "I2Cdev.h"
#include "MPU6050.h"

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
    #include "Wire.h"
#endif

MPU6050 accelgyro;
//MPU6050 accelgyro(0x69); // <-- use for AD0 high

int16_t ax, ay, az,axpre=0, aypre=0, azpre=0;
int count = 0;
int16_t gx, gy, gz;

#define OUTPUT_READABLE_ACCELGYRO



#define LED_PIN 13
bool blinkState = false;
int value;
void setup() {
    // join I2C bus (I2Cdev library doesn't do this automatically)
    #if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
        Wire.begin();
    #elif I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE
        Fastwire::setup(400, true);
    #endif

    //lcd.begin(16, 2);

    // initialize serial communication
    // (38400 chosen because it works as well at 8MHz as it does at 16MHz, but
    // it's really up to you depending on your project)
    Serial.begin(9600);

    // initialize device
    Serial.println("Initializing PEDOMETER ...");
    accelgyro.initialize();

    // verify connection
    Serial.println("Testing device connections...");
    Serial.println(accelgyro.testConnection() ? "Pedometer connection successful" : "Pedometer connection failed");

    pinMode(LED_PIN, OUTPUT);
}

void loop() {
    // read raw accel/gyro measurements from device
    digitalWrite(7,LOW);
    digitalWrite(6,HIGH);
    
    if(digitalRead(6) == HIGH)
    {
    
    Serial.print("Steps counted so far : ");
    Serial.println(EEPROM.read(100));
    
    
    axpre = ax; aypre = ay; azpre = az;
    accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);


    #ifdef OUTPUT_READABLE_ACCELGYRO
        // display tab-separated accel/gyro x/y/z values
        
        if((ax > (axpre + 2500))&&(ay > (aypre + 2500))||(az > (azpre + 2500))&&(ay > (aypre + 2500))||(ax > (axpre + 2500))&&(az > (azpre + 2500)))
        {
          count ++;
        }
        
        if(digitalRead(3) == LOW)
        {
          
        //printing X, Y, Z co-ordinates  
        Serial.print("a/g:\t");
        Serial.print(ax); Serial.print("\t");
        Serial.print(ay); Serial.print("\t");
        Serial.print(az); Serial.print("\t");
        Serial.print(gx); Serial.print("\t");
        Serial.print(gy); Serial.print("\t");
        Serial.print(gz);Serial.print("\t");
        Serial.println(count);
        }
        else
        {
          Serial.println();
          Serial.print("Total number of Steps : ");
          
          
          Serial.println(count);
          EEPROM.write(100, count);
          value = EEPROM.read(100);
 /*         lcd.setCursor(0,0);
          lcd.print("Total Steps:");
          lcd.setCursor(13,0);
          lcd.print(value);
          
          lcd.setCursor(0,1);
          lcd.print("Distance :");
          lcd.setCursor(13,0);
          lcd.print(value*0.3);
   */       
          Serial.print("Value Stored in EEPROM");
          Serial.println(value);
      }
        
    #endif

    #ifdef OUTPUT_BINARY_ACCELGYRO
        Serial.write((uint8_t)(ax >> 8)); Serial.write((uint8_t)(ax & 0xFF));
        Serial.write((uint8_t)(ay >> 8)); Serial.write((uint8_t)(ay & 0xFF));
        Serial.write((uint8_t)(az >> 8)); Serial.write((uint8_t)(az & 0xFF));
        Serial.write((uint8_t)(gx >> 8)); Serial.write((uint8_t)(gx & 0xFF));
        Serial.write((uint8_t)(gy >> 8)); Serial.write((uint8_t)(gy & 0xFF));
        Serial.write((uint8_t)(gz >> 8)); Serial.write((uint8_t)(gz & 0xFF));
    #endif

    // blink LED to indicate activity
    blinkState = !blinkState;
    digitalWrite(LED_PIN, blinkState);
}
else

{
  
          Serial.println("LCD IS PRINTING");
          digitalWrite(13, HIGH);
          lcd.setCursor(0,0);
          lcd.print("Total Steps:");
          lcd.setCursor(13,0);
          lcd.print(value);
          delay(500);         
          lcd.setCursor(0,1);
          lcd.print("Distance :");
          lcd.setCursor(13,0);
          lcd.print(value*0.3);
          digitalWrite(13,LOW);
}


}

You should add pullups to the SCL/SDA lines, if not yet done. In case of trouble stay with minimal transmission rates, higher rates require properly terminated bus lines.

This is a known problem on the MPU.

The MPU just freezes, right?

Stop using the demo code and start pulling the data.
Just get data everytime you went through the loop.

Get lost of the interrupt pin, you don't need it. You can get the interrupt status by using:
mpuIntStatus = mpu.getIntStatus();
If you are using interrupts, system will freeze.
Disable them.

Adjust the i2c spped:
mpu.setRate(9); // 9=100Hz -> 1khz / ( 1 + rate )
mpu.setIntEnabled(false); // disable Interrups on the MPU (Important!)

And reset the MPU if the packet size (42 bytes) is "funny"

if (fifoCount % packetSize != 0)
{
mpu.resetFIFO();
}

There is another error in the demo code. You need to get ALL packets if fifo > 42 bytes.

There should be a line:
if (fifoCount >= packetSize)

change it to while and adjust the code behind it.

I search for that bit.h 4 weeks, but my test code is gone and I'm just using z values in my current code.

And of course:
You can use the internal pullups of the arduino or even better real ones.

After all of that, buy a Oled display on spi for a couple of bucks and use the i2c only for the mpu to gain bus speed. :grinning:

Hope that helps,

  • Rene