Build an Electronic...
 
Notifications
Clear all

Build an Electronic Level with MPU-6050 and Arduino

11 Posts
4 Users
0 Likes
2,318 Views
(@dronebot-workshop)
Workshop Guru Admin
Joined: 5 years ago
Posts: 1073
Topic starter  

Today we will be working with the MPU-6050 IMU, and we will use it to build an electronic level with an Arduino.

Article with libraries and code - https://dbot.ws/mpu6050

Today we will be working with the MPU-6050, a low-cost Inertial Measurement Unit, or IMU. This device has a lot of different applications in many fields including robotics, gaming, quadcopters, and IoT devices.

The MPU-6050 has an internal gyroscope and accelerometer. It's an inexpensive device, available in several packages and modules. The module I am using is very common and has a built-0in voltage regulator so it can work with 5-volt logic.

We will start by looking at the MPU-6050 internals, as well as at the pinout of the module I am using.

Next, we will load a couple of libraries into our Arduino IDE and ruins some sample code to display Pitch, Yaw, and Roll.

After that, we will build a small project, an electronic level. Our level will display on an LCD and will also use 5 LEDs to give a good visual indication of how level the surface is.

Here are the contents for today's video:

00:00 - Introduction
02:53 - MPU-6050 Intro
11:20 - MPU-6050 Arduino Experiments
20:29 - Electronic Level

You can get the sketch and the two libraries used in the experiments on the DroneBot Workshop website at https://dbot.ws/mpu6050 <.

"Never trust a computer you can’t throw out a window." — Steve Wozniak


   
Quote
Joseph Lamoree
(@jlamoree)
Amateur
Joined: 3 years ago
Posts: 1
 

I wanted to see if I could use a MPU-6050 module for a robot arm I'm working on. I don't think anybody has mentioned yet that the New LiquidCrystal library has moved from https://bitbucket.org/fmalpartida/new-liquidcrystal/downloads/ to https://github.com/fmalpartida/New-LiquidCrystal

I installed the version from the GitHub repo, and it works fine (after setting the right I2C address for the board I ended up with). Thank goodness for that i2c_scanner example.

I built the Electronic Level as designed, with the omission of the LEDs. My results were pretty much the same as shown in the video. However, I'm seeing a problem that has persisted for me every time I've used any MPU-6050 board (with or without the LCD hooked up). When left still, the sensor will output slightly varying values indefinitely, but if I move the sensor with even the most modest acceleration, the Arduino locks up. Or at least that's what seems to happen. I didn't put a scope on the I2C bus to watch. The same behavior exists with MPU-6050 boards from two different manufacturers, each of which have slightly different hardware designs.

This is perplexing. Is it possible that the accelerometer and gyroscope hardware gets overwhelmed by too much change per tick? I'm going to assume this part isn't robust enough for my intended use. I bought some AS5147 Rotary Sensor boards to see if that's a better fit for my application -- sensing the position of a shaft between 0 and 90 degrees.

 


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6856
 

@jlamoree I just downloaded the github lib and it doesn't work. Also I see .pde samples so it's very old. POSITIVE isn't declared.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
Mac-in-Memphis
(@mac-in-memphis)
Member
Joined: 2 years ago
Posts: 8
 

Hi All Esp32'ers,

Has anyone modified this "Electronic Level" sketch (& its Interrupt Pin configuration) to work with an ESP32 Dev board? Without attaching the the correct interrupt pin, the MPU6050 seems to hang @ sketch line ~284... 

284 Wire.requestFrom(0x68,14);    
285 //Wait until all the bytes are received 

...and I don't see where the interrupt I/O Pin number for the microcontroller is declarer within the sketch; so that I may change the wiring of Arduino Pin I/O pin 2, to ESP32 Pin (#?)... 

I'd experiment with the ESP i/o's, but I don't wish to brick the board until by backup arrives.

THX! Mac

ref:

Build a Digital Level with MPU-6050 and Arduino | DroneBot Workshop

 

If it is broken, anyway; then just take it completely apart and try to fix it.
you may learn something valuable from the ancient efforts of industrious others. /hms


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6856
 

