Libraries

Hello,
I'm trying to build a self balancing robot with (Uno, MPU6050, L298N module, DC Motors etc).
Can anyone recommend a project that works / has code including libraries please?
The code attached seems promising but I'm stuck on the libraries. The links in the code arn't all zip files and so ive tried copying them to the library directory. I get get some motor movement but compiler errors as attached.
Should I wipe the Arduino installation and start again?
Thanks for any help

sketch_feb09a.ino (6.53 KB)

error text.txt (10.8 KB)

Arduino Web Editor has all the ~3000 libraries of the Arduino Library Manager index pre-installed. This is very convenient, but can also be a mixed blessing. The reason is that practically any common filename you use in an #include directive will exist in multiple libraries. When this occurs, the Arduino Web Editor does its best to pick the right library, but doesn't always get it right.

That is the cause of the error you encountered. Your sketch #includes the I2Cdev.h file:

#include "I2Cdev.h"

Multiple libraries were found that contain a file of this name:

Multiple libraries were found for "I2Cdev.h"
Used: /home/builder/opt/libraries/latest/seeed_pca9685_0_0_1
Not used: /home/builder/opt/libraries/latest/mpu6050_0_0_2
Not used: /home/builder/opt/libraries/latest/grove_motor_driver_tb6612fng_0_0_1
Not used: /home/builder/opt/libraries/latest/grove_imu_9dof_1_0_0
Not used: /home/builder/opt/libraries/latest/mpu6050_0_0_2
Not used: /home/builder/opt/libraries/latest/grove_motor_driver_tb6612fng_0_0_1
Not used: /home/builder/opt/libraries/latest/grove_imu_9dof_1_0_0
Not used: /home/builder/opt/libraries/latest/mpu6050_0_0_2
Not used: /home/builder/opt/libraries/latest/grove_motor_driver_tb6612fng_0_0_1
Not used: /home/builder/opt/libraries/latest/grove_imu_9dof_1_0_0
Not used: /home/builder/opt/libraries/latest/mpu6050_0_0_2
Not used: /home/builder/opt/libraries/latest/grove_motor_driver_tb6612fng_0_0_1
Not used: /home/builder/opt/libraries/latest/grove_imu_9dof_1_0_0

Your sketch also includes MPU6050_6Axis_MotionApps20.h:

#include "MPU6050_6Axis_MotionApps20.h" //https://github.com/jrowberg/i2cdevlib/tree/master/Arduino/MPU6050

Multiple libraries were found that contain a file of this name:

Multiple libraries were found for "MPU6050_6Axis_MotionApps20.h"
Used: /home/builder/opt/libraries/latest/mpu6050_0_0_2
Not used: /home/builder/opt/libraries/latest/grove_imu_9dof_1_0_0

So you can see that the seeed_pca9685 and the mpu6050 libraries ended up being compiled. This is a problem because both contain the source files of the I2Cdev library, thus the "multiple definition of" errors.

Fortunately, there are ways to influence Arduino Web Editor to pick the right library. You don't even use the PCA9685, so clearly it was wrong to use that library. You do use the MPU6050, so that library is useful. We can see that the mpu6050 library contains I2Cdev.h, so we want to influence Arduino Web Editor to use that library for the #include directive. Previously #included libraries get preference when multiple libraries are found that contain the filename from an #include directive. So the easiest solution is simply to re-order your #include directives from this:

#include "I2Cdev.h"
#include <PID_v1.h> //From https://github.com/br3ttb/Arduino-PID-Library/blob/master/PID_v1.h
#include "MPU6050_6Axis_MotionApps20.h" //https://github.com/jrowberg/i2cdevlib/tree/master/Arduino/MPU6050

to this:

#include "MPU6050_6Axis_MotionApps20.h" //https://github.com/jrowberg/i2cdevlib/tree/master/Arduino/MPU6050
#include "I2Cdev.h"
#include <PID_v1.h> //From https://github.com/br3ttb/Arduino-PID-Library/blob/master/PID_v1.h

Now, I should note that the comments in your sketch indicate it was written to use i2cdevlib/Arduino/MPU6050 at master · jrowberg/i2cdevlib · GitHub, but the mpu6050 library is a different library from that. However, due to the structure of the i2cdevlib, it is challenging to install in Arduino Web Editor and the mpu6050 library is based on the i2cdevlib project's MPU6050 library. So I think just using the mpu6050 library is the best solution. If you end up having problems, I can provide you instructions for installing the i2cdevlib libraries instead.

Hi Pert,
Thanks for your response - its appreciated and Ive learnt a lot from your post.

Ive tried numerous things today and made a little progress and have some questions if you wouldn't mind:

Is it generally best to use the Web editor or the local IDE and specifically in this case isn't using the IDE better as it avoids all the duplicated libraries in the Web editor?

I switched the #include directive as you suggested but still couldn't get it to work on the web editor. So I then ran the sketch on the IDE (with the amended #include order). The program runs to enable me to tinker with the GY5-21 and see some motor movement. However, an overflow error is reported and the program stops. There is still a long list of errors reported at compile time.

