#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));
}
}
By default, NodeMCU uses D1 (SCL) and D2 (SDA) pins for I2C communication.
When you call Wire.begin() without any parameters, it automatically assumes these default pins.
If you want to use different pins for I2C, you can specify them in the Wire.begin(SDA, SCL) function,
where SDA and SCL are the new pins you choose.
For example, if you want to use D2 (SDA) and D5 (SCL), you would write:
Wire.begin(D2, D5);
Here’s a list of recommended I2C pins for NodeMCU:
- SDA: D2, D3, D4, D5
- SCL: D1, D2, D3, D5
NodeMCU
19 Nov. 2024
|
Last Updated: 22 Nov. 2025
|
jaimedcsilva Related