This example scans the I2C bus and prints the addresses of detected devices to the serial monitor.
By default, NodeMCU uses D1 (SCL) and D2 (SDA) for I2C communication.
When calling Wire.begin() without parameters, these default pins are used.
#include <Wire.h>
void setup() {
Wire.begin();
Serial.begin(115200);
}
void loop() {
scanI2CDevices();
delay(5000);
}
void scanI2CDevices() {
for (byte address = 1; address < 127; ++address) {
Wire.beginTransmission(address);
if (Wire.endTransmission() == 0)
Serial.println("Device found at 0x" + String(address < 16 ? "0" : "") + String(address, HEX));
}
}
If you want to use different pins, specify them in Wire.begin(SDA, SCL)
NodeMCU