Possible multiple flukes using MPU-6050 breakout board

i have been testing this accelerometer/gyroscope sensor for some time now, just reading data and then mounting it to a servo so that a sketch will run to keep it ‘upright’ (leading up to making a self-balancing robot).

i now want to add Serial communication to allow for live ‘tuning’ of the kP and kD to see the effects instead of uploading the sketch every time with new values.

i have done this by using Robin2’s example code of Serial Input Basics from here;
specifically Reply#72

it had run successfully by itself, but after combining it with my MPU-6050 code, the Serial communication didn’t seem to get detected.

my first thought was that it was bad program execution flow, so i stripped it bare again and slowly added the sections.

now i have stumbled onto “success” by inadvertently connecting the MPU-6050 Vcc to 3.3V instead of 5V (previously and all this time).

so; it wasn’t the code execution at all - the question now being, what actually is happening in this situation ?

here’s the code used;

// from Robin2 thread - Serial Input Basics
// https://forum.arduino.cc/index.php?topic=288234.0

// Receive with start- and end-markers combined with parsing

const byte numChars = 32;
char receivedChars[numChars];
char tempChars[numChars];        // temporary array for use by strtok() function

      // variables to hold the parsed data
char BTcommand[numChars] = {0};
int numONE = 0;
int numTWO = 0;
float numTWOf = 0.0;

boolean newData = false;

//============

// MPU-6050 Short Example Sketch  // August 17, 2014
// By Arduino User JohnChi       // Public Domain
#include<Wire.h>
const int MPU_addr=0x68;  // I2C address of the MPU-6050
int16_t AcX,AcY,AcZ,Tmp,GyX,GyY,GyZ;

void setup() {
  Wire.begin();
  Wire.beginTransmission(MPU_addr);
  Wire.write(0x6B);  // PWR_MGMT_1 register
  Wire.write(0);     // set to zero (wakes up the MPU-6050)
  Wire.endTransmission(true);

    Serial.begin(9600);
    Serial.println("<Arduino ready \n running : MPU6050testing.ino>");
}

int qP=900;
int qD=200;

void loop() {
  
  Wire.beginTransmission(MPU_addr);
  Wire.write(0x3B);  // starting with register 0x3B (ACCEL_XOUT_H)
  Wire.endTransmission(false);
  Wire.requestFrom(MPU_addr,14,true);  // request a total of 14 registers
  AcX=Wire.read()<<8|Wire.read();  // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L)     
  AcY=Wire.read()<<8|Wire.read();  // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L)
  AcZ=Wire.read()<<8|Wire.read();  // 0x3F (ACCEL_ZOUT_H) & 0x40 (ACCEL_ZOUT_L)
  Tmp=Wire.read()<<8|Wire.read();  // 0x41 (TEMP_OUT_H) & 0x42 (TEMP_OUT_L)
  GyX=Wire.read()<<8|Wire.read();  // 0x43 (GYRO_XOUT_H) & 0x44 (GYRO_XOUT_L)
  GyY=Wire.read()<<8|Wire.read();  // 0x45 (GYRO_YOUT_H) & 0x46 (GYRO_YOUT_L)
  GyZ=Wire.read()<<8|Wire.read();  // 0x47 (GYRO_ZOUT_H) & 0x48 (GYRO_ZOUT_L)

  Serial.print("AcX\t"); Serial.print(AcX);
  Serial.print("\tGyX\t"); Serial.print(GyX);
  Serial.print("\tAcY\t"); Serial.print(AcY);
  Serial.print("\tAcZ\t"); Serial.print(AcZ);
  Serial.print("\tTmp\t"); Serial.print(Tmp/340.00+36.53);  //equation for temperature in degrees C from datasheet
  Serial.print("\tGyY\t"); Serial.print(GyY);
  Serial.print("\tGyZ\t"); Serial.println(GyZ);
  delay(333);

    recvWithStartEndMarkers();
    if (newData == true) {
        strcpy(tempChars, receivedChars);
            // this temporary copy is necessary to protect the original data
            //   because strtok() replaces the commas with \0
        //ORIGparseData();  COMBINE INTO ONE with processData()
        processData();
        newData = false;
    }
}

//============

void recvWithStartEndMarkers() {
    static boolean recvInProgress = false;
    static byte ndx = 0;
    char startMarker = '<';
    char endMarker = '>';
    char rc;
    while (Serial.available() > 0 && newData == false) {
        rc = Serial.read();
        if (recvInProgress == true) {
            if (rc != endMarker) {
                receivedChars[ndx] = rc;
                ndx++;
                if (ndx >= numChars) {
                    ndx = numChars - 1;
                }
            } else {
                receivedChars[ndx] = '\0'; // terminate the string
                recvInProgress = false;
                ndx = 0;
                newData = true;
            }
        }
        else if (rc == startMarker) {
            recvInProgress = true;
        }
    }
}

//============

void processData() {
// void ORIGparseData()

      // split the data into its parts
    char * strtokIndx; // this is used by strtok() as an index

    strtokIndx = strtok(tempChars,",");      // get the first part - the string
    strcpy(BTcommand, strtokIndx);
 
    strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
    numONE = atoi(strtokIndx);     // convert this part to an integer

    strtokIndx = strtok(NULL, ",");
    numTWOf = atof(strtokIndx);     // convert this part to a float

//    if (BTcommand=="update") 
    if (BTcommand[0]=='u') {
      qP=numONE;
      qD=(int)numTWOf;  // sloppy fix to avoid using atoi() twice which crashes ld.exe
      Serial.print("NEW values : ");
      Serial.print("qP=");
      Serial.print(qP);
      Serial.print(" qD=");
      Serial.println(qD);
    } else {
      Serial.print("qP=");
      Serial.print(qP);
      Serial.print(" qD=");
      Serial.println(qD);
    }
}

