MPU-6050 blocks transfer data via Serial to bluetooth component

Hello.

I work on my project on Arudino UNO. I have the Bluetooth Classic module HC-06 and MPU-6050. For communication with bluetooth module I use Serial and transfer data via TX (1) and RX (0) pins and all are okey, but when I try to use MPU-6050, when bluetooth is connected to Arduino - MPU blocks transfer of data, what's a problem?

imu.hpp code.

#pragma once

#include "I2Cdev.h"
#include "MPU6050_6Axis_MotionApps20.h"
#include "Wire.h"

    class IMU
    {
    public:
        IMU()
        {
            Wire.begin();
            _device.initialize();
            const uint8_t status = _device.dmpInitialize();

            if (_device.testConnection() == false || status != 0)
                return;

            _device.setXAccelOffset(0);
            _device.setYAccelOffset(0);
            _device.setZAccelOffset(0);
            _device.setXGyroOffset(0);
            _device.setYGyroOffset(0);
            _device.setZGyroOffset(0);

            _valid = true;
        }

        ~IMU()
        {
            _valid = false;
        }

        inline void calibrate()
        {
            _calibrated = false;
            _device.setDMPEnabled(false);

            if (!valid())
                return;

            _device.CalibrateAccel(CABLIRATION_ITERATIONS);
            _device.CalibrateGyro(CABLIRATION_ITERATIONS);

            _calibrated = true;
            _device.setDMPEnabled(true);
        }

        inline bool calibrated() const noexcept
        {
            return _calibrated;
        }

        inline bool valid() const noexcept
        {
            return _valid;
        }

        operator bool() const noexcept
        {
            return valid();
        }

        inline Quaternion get_angle() const noexcept
        {
            if (!valid())
                return {};

            Quaternion value{};

            if (_device.dmpGetCurrentFIFOPacket(_fifoBuffer))
                _device.dmpGetQuaternion(&value, _fifoBuffer);

            return value;
        }

        inline VectorInt16 get_accel() const noexcept
        {
            if (valid())
                return {};

            VectorInt16 value;

            if (_device.dmpGetCurrentFIFOPacket(_fifoBuffer))
                _device.dmpGetAccel(&value, _fifoBuffer);

            return value;
        }

    private:
        bool _calibrated{false};
        bool _valid{false};

        MPU6050 _device{0x68};

        uint8_t _fifoBuffer[64]{};

        static constexpr uint8_t CABLIRATION_ITERATIONS{15};
    };

main.ino code

#include "bluetooth.hpp" // in this header I use Serial as default to communication with bluetooth
#include "imu.hpp"

#define BUTTON 7

IMU imu;
BLUETOOTH bluetooth;

void processRequest(const String &data);

void setup()
{
    bluetooth.begin(115200); // Sets to Serial the same value

    if (bluetooth.is_connected())
        Serial.println("Bluetooth is initialized!");
    else
        Serial.println("Bluetooth is not initialized!");

    bluetooth.clear_serial();
}

void loop()
{
    buffer = bluetooth.receive();
    processRequest(buffer);
}

void processRequest(const String &data)
{
}

bluetooth.hpp

#pragma once

#include <stdint.h>

class BLUETOOTH 
{
public:
    BLUETOOTH () = default;
    ~BLUETOOTH () = default;

    inline void begin(uint32_t baudrate)
    {
        _baudrate = baudrate;

        _serial.begin(_baudrate);

        while (!_serial)
        {
            // Waiting for the module to start working
        }

        clear_serial();

        _isConnected = true;
    }

    inline String receive()
    {
        return _serial.available() ? _serial.readStringUntil('\n') : String{};
    }

    inline void send(const String &msg)
    {
        _serial.write(msg.c_str(), msg.length());
    }

    inline uint32_t get_baudrate() const noexcept
    {
        return _baudrate;
    }

    constexpr bool is_connected() const noexcept
    {
        return _isConnected;
    }

    inline void clear_serial() noexcept
    {
        if (_serial.available())
        {
            _serial.readString();
        }
    }

private:
    uint32_t _baudrate{};

    bool _isConnected{false};

    static auto& _serial = Serial;
};

That's how I connected MPU to the Arduino.

To whom are you sending this: Serial.println("Bluetooth is initialized!");

You should not put code in a .h file.

We need to see all the code, there is a website for that: https://snippets-r-us.com/

When writing code, you should be more accurate. Did you perhaps use ChatGPT to write the code ?

  1. This Serial sends text to the bluetooth.
  2. Why?
  3. I don't understand why you say me about this site, when I can put my code in the form on the post
  4. No, I don't use ChatGPT, I just cannot push all code that I use that's why I made this mistake, but never mind you see all code that corresponding to the question.

Post ALL the code. In order to provide help, we need to see all of the libraries and port pins you are using, as well as variable declarations. Also, post a complete wiring diagram.

Please read and follow the instructions in the "How to get the best out of this forum" post.

So if the Bluetooth is not connected, then you send "Bluetooth is not initialized!" to the Bluetooth :exploding_head:

The 'C' language allows many things, but it is common practice to not put code in a *.h file. But a class (with its functions) can be in a *.h file, because it is a definition. It is okay, I was wrong.

You still write that the first code is the file "mpu.hpp".
That is "imu.hpp" I assume ?

Which "I2Cdev.h" and "MPU6050_6Axis_MotionApps20.h" files do you use ? There are so many copies of those. If you added a library, which library is it ? I can not find the "i2cdevlib" in the Library Manager of the Arduino IDE.

When the 'dmp' of the MPU-6050 sensor it used, a flag is set in a interrupt and in the loop() the FIFO of the MPU-6050 is read via I2C.
In your code I can not find that a interrupt routine is installed. I don't see code in the loop() that empties the FIFO. Is your "VectorInt16 get_accel()" function supposed to run in a interrupt ? That is not possible, because the Wire library is interrupt driven. You can not use the Wire library from an interrupt routine.

The I2Cdev library has issues and causes trouble.
You could try a simpler method: https://github.com/jremington/MPU-6050-Fusion

Where did you buy your MPU-6050 ? If you bought it at Amazon/Ebay/AliExpress then it is probably fake :scream: It might work, or not.

Make simple experiment as follows and then perform the advanced experiments to meet the thirst of your desire.

1. MPU6050's connection is alright.

2. Connect BT with soft UART Port using 10 = SRX and 11 = STX pins with a level shifter for the RX-pin of BT (Fig-1).
hc05-UNO
Figure-1:

3. Acquire temperature data from MPU6050 and sent it to your Android Phone via BT. At this stage, you may use a Library or write register level codes.

4. If Step-3 works, then write your own Class-based codes to acquire other parameters of MPU6050 and send them to your Phone via BT.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.