I am using the Adafruit BME280 library in my sketch. In the example sketches, they have included the Wire, SPI and the Adafruit_Sensor library. However, I see that in the Adafruit_BME280 library header file, these libraries are already included. So I tried commenting out them in my sketch and it still works. So I wonder if there are still cases or any specific reason to include them twice?

Additionally, if a library is included/referenced twice like mentioned above, wouldn't it increase the size of the sketch unnecessarily?

Screenshot of Adafruit_BME280 header file

Example Sketch that comes with the library

#include <Wire.h>
#include <SPI.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>

#define BME_SCK 13
#define BME_MISO 12
#define BME_MOSI 11
#define BME_CS 10

#define SEALEVELPRESSURE_HPA (1013.25)

Adafruit_BME280 bme; // I2C
//Adafruit_BME280 bme(BME_CS); // hardware SPI
//Adafruit_BME280 bme(BME_CS, BME_MOSI, BME_MISO, BME_SCK); // software SPI

unsigned long delayTime;

void setup() {
    while(!Serial);    // time to get serial running
    Serial.println(F("BME280 test"));

    unsigned status;
    // default settings
    status = bme.begin();  
    // You can also pass in a Wire library object like &Wire2
    // status = bme.begin(0x76, &Wire2)
    if (!status) {
        Serial.println("Could not find a valid BME280 sensor, check wiring, address, sensor ID!");
        Serial.print("SensorID was: 0x"); Serial.println(bme.sensorID(),16);
        Serial.print("        ID of 0xFF probably means a bad address, a BMP 180 or BMP 085\n");
        Serial.print("   ID of 0x56-0x58 represents a BMP 280,\n");
        Serial.print("        ID of 0x60 represents a BME 280.\n");
        Serial.print("        ID of 0x61 represents a BME 680.\n");
        while (1) delay(10);
    Serial.println("-- Default Test --");
    delayTime = 1000;


void loop() { 

void printValues() {
    Serial.print("Temperature = ");
    Serial.println(" *C");

    Serial.print("Pressure = ");

    Serial.print(bme.readPressure() / 100.0F);
    Serial.println(" hPa");

    Serial.print("Approx. Altitude = ");
    Serial.println(" m");

    Serial.print("Humidity = ");
    Serial.println(" %");

  • 1
    it is not necessary now. in the past it was necessary to include all used libraries in the main sketch file.
    – Juraj
    Sep 12, 2020 at 9:40
  • 1
    It doesn’t use any extra code space. There is an include guard on any decent library that stops it actually being included twice.
    – Delta_G
    Sep 12, 2020 at 11:58
  • 1
    Actually, the first two lines in the include file (#ifndef __BME280_H__, #define __BME280_H__ .... and the #endif on the last line of the file prevent it from being processed twice, so it doesn't matter how many times your code tries to #include it. All or most include files from libraries work the same way.
    – StarCat
    Sep 12, 2020 at 15:25

1 Answer 1


Historically it was necessary, yes. In times past the IDE would look only in your sketch to find the list of libraries to compile and link. However much progress has been made in recursive library searching, whereby the IDE can now find libraries that are included by other libraries.

I am not sure when that change was implemented, but it was a long time coming and is only recently that it's worked.

So if you are distributing code for other people to use then it's a good idea to still add the libraries in the sketch - just in case they're using an older version of the IDE that can't find the libraries included by other libraries.

  • Thanks for the answer. But what about the memory usage if libraries are added twice?
    – Zaffresky
    Sep 12, 2020 at 10:56
  • I don't understand what you're asking. You mean the memory usage of your computer?
    – Majenko
    Sep 12, 2020 at 11:06
  • I meant size of the sketch and required memory (SRAM and/or Flash) on the MCU if the library is added once in the sketch and once called by another library. Usually when libraries are included the sketch size increases. But I suppose the compiler would be smart enough to see the duplication.
    – Zaffresky
    Sep 12, 2020 at 11:24
  • Oh, I see. The compiler doesn't need to be "smart enough" to "see" the duplication. There is no duplication. Added is added. You can't add the same apple to a bowl twice - no matter how many times you put it in the bowl it's the same apple. A library is a library. It's a single item. Referencing it multiple times in multiple places is still just the one library.
    – Majenko
    Sep 12, 2020 at 11:26
  • 1
    @Zaffresky: "But what about the memory usage if libraries are added twice?" If a global is defined (not just declared) in a .h file and the .h file lacks include-guards, then that global would be multiply-defined. But the .h file doesn't actually pull in the library code and data; the linker does that in order to satisfy references to definitions the library contains. When the library has been added, all program references to anything in it are now satisfied, so the linker never needs to pull it in a second time.
    – JRobert
    Sep 12, 2020 at 16:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.