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)
2 Answers 2
I was able to resolve my issue thanks to Juraj comment. I did :
Wire.begin(16, 17);
htu21d.begin(Wire);
-
worked for me too, thanks,
Wire.begin(SDA, SCK);
for ssd1306 library for oled .alireza– alireza2021年06月20日 17:33:23 +00:00Commented Jun 20, 2021 at 17:33 -
Some of the ESP32 pins do not support SPI. You can only use pins 11,12,13,18,23,24,25,26,29 and 30 as SDA or SCK.Georgi Peev– Georgi Peev2022年10月01日 23:55:34 +00:00Commented Oct 1, 2022 at 23:55
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.
htu21d.begin(Wire);
?Wire
is an object, an instance of TwoWire class. it is defined in Wire.h at the endvoid TwoWire::begin(int sdaPin, int sclPin, uint32_t frequency)
here github.com/espressif/arduino-esp32/blob/master/libraries/Wire/…