as for the hardware side, there’s no marking on what type of breakout board it is (from China), but i am assuming it is of the GY-521 type, and what looks like a voltage regulator near the Vcc pin.

i can get that the difference in Vcc would change the sensitivity of the sensor, but how does that affect the code reading/not reading the Serial data incoming ?

i can get that the difference in Vcc would change the sensitivity of the sensor,

No it would not. This is a complicated sensor and in effect contains another computer so the values would not be dependant on the voltage. What could be happening is that the sensor might be jamming up if it is supplied with too much voltage, or working just on the limit.

Check with a meter exactly what voltage is being put on the sensor, and comparing this with the voltage it says in the data sheet for normal operation.

Grumpy_Mike:
...What could be happening is that the sensor might be jamming up if it is supplied with too much voltage, or working just on the limit.

i see, and this would affect the Arduino's capability to detect incoming serial data ?

Grumpy_Mike:
Check with a meter exactly what voltage is being put on the sensor, and comparing this with the voltage it says in the data sheet for normal operation.

i have a "LED voltmeter" on the power rail (for the sensor/s) which shows 3.22-3.23 when running - there's also a HC-05 Bluetooth module on this same power rail, but it needs 5V Vcc and doesn't/didn't switch on.

not sure what datasheet would be applicable, but the GY-521 supposedly works with either voltage.
http://playground.arduino.cc/Main/MPU-6050#boards

probably hit and miss with such boards and so i have to "tune" with the given conditions which the (such?) board/s (supposedly) is flexible enough to accept.

If an I2C slave holds the clock or data lines LOW then the I2C subsystem of the Arduino totally locks up. It’s actually very difficult to recover from that situation.

It is possible that a tiny difference in voltage has caused the sensor to go into some kind of emergency shutdown or a lockup of its own. You’ve pushed it beyond its limits so there is no guarantee that anything specific will happen. Make sure you buy the sensor breakout boards which include a 3.3V regulator and level converters on the clock and data pins. Or use a 3.3V Arduino.

MorganS:
If an I2C slave holds the clock or data lines LOW then the I2C subsystem of the Arduino totally locks up. It's actually very difficult to recover from that situation.

but the Arduino doesn't "totally lock up" - the sketch still runs, looping through the other Serial.println, it just doesn't detect the Serial.available event.

i don't know if this is such a 'thing' (i could be talking nonsense) but is the "voltage being pulled one way or the other" which makes the detection get bypassed ?

MorganS:
It is possible that a tiny difference in voltage has caused the sensor to go into some kind of emergency shutdown or a lockup of its own. You've pushed it beyond its limits so there is no guarantee that anything specific will happen. Make sure you buy the sensor breakout boards which include a 3.3V regulator and level converters on the clock and data pins. Or use a 3.3V Arduino.

or a logic-level shifter ?

the thing is, i have been using the 5V supply all this time and been getting results, granted rather "noisy" - but i can get the servo to "balance" and stay still eventually.

the extra tuning is because i want to see if i can improve the speed of achieving that balance.

perhaps using a 3v3 will change all that again.

but the Arduino doesn't "totally lock up" - the sketch still runs, looping through the other Serial.println, it just doesn't detect the Serial.available event.

That is probably not happening. More likely is that something is turning off the interrupts for sufficient long that the serial input is missed. Serial input needs the interrupts to be enabled for it to work. Interrupts are also needed with the I2C.

Anyway I don’t think you should worry too much about exactly what is going wrong when you have a hardware fault.

Grumpy_Mike:
That is probably not happening. More likely is that something is turning off the interrupts for sufficient long that the serial input is missed. Serial input needs the interrupts to be enabled for it to work. Interrupts are also needed with the I2C.

i see - okay then.
i guess it confirms that the module needs to 3v3 Vcc and NOT 5V.

Grumpy_Mike:
Anyway I don’t think you should worry too much about exactly what is going wrong when you have a hardware fault.

you're referring to the 3v3 vs. 5V issue, right ?

i guess i've been lucky all this time then - i'll have to redo all the response-testing on 3v3 levels now.

you're referring to the 3v3 vs. 5V issue, right ?

Yes.

Grumpy_Mike:
Yes.

ok, thanks - will still have to investigate this (voltage level)

BUT;

SILLY GEEZER !!

BabyGeezer:
... - there's also a HC-05 Bluetooth module on this same power rail, but it needs 5V Vcc and doesn't/didn't switch on.

because it wasn't on, the TX/RX pins were free for Serial.

the Serial communication was "failing" because the HC-05 was connected at the time ! -- D'OH

the sketch uploading went through because i disconnected the power rail - KNOWING the HC-05 would block it - it didn't occur to me this would also block Serial communication to the PC when running !

i should just use SoftwareSerial and put the Bluetooth on different pins - having the Serial Monitor and "PD Tuning interface" on the same line was also sub-optimal anyway.

the 5V/3.3V is still an issue though, just eye-balling the servo movement it does seem less noisy when Vcc is via 3v3.

will have to check the differences between the various boards and determine which one i really have !

Thanks for the input though guys.