8

I'm a begginer with Arduino and this may be a basic question but I'm facing an issue.

I'm using a HTU21D sensor with an ESP32. This sensor use I2C communication. Inside the Arduino library, there is this function to start to collect data:

//Start I2C communication
void HTU21D::begin(TwoWire &wirePort)
{
  _i2cPort = &wirePort; //Grab which port the user wants us to use

  _i2cPort->begin();
}

As you can see, we can set in this function which port we want to use. Perfect. But my probleme is that I don't know how to use the type TwoWire. I checked some documentation but I was still unable to find a solution. I want to use GPIO_16 as SDA and GPIO_17 as SCL. So I tried something like that without result: htu21d.begin(Wire(16, 17));

How to set SDA and SCL pin in TwoWire type ? (in my case GPIO_16 and GPIO_17)

VE7JRO
  • 2,515
  • 19
  • 27
  • 29
Gazouu
  • 273
  • 2
  • 4
  • 10

2 Answers2

7

I was able to resolve my issue thanks to Juraj comment. I did :

Wire.begin(16, 17);
htu21d.begin(Wire);
Gazouu
  • 273
  • 2
  • 4
  • 10
2

Here is the example of how you can change esp8266 i2c pins. I used it for my ESP8266 and bme280, but the same for Arduino and other sensors.

#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>

void setup() {
  Wire.pins(12,2); //new SDA SCL pins (D6 and D4 for esp8266)

  Serial.begin(115200);
  delay(100);

  bme.begin(0x76, &Wire); //here is the address for my bme280 and instance of TwoWire object
} 

For my ESP8266 I tried to use GPIO0 for SDA and GPIO2 for SCL, but no luck. Probably because GPIO0 is a Flash pin. Changing SDA to GPIO12 (or D6 pin) helped me.

Max Gorch
  • 21
  • 1