MPU magic and starting to pull things together.

A project log for ESP32 Drone

I wanted to make a low cost drone with all the bells and whistles, so people working with a small budget can get into the drone game. ~$50

jon-vbJon VB 12/11/2022 at 20:270 Comments

Alright, so I got to looking at the Arduino build code for the motors with PWM and... of course the PID and MPU6050 were all coded in together. What I ended up doing was working on the gyro first.

What I had to do was find a library that would work with the MPU6050 and the ESP-IDF build environment. Luckily, there was such a thing, that's here and I wasn't going to have to start from scratch. Also, it has an awesome little test example which I could use to to do a function check really quick.

I did run in to an issue with the example, nothing ever works perfectly on the first try. If it does that generally tends to worry me as there might be an issue further on down the line. The issue was with their sdkconfig file. They seemed to have some different boot image options enabled for a different board that wasn't going to jive with the current board I am using for this build. So, being the lazy person I am, I just copied a version of the sdkconfig that I had from a different project over to it and that seemed to fix the boot image issue that I was having.

Great Success, yes! got some good initialization messages along with some good telemetry data.

MPU6050 being initialized:

MPU6050 telemetry data:

Good stuff, I proceeded to see if there would be any issues with the gyro working with the camera. Since they both use the I2C interface. Which of course there was... Dug a little bit and with some critical thinking realized both the camera and the gyro both are using different libraries to use the I2C interface. Small brain move on my part I then remembered that the ESP32 has more than one I2C port, I2C_NUM_0 and I2C_NUM_1 so... instead of rewriting or scrambling to find something thing that would work with both of the devices. I opted to just use both of the I2C interface ports and save some time.

Doing this required having to change the SDA and SCL pin numbers for the MPU6050. SCL got assigned to 16 and SDA got assigned to 17. Also, since the library being used for the gyro is in C++ I had to add a header to the example and a little #ifdef __CPLUSPLUS magic so my C could see the functions in the object files that were compiled in C++.

Related header file:

#ifndef EXAMPLE_H
#define EXAMPLE_H

#define PIN_SDA 17
#define PIN_CLK 16

#ifdef __cplusplus
extern "C" {

    extern void task_initI2C(void*);
    extern void task_display(void*);
#ifdef __cplusplus


With that out of the way, it appeared to work correctly. Got a good initialization from the camera and also the same from the gyro along with telemetry data. 

Camera initialization and beginning of Gyro initialization:

Some Telemetry data:

with this I just wanted to function test that both the camera and gyro would work together with out any issues which appears to be the case for now.

For the rest of the day I will probably be working on getting the code for the PID and PWM for the motors working, if there's enough time in the day I will see if I can get the gyro, PID and PWM all working together on the same thread. We'll see if I can get that far.