Is it really necessary to include Wire.h for I2C operations?

I've merged a couple of sample sketches; the first reads info from a RTC module and the second displays it on an OLED.

I've been sifting through the code to remove what wasn't needed - and understand what was. I started by removing several libraries and was somewhat surprised that the sketch continued to compile -> upload -> and run correctly with Wire.h commented out.

I thought that library was needed for I2C communications? (Which both the RTC and OLED most certainly use). Can anyone give me a "heads up" as to why the sketch is still working? Is Wire.h now included implicity with later versions of the IDE or something? (I'm using v1.8.13).

Many thanks,

Cheers,

Colin

From Adafruit_SSD1306.h

#include <Adafruit_GFX.h>
#include <SPI.h>
#include <Wire.h>

the library includes Wire.h
So does the RTC library (in the cpp file).

1 Like

Many thanks. I've been keen to look inside some libraries to figure out "how they tick" - how does one go about doing that to find info like you've listed?

Any user installed libraries will be in your sketchbook\libraries folder. Just navigate to the library that you want and open the .h and .cpp files in a text editor. In general the .h files contain all the variable and function declarations and the .cpp files contain the function definitions (the guts of the functions). Files can be included in either of the .h or .cpp files.

This page from the reference may be of interest.

1 Like

Thanks for that; I've saved the link to look at when my brain isn't so tired.

I had a follow-up qusestion, when I couldn't find the file I was after ... but realised I was looking in the wrong place.

Many thanks again for your help; it's not the first time you've helped me out ... it's very much appreciated!

My pleasure. Any time.

1 Like

Rule of thumb: if it compiles without the include then it’s probably included somewhere else :slight_smile:

Sage words for me to remember. Cheers.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.