As you know, the link in the program text is to I2cdevlib-master. This folder has an arduino subdirectory with MPU6050 directory that contains .h file etc etc. So I copied the folder to the IDE library. Same with I2Cdev. Is this correct?

Thanks

LymeRegis:
Is it generally best to use the Web editor or the local IDE and specifically in this case isn't using the IDE better as it avoids all the duplicated libraries in the Web editor?

Having all those libraries pre-installed can be convenient, because it means you don't need to figure out how to install libraries very often. On the other hand, those libraries are installed whether you like it or not. With the standard Arduino IDE, you have complete control over everything.

There's always a way to get around issues caused by the pre-installed libraries in the Arduino Web Editor. You can easily install the libraries you need with the standard Arduino IDE.

So it really is just a matter of preference. It's nice that we have choices. If you value having a cloud based solution, then the Arduino Web Editor is going to be a good choice. It can also be useful as an alternative for people who have problems getting the Arduino IDE working on their computer (and vice versa). If you don't like the Arduino Web Editor, then you always have the option of the free, open source Arduino IDE.

LymeRegis:
I switched the #include directive as you suggested but still couldn't get it to work on the web editor.

I'm sorry to hear that. It compiled without error for me after switching the order of the #include directives. I'm sure I could help you solve the problem with the Arduino Web Editor if you like, but it sounds like you are giving the Arduino IDE a try now.

LymeRegis:
However, an overflow error is reported and the program stops.

If you post the full and exact text of the error I'll take a look. I don't remember whether I have a MPU6050, so I might be limited in how far I can investigate this, but there are plenty other people here on the forum to provide assistance if I can't.

LymeRegis:
There is still a long list of errors reported at compile time.

Are you sure they aren't warnings? If there are compilation errors, the sketch wouldn't upload to your board.

LymeRegis:
As you know, the link in the program text is to I2cdevlib-master. This folder has an arduino subdirectory with MPU6050 directory that contains .h file etc etc. So I copied the folder to the IDE library. Same with I2Cdev. Is this correct?

I can't say without knowing where you copied it to. Although it will work, you should never install libraries to the libraries subfolder of the Arduino IDE installation folder. The reason is that everything in the Arduino IDE installation folder is lost every time you update the Arduino IDE. You don't want to have to re-install everything every time you update the IDE, and if you save any irreplaceable files to the IDE folder that would be a real disaster. The correct place to install libraries is to the libraries subfolder of the sketchbook folder. You can find the location of the sketchbook folder in the Arduino IDE at File > Preferences > Sketchbook location.

But really, you don't need to even think about any of that. The Arduino IDE has a feature that automatically installs libraries to the correct location. This is how I would recommend installing the i2cdevlib libraries:

  • Download the i2cdevlib project: https://github.com/jrowberg/i2cdevlib/archive/master.zip
  • Unzip the downloaded file.
  • (In the Arduino IDE) Sketch > Include Library > Add .ZIP Library...
  • Select the Arduino/I2Cdev subfolder of the unzipped i2cdevlib-master folder.
  • Click the "Open" button.
  • Wait for the Arduino IDE to indicate the library was successfully installed on the status bar.
  • Repeat the process with the Arduino/MPU6050 folder.

I should mention that the "Install .ZIP Library..." feature's ability to install libraries from folders as well as .zip files was recently lost in the Microsoft Store version of the Arduino IDE, but it still works in all other version of the Arduino IDE (including the standard Windows versions).

Thankyou for your advice and patience.

My version is 1.8.11 and it will not install libraries that arn't .zip files.
Would it help if I installed a version that will, and where would I link to this please?

Eventually I hit on the idea of compiling a new (empty) sketch file with just the headers -

#include "I2Cdev.h"
#include <PID_v1.h> //From Arduino-PID-Library/PID_v1.h at master · br3ttb/Arduino-PID-Library · GitHub
#include "MPU6050_6Axis_MotionApps20.h" //i2cdevlib/Arduino/MPU6050 at master · jrowberg/i2cdevlib · GitHub

Th first two headers compile without comment
But this line

#include "MPU6050_6Axis_MotionApps20.h"

throws up a long list of errors

This file is an h. file in the MPU6050 library.

Could you throw some light on this please.

LymeRegis:
Would it help if I installed a version that will

It's a useful feature, but not essential, so it's up to you.

You could always zip the folder you want to install. Or you can install the library manually:
https://www.arduino.cc/en/guide/libraries#toc5

LymeRegis:
and where would I link to this please?

Since "Install .ZIP Library" isn't working for you with folders, I'm guessing you must be using the Microsoft Store version of the Arduino IDE. If you use the standard version of the Arduino IDE, version 1.8.11, you will be able to install folders. You can get the standard Arduino IDE for Windows by clicking the "Windows Installer, for Windows XP and up" or the "Windows ZIP file for non admin install" links on the software page. Don't click the "Windows app Requires Win 8.1 or 10" link or the "Get" button.:

LymeRegis:
throws up a long list of errors

