5

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)

  • 3
    did you try htu21d.begin(Wire);? Wire is an object, an instance of TwoWire class. it is defined in Wire.h at the end – Juraj Jul 18 '18 at 19:01
  • But how do I tell Wire that I want to use pin 16 as SDA and pin 17 as SCL ? – Gazouu Jul 18 '18 at 19:04
  • 2
    found this void TwoWire::begin(int sdaPin, int sclPin, uint32_t frequency) here github.com/espressif/arduino-esp32/blob/master/libraries/Wire/… – jsotola Jul 18 '18 at 19:08
  • 2
    you call Wire.begin – Juraj Jul 18 '18 at 19:08
3

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

Wire.begin(16, 17);
htu21d.begin(Wire);
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.