NodeMCU I2C Test

Updated: Nov 20, 2021



We need the address of the product we will use for the I2C communication system.


Communication uses two pins.It is called SCL and SDA.

You can search the internet for I2C communication details.


Manufacturers often publish libraries that we can use for their products.


You can use the i2c_test program to see that the I2C connections and the system are OK.


What we need to pay attention to is that the communication pins are different on all devices.You can see this in the examples we will do with Esp32.



For Nodemcu, the SCL pin will be used as D6 -GPIO 14 and the SDA Pin as D5-GPIO 12 will be used in our examples.



Wire library is used for I2C communication.In order for the Wire library to work with the pins we want, we should use it as follows.


Wire.begin(14,12); // d6-14 scl d5-12 sda

#include <Wire.h>
 
 
void setup()
{
  Wire.begin(14,12); // d6-14 scl d5-12 sda
 
  Serial.begin(9600);
  while (!Serial);             // 
  Serial.println("\nI2C Scanner");
}
 
 
void loop()
{
  byte error, address;
  int nDevices;
 
  Serial.println("Scanning...");
 
  nDevices = 0;
  for(address = 1; address < 127; address++ )
  {
    // The i2c_scanner uses the return value of
    // the Write.endTransmisstion to see if
    // a device did acknowledge to the address.
    Wire.beginTransmission(address);
    error = Wire.endTransmission();
 
    if (error == 0)
    {
      Serial.print("I2C device found at address 0x");
      if (address<16)
        Serial.print("0");
      Serial.print(address,HEX);
      Serial.println("  !");
 
      nDevices++;
    }
    else if (error==4)
    {
      Serial.print("Unknown error at address 0x");
      if (address<16)
        Serial.print("0");
      Serial.println(address,HEX);
    }    
  }
  if (nDevices == 0)
    Serial.println("No I2C devices found\n");
  else
    Serial.println("done\n");
 
  delay(5000);           // wait 5 seconds for next scan
}

If there is no problem, you can view the device and its address on the Serial port screen.


More than one product can be communicated at the same time.In case of more than one product, all product addresses will be listed.



i2c_test_nodemcu
.zip
Download ZIP • 622B

35 views0 comments

Recent Posts

See All