@mac-in-memphis As far as I know that sketch does not compile. I will download Bill's code and try it but I have helped several people with the LCD portion that has some minor errors. I know one of the MPU6050 sketches is badly messed up but not sure if it is this one.

That said, maybe you are a great debugger so I hope I didn't insult you. I am tied up at the moment but will download all of the code Bill used and see what happens.

I quickly looked at the code and did not see any interrupts being used, it appears to be I2C bus.

Did you compile with All errors, maybe that is needed to point out the other errors.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6856
 

@mac-in-memphis Here is the compile error I get I expect this as the LCD setup is now different and easily fixed. How did you deal with it? The only mention of Interrupt in Bill's blog is to do with the UNO, not the esp32

We are also using the interrupt output from the MPU-6050. This connects to pin 2 on the Arduino Uno, which is interrupt number 0.

Compilation error: 'POSITIVE' was not declared in this scope

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6856
 

@mac-in-memphis The board Bill used is an Arduino UNO, not an esp32. It fails on compile the same way.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
Mac-in-Memphis
(@mac-in-memphis)
Member
Joined: 2 years ago
Posts: 8
 

@zander Thanks Ron, for investigating! 

Apparently the Arduino UNO sketch uses the one interrupt IO pin (2)?... And it's Interrupt Routine might work easily, fetching in the background.

However, the ESP32 has more than a dozen or so pins that each can be programmed to be interrupts; but the coding to define the available pin that one wants to select as the interrupt, becomes a bit more complicated! Then, getting the routine to watch for the correct trigger pulse, is even more complex (at least it is to me).

I'm working thru a simpler example found on another website* that may be helpful in adding the code, and then debug, so stay tuned. Perhaps I can yield back something useful to the Arduino sketch :/ 

I have a lot of other modules loaded on my breadboard, so there is a lot going on. Power drain? crossover cross-talk emf? cross-eyed wiring? 😉  

Man, I always learn something great on Bill's eloquent videos and his superior web site!

Kind Regards, 

Mac

* Does Bill mind if other websites are referenced/posted on the forum?

...I realize that any, such nacho hyperlinks could go stale at any moment...

If it is broken, anyway; then just take it completely apart and try to fix it.
you may learn something valuable from the ancient efforts of industrious others. /hms


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6856
 

@mac-in-memphis The problem is the UNO based sketch does NOT have an ISR routine. This is the first time i have seen this type of use and do not understand how it is working. Perhaps Bill @dronebot-workshop can answer how the UNO pin 2 INTR0 is used since there is NO mention of an ISR in the sketch. 

You didn't say how you got past the compile error.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
Mac-in-Memphis
(@mac-in-memphis)
Member
Joined: 2 years ago
Posts: 8
 

@zander, Well the Arduino's Pin(2) trigger w/no ISR method has me curious, too!

I just 'assumed' that there must be some internal hardware magic that is unique to the UNO board series?

Since my project mainly needed to use of the MPU6050 accelerometer, I chopped a lot of the 'extraneous' LCD code and focused on trying to get the Esp32's fetching ISR to work. Had to mish-mash with other examples in order to try to pull-in the data from the registers,,, It compiled, though it had a lot of red flags.

So far I'm ready to drop back and punt with a fresh ESP & without the overhead of my overloaded breadboard.

Someday, I would like to see Bill's 'Electronic Level" sketch data output, while using the Serial Plotter tool. 😉

Mac

If it is broken, anyway; then just take it completely apart and try to fix it.
you may learn something valuable from the ancient efforts of industrious others. /hms


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6856
 

@mac-in-memphis As you know there is NO interrupt code in Bill's sketch or yours. Pin 2 on the UNO does not mean pin 2 on the esp32. It's interrupt 0 but since it doesn't appear to be used it's irrelevant. I even had a quick look at the wire library code and did not see any normal interrupt code although there is a hint of something. Your code which is a copy of Bill's does NOT use interrupts just look at your main loop, it is simple polling code. Both work, interrupts are used for certain cases but this case is not one of them. You can't just take a UNO sketch and expect it to run on an esp32.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote