[SOLVED] Need working code for I2C on WSL V3

Can someone please post some working Arduino code for an I2C device on WSL V3?
CCS811, BME380, other device.
I tried everything but cannot make it work.

Do I need to activate a 2nd I2C port?

Any help is appreciated.

Maybe just start with a simple I2C port scanner and make sure you’ve got that side of things sorted first.

Set the SDA/SCL definitions in the following to the pins you are using. If the scanner can’t ‘see’ anything with your sensor connected, check your wiring. When you can ‘see’ your sensor, add the relevant code to read the sensor(s).

// ` I2C Scanner

#include <Wire.h>

#define SDA 3
#define SCL 4

void setup()
{
  Wire.begin(SDA,SCL);
 
  Serial.begin(115200);
  delay(500);
  Serial.println("\nI2C Scanner");
  Serial.print("SDA : ");
  Serial.print(SDA);
  Serial.print(", SCL: ");
  Serial.println(SCL);
  Serial.println();
}

void loop()
{
  byte error, address;
  int nDevices;
 
  Serial.println("Scanning...");
 
  nDevices = 0;
  for(address = 1; address < 127; 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
}

Hi UniquePete,

Thank you very much.
I will try the code.
Are these the designated pins for SDA and SCL, or can I use any GPIO pins?

I tried a few Adafruit I2C libraries but none of them seem to work with the ESP32-S3 of the WSL V3.
Are there libraries for OLED display, BME280, HDC1080, CCS811, etc. that you can recommend?

To the best of my knowledge, there are no ‘designated pins’ for the I2C bus on the ESP32. You can generally use any GPIO pins, but conditions apply. It’s best to check the ESP32 documentation for pins that are unencumbered with special functions, like strapping pins, or those designated for input or output only:


My own empirical tests would suggest that GPIO0, 1, 2, 3, 4, 5, 6, 7, 19, 20, 33, 34, 38, 39, 40, 41, 42, 45, 46, 47, 48 all seem to be OK in my development environment, although some of these still have other functions that would warrant avoiding their usage regardless.

If you search this forum for something like “I2C V3” you should find a couple of threads discussing the use of an I2C bus with the V3 (and earlier) boards.

I use the ThingPulse SSD1306 library for OLED displays and the Adafruit_BME280 library, which has other dependencies (the Adafruit_Sensor library at least) for the BME280, although looking quickly through my sketches, I’m not sure that I’ve configured a BME280 with a V3 module yet—I’m usually only interested in the temperature of a Node and for that I use the DS18B20. I’ve not used a CCS811 with a V3 module yet and I’ve never used an HDC1080.

Thnx, I will try those libraries.
I also found this post:


that suggested the Sparkfun_BME280 library.
I had to change the I2C address in the code of that library to 0x76 and changed the I2C pins to 17 and 18, and it worked!