PlatformIO Community

How to include Arduino Library in PlatformIO?

Should i unzipped the .zip file, then added both .c and .h file into “include” directory?

1 Like

The preferred way is to NOT download the Arduino library yourself. Instead go to PIO Home / Libraries (in Visual Studio Code) or to https://platformio.org/lib and search for the library. If it is found, add the name of the library to platformio.ini (an example can be seen if you click on the Installation tab in the search result). PlatformIO will then take care of the download, unpacking etc.

As an example: search for ArduinoJson. It’s a registered library. So you can then add it to platformio.ini:

lib_deps =
    ArduinoJson

If the library is not registered, you can go the other route, e.g. for library ABC:

  • Download the library
  • Unzip the library
  • Put it into the lib directory within your project

The project structure should then look like:

include
lib
+-- abc
     +-- examples
     +-- include
     +-- keywords.txt
     +-- library.json
     +-- src
src
test

Or if it is a very minimalistic library:

include
lib
+-- abc
     +-- abc.h
     +-- abc.c
src
test

Should I create file “library.json” myself or it would be created by PlatformIO?

I tried the second way you proposed, the question is: should I assert in platformio.ini

lib_deps =
    hd44780-1.3.0

Untitled

This seems to have gone wrong here. Either lib_deps should be used or the library should be put into the libs directory but not both.

In addition, your error messages look as if you have renamed hd44780.cpp to hd44780.c. Now it’s compiled by the C compiler instead of the C++ compiler, resulting in many error messages.

Delete all files in the lib folder and instead put the following lines into platformio.ini:

lib_deps =
    hd44780

Or if the version number is important:

lib_deps =
    hd44780@1.3.0

If you click Libraries (either in PlatformIO’s QUICK ACCESS or in the left bar on PlatformIO Home), you’ll find a UI to look up libraries and what you need to add platformio.ini (tab Installation). hd44780 is a fully supported and registered library.

1 Like

search for “hd44780” is an excellent suggestion, I searched “LCD1602” before, only two libraries shown.

And, there is another question:
After I installed a library, if I want to adjust some arguments value, for example, the pin number in it, where should I go?

Pin configuration is often solved with an init() method or with a constructor taking pin numbers as parameters. It really depends on the library.

The hd44780 library has a GitHub repository with documentation and examples. As far as I can tell, it not only supports different pins but also different I/O schemes (I2C, serial, parallel etc.).

1 Like

your error messages look as if you have renamed hd44780.cpp to hd44780.c . Now it’s compiled by the C compiler instead of the C++ compiler, resulting in many error messages.

right.

1 Like

Doesn’t work AT ALL.
Doesn’t matter if I copy the whole folder, the files I need, the unzipped files… NOTHING is recognized by the VSCode/PlatformIO.
I just gave up and returned to Arduino IDE.

We can only help you if you provide the full details of your project files and errors.

Hi Max…

I don´t have many details to say. I just did the following:

  1. Installed the PlatformIO pluggin.
  2. Setup my board, folders etc successfully.
  3. Started new project.
  4. Added 2 libs to the project: AutoConnect and HttpClient.
  5. Restarted the VSCode (who knows…)
  6. Wrote on the first lines of my main code, inside the Project’s folder:
    #include <AutoConnect.h>
    #include <HTTPClient.h>

Immediatelly I had the following errors:
Cannot open source file Autoconnect.h
Cannot open source file httpclient.h

And that´s all! NOTHING can clean these errors. If I uninstall and reinstall PlatformIO (I already did like 5 times), create a new project (I already create some 10 or 15 on my trials), also doesn’t work.

And of course, I looked for it in several different forums, some people has the same problem and couldn’t solve it.
I just tried VSCode, because the Arduino IDE keeps creating folders everywhere in my computer and storing libs in all these folders, just to accuse duplicated libs when trying to compile the code.

I’m about to give up and pay someone to write a SIMPLE CODE for Esp32 for me…

But thank you anyway. It’s terrible to be a beginner!
Tamer

In these situations you should first check if the project builds via the “build” button. A red squiggle in VSCode doesn’t mean much, PlatformIO is the build system, not VSCode. It is also possible that some libraries are only downloaded during build, so at least one build has to be done.

If the project builds, the project’s IntelliSense is likely auto-refreshed and the red squiggle goes away. You can also trigger the IntelliSense rebuild anytime by doing a Ctrl+Shift+P → Rebuild IntelliSense.

If the project does not build, the library was not included correctly in the PlatformIO build system. In this case you need to show the full platformio.ini.

I have added it this way:

include
lib
+-- abc
     +-- abc.h
     +-- abc.c
src
test

how to include it into project? I tried:

#include "abc.h"
#include "abc/abc.h"
#include "lib/abc/abc.h"

neither one works.

This is the correct way.

grafik

Make sure to build the project once and rebuild the intellisense (Ctrl+Shift+P → Rebuild IntelliSense).

If you are mixing .c with .cpp code, odn’t forget to add the standard extern "C" declarations.

1 Like