Adafruit DHT22 sensor library problem

Hi all!

I’m new to PIO and I’m moving a project from the arduino IDE over to PIO. I am having problems with the adafruit DHT22 library compiling with my code. This is the error I get when I try to compile:

C:\Users\Randy\Documents\Arduino\libraries\DHT_sensor_library\DHT_U.h:36:29: fatal error: Adafruit_Sensor.h: No such file or directory.

Adafruit_Sensor.h is part of their unified sensor library. I have installed (using library manager) the library and have compiled the example code included with the library. So I’m left wondering why my code can’t find the library.

Can someone please point me in the direction I should look?


What is the code that you’re using? Your code needs to #include <DHT_U.h> at least so that PlatformIO is able to scan the dependency chain correctly.

While it is possible to use all Arduino libraries (you probably have lib_extra_dirs directive in your platformio.ini), you should try and use the “Only PlatformIO” way directly, through PIO’s library management via lib_deps or the graphical library manager.

So for a project that uses the DHT_sensor_library which uses the Adafruit_Sensor library I’d expect

lib_deps = 
   adafruit/DHT sensor library @ ^1.4.1
   adafruit/Adafruit Unified Sensor @ ^1.1.4

in the platformio.ini and no lib_extra_dirs directive. (Found via library searcher and the header names) and as example code one of these.

Hi maxgerhardt, can you explain this a bit more?

My code was using #include <DHT.h>, changing it to #include <DHT_U.h> fixed the errors. I can now build and upload the code to the board. So why does it need to be <DHT_U.h>?

I tried this:

But ran into an issue with this - see next post

I’m also using the TFT_eSPI library as well. This library has a User_Setup.h file that needs to be edited to reflect my hardware. So when I use the PIO library manager, where are the libraries stored at? What’s the directory path to the libraries imported via PIO library manager?


The dependency that is not found is Adafruit_Sensor.h When you include DHT.h, it doesn’t have a dependency on Adafruit_Sensor.h, so PlO doesn’t recognize it. However, the whole DHT_sensor_library is still compiled however and so is the DHT_U.cpp file, which needs, through DHT_U.h, the Adafruit_Sensor.h file, which was however not recognized as a dependency and will thus fail.

As said, you don’t have this issue when using explicit library declarations via lib_deps as written above.

The author has written a wiki page for that which you should follow primarily. However, the total number of options available here are

  • duplicate all set macros from your user setup file in the build_flags section of the platformio.ini
  • modify the .pio\libdeps\<environment name>\TFT_eSPI\User_Setup.h file (however, this is not portable – .pio is an internal build and library folder
  • pull the .pio\libdeps\<environment name>\TFT_eSPI out to the lib/ folder of the project, and modify lib/TFT_eSPI/User_Setup.h then
  • Install TFT_eSPI globally (here, here) an modify C:\Users\<user>\.platformio\lib\TFT_eSPI\...