How to change the default pin number in wire.h library

My work is I try to create a function for customize the pin number in wire.h library..
is it possible to do that.

Welcome to the forum.

There are many Arduino boards and compatible Arduino boards. Some board can set the I2C bus on other pins. If you have soldered something and have the wrong I2C pins, then there are options, such as a software I2C library.

Can you give (a lot) more information ?
If you are making a project, you could show the sketch, a schematic (a drawing with a pen is okay), photos, and so on.

What board are you using?

If the actual Arduino you are using, and you did not tell us, does support re-direction of I2C pins, then the version of wire.h for that Arduino probably already supports pin re-direction.

Do not assume all Arduinos support I2C pin re-direction.

I'm using ESP8266

I'm using ESP8266. In the reason why I need to change the default pin number is.. In my board already occupies the pin so.

On the 8266 you don't need any function for doing it The ESP2866 doesn’t actually have any hardware I2C pins.

See discussion on the link:

1 Like

SDA and SCL pins..

yes yes, SDA and SCL
Do you read the topic at the link above?

These are the two pins that make up the I2C bus, that the wire.h library uses to communicate between Arduino and peripheral you want to use.

Wire.h
TwoWire::TwoWire() { }

// Public Methods //////////////////////////////////////////////////////////////

void TwoWire::begin(int sda, int scl)
{
default_sda_pin = sda;
default_scl_pin = scl;
twi_init(sda, scl);
flush();
}

void TwoWire::begin(int sda, int scl, uint8_t address)
{
default_sda_pin = sda;
default_scl_pin = scl;
twi_setAddress(address);
twi_init(sda, scl);
twi_attachSlaveTxEvent(onRequestService);
twi_attachSlaveRxEvent(onReceiveService);
flush();
}

void TwoWire::pins(int sda, int scl)
{
default_sda_pin = sda;
default_scl_pin = scl;
}

void TwoWire::begin(void)
{
begin(default_sda_pin, default_scl_pin);
}

pin_arduino.h
static const uint8_t SDA = PIN_WIRE_SDA;
static const uint8_t SCL = PIN_WIRE_SCL;

#ifndef LED_BUILTIN
#define LED_BUILTIN 2
#endif
#define LED_BUILTIN_AUX 16

static const uint8_t D0 = 16;
static const uint8_t D1 = 5;
static const uint8_t D2 = 4;
static const uint8_t D3 = 0;
static const uint8_t D4 = 2;

Here my question is how to change and modify the wire.h library to add my function for customize the pin numbers according to our work case..

You don't need to change the library. You can see there that there's a begin method which you just call with the pin numbers you want to use, as also explained in that link in post#7:

Calling Wire.begin() will assume pins 2 and 14 are SDA and SCL, but you can manually set them to any other pin by calling Wire.begin([SDA], [SCL]).

1 Like

@pgraina , show the example of your code, in which you want to use Wire with non-standard pins. I will show you right in the code what needs to be changed

My thought is we want to change the default pin numbers ..In there is no method for choosing pin numbers I think so.

Is my work extern the wire library file and add the function changeDefaultpin() to wire library. I haven't clue if anyone know kindly give your idea and thoughts.

Some... contradictory statements, don't you think?

Do you read the link in post#7? It contains complete information on how to do what you want. If you don't like this method, please explain why.

It is difficult to help you if you do not read the answers and do not explain what, in fact, the problem is.

will it work or not.

just try it

adafruit_ads1x15.h
void setPins(int sda, int scl);
void setPins();

adafruit_ads1x15.cpp
void setPins(int sda, int scl) {
TwoWire::begin(sda, scl){
default_sda_pin = sda;
default_scl_pin = scl;
}
I'm here extend the adafruit_ads1x15 library to create the function for set the sda and scl pins. This is code is not working even I don't know whether this is correct approach or not.

@pgraina,

Your other topic on the same subject deleted.

Please do not duplicate your questions as doing so wastes the time and effort of the volunteers trying to help you as they are then answering the same thing in different places.

Please create one topic only for your question and choose the forum category carefully. If you have multiple questions about the same project then please ask your questions in the one topic as the answers to one question provide useful context for the others, and also you won’t have to keep explaining your project repeatedly.

Repeated duplicate posting could result in a temporary or permanent ban from the forum.

Could you take a few moments to Learn How To Use The Forum

It will help you get the best out of the forum in the future.

Thank you.

I would not modify the adafruit library (or any 3rd party library that uses the Wire library) to set the i2c pins as it is not needed.
The esp8266 core Wire library already has an API function to set the pins used.
If you call the Wire.begin(sda_pin, scl_pin) function, the pins you pass in will be remembered. So if another library you use calls Wire.begin() then the pins you passed in will still be used.
Just make sure to call Wire.begin(sda_pin, scl_pin) before you call the begin function of any library that will be using the Wire library Wire object.

--- bill

1 Like