I use the barometer inside the MPU-6050 GY-86 to implement altitude hold, combining accelerometer and barometer data. However, when reading the barometer data, the program would block execution, waiting for pressure and temperature readings. The issue stemmed from delay() functions in the library, significantly increasing my main loop time from 5ms to nearly 20ms. This led to noticeable drone wobble and control difficulties.

The Solution
To restore performance, I implemented a non-blocking method for retrieving barometer data, reducing my main loop time to a more reasonable 9ms. This was achieved with the help of ChatGPT support and insights from a video tutorial (linked below). The key steps in my approach are:
1. Modifying the MS5611 Library
To streamline the library and save memory, I commented out unused functions and introduced the following:
- uint8_t getConversionTime() { return ct; } // Matteo
- uint32_t readADC(uint8_t reg) { return readRegister24(reg); } // Matteo
- uint16_t getPROM(uint8_t index) { return fc[index]; } // Matteo
- int32_t readPressureMAT(bool compensation, uint32_t D1, uint32_t D2); // Matteo
2. Creating a Non-Blocking Function
I developed a non-blocking barometer update function in the InertialMeasurementUnit class: updateMS5611NonBlocking(). The logic is simple:
- Implemented a state-based approach with steps such as STATE_WAIT_TEMP, STATE_WAIT_PRESSURE, etc.
- Instead of blocking the main loop, I trigger the barometer to process data asynchronously.
- Using millis(), I track elapsed time and retrieve data only when it’s ready.
- Most of the function code was adapted from the original library, but restructured to avoid blocking execution.
3. Updating Barometer Offset Computation
I also optimized the barometer offsets computation using the same non-blocking approach. This allowed me to eliminate unnecessary functions from the library, further reducing memory usage.
Results & Resources
With these changes, the main loop now runs at 9ms, ensuring smoother flight performance. Below, you’ll find additional resources:
- My Code Implementation (simple sketch just for functionality – not full drone code) – download
- Video Tutorial (providing insights on non-blocking sensor readings) – Video link
- MS5611 Fact-Sheet (for reference) – check it
- Library page – check it
Leave a Reply