Please do this:

  • When you encounter an error, you'll see a button on the right side of the orange bar "Copy error messages" in the Arduino IDE (or the icon that looks like two pieces of paper at the top right corner of the black console window in the Arduino Web Editor). Click that button..
  • In a forum reply here, click on the reply field.
  • Click the </> button on the forum toolbar. This will add the forum's code tags markup to your reply.
  • Press "Ctrl + V". This will paste the error between the code tags.
  • Move the cursor outside of the code tags before you add any additional text to your reply.

If the text exceeds the forum's 9000 character limit, save it to a .txt file and post it as an attachment. If you click the "Reply" button here, you will see an "Attachments and other settings" link.

Have reinstalled and now running version 1.8.12

Was compiling without Arduous uno board connected so wasn't seeing the 'copy error messages' window.

The 'copy error messages' only appears if 'compile and upload' is selected. Not sure why ?

Now in the quick reply section but the <> symbol isnt available to allow me to send copy of errors

so back to 'post reply' pasted as suggested between <> below:

  Have reinstalled and now running version 1.8.12

Was compiling without Arduous uno board connected so wasn't seeing the 'copy error messages' window.

The 'copy error messages' only appears if 'compile and upload' is selected. Not sure why ?

Now in the quick reply section but the <> symbol isnt available to allow me to send copy of errors

so pasted below:

Thanks for using code tags! If you want to make the code tags button appear in Quick Reply, follow these instructions:

I think you forgot to post your error messages.

Hi Pert,

After much trial and error I got the balancing robot working. Its a bit of lash-up but I can now tidy it up. See attached (abandoned as the video is too large ).

In searching to solve the problem I discovered that many others have issues with Libraries. In truth this was (and still is) my problem. I sat with my son for several hours to no avail. Then, when I left him to his own devices he downloaded someone else's code ( that was, it turned out, identical). Bingo! It worked. But the reason, not yet fully understood, was the placement of files in suitable directories. We will figure it out.

Can I ask, can you advise any other Library explanation ( other than that on the Arduino site ) that may help. A visual (diagram) would be very helpful please.

Regards
Mike

I don't know of any other explanation of libraries, but I'm happy to help you understand things, so let me know if you have any specific questions.

As you discovered, libraries must be installed with a very specific folder structure in order to be recognized by the Arduino IDE. I could explain this in detail, but fortunately it's not something you need to know. If you use the Arduino IDE's Library Manager (Sketch > Include Library > Manage Libraries) or Sketch > Include Library > Add .ZIP Library, the library will be automatically installed correctly without you needing to worry about it.

Thanks for this.
I'm having fun tuning the PID - the real reason for starting in this. I've got it balancing quite well but not yet fully stand alone with battery only power.
Could you please confirm / comment on my current understanding of some issues please?

Include comment between <> marks indicate that the compiler should search the Arduino library directory only?

Include comment between "" mark's indicate to compiler to search the directory that the oarticular sketch happens to be in. Can include path? If not found, compilerwill search Arduino library directory for the file?

Better to use <> if the library is actually contained in the Arduino library as would be quicker?

There is a maths helper.h file. If I compile without this in correct directory, the I get message 'not for uno'. Why is there no cpp file with this one?

Regards

LymeRegis:
I'm having fun tuning the PID - the real reason for starting in this. I've got it balancing quite well but not yet fully stand alone with battery only power.

Sounds like fun!

LymeRegis:
Include comment between <> marks indicate that the compiler should search the Arduino library directory only?

Essentially correct. There are actually multiple Arduino library directories which will be searched.

LymeRegis:
Can include path?

I don't know what you mean by that.

LymeRegis:
If not found, compilerwill search Arduino library directory for the file?

Correct.

LymeRegis:
Better to use <> if the library is actually contained in the Arduino library as would be quicker?

I assume, yes. Probably the speed difference is not noticeable, but I still think it's best to always use the most specific #include syntax because it makes your intention clear and also makes your code consistent. It bothers me to see professional developers intermixing quotes and angle brackets #include syntax randomly with no rhyme or reason in their code. It makes me question the quality of their work.

LymeRegis:
There is a maths helper.h file. If I compile without this in correct directory, the I get message 'not for uno'.

I'd need more information, including the full and exact text of the message, to explain this.

LymeRegis:
Why is there no cpp file with this one?

It's possible to make "header-only" libraries that consist only of .h files. A good example is the Arduino AVR Boards EEPROM library:

There are certain advantages and disadvantages to a header-only library. Sometimes it wouldn't make sense to have a .cpp file because the library doesn't actually contain any executable code. An example of this would be the pitches.h file used by some of the tone() example sketches:
https://github.com/arduino/Arduino/blob/master/build/shared/examples/02.Digital/toneMelody/pitches.h
This simply defines macros for the frequency of each musical note. Now, it's true that this is part of a sketch and not a library, but if you were writing multiple sketches that use musical notes, you might like to install the pitches.h file as a library so that you can share that single file between all you sketches, rather than having to copy the same file into every sketch. This would also allow you to easily make a correction or enhancement to a single file, rather than having to make the same modification to the copy of the pitches.h file in every one of your sketches.