No matching function for call to 'TwoWire::begin(int, int)'

Hi everyone.
The code from: no matching function for call to TwoWire::begin(int, int) got ERROR, WHY?

 #include <Wire.h>
int pinSDA = 5;               // esp32
int pinSCL = 4;
// int pinSDA = 20;           //ATMega2560
// int pinSCL = 21;

void setup() 
{
  Wire.begin(5, 4);
  Serial.begin(9600);
  while (!Serial);
  Serial.println("\nI2C Scanner");
}

void loop() {
  byte error, address;
  int nDevices;
  Serial.println("Scanning...");
  nDevices = 0;
  //  for(address = 1; address < 127; address++ ) {
  for ( int address = 0x00; address < 0x80; 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.println(address, HEX);
      nDevices++;
    }
    else if (error == 4)
    {
      Serial.print("Unknow 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);
}

ERROR:

Arduino: 1.8.19 (Windows 7), Board: "Arduino Uno"

C:\Users\HUA.DELLV-PC\Documents\Arduino\I2C_Scanner_M\I2C_Scanner_M.ino: In function 'void setup()':

I2C_Scanner_M:13:18: error: no matching function for call to 'TwoWire::begin(int, int)'

   Wire.begin(5, 4);

                  ^

In file included from C:\Users\HUA.DELLV-PC\Documents\Arduino\I2C_Scanner_M\I2C_Scanner_M.ino:5:0:

C:\Users\HUA.DELLV-PC\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.5\libraries\Wire\src/Wire.h:53:10: note: candidate: void TwoWire::begin()

     void begin();

          ^~~~~

C:\Users\HUA.DELLV-PC\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.5\libraries\Wire\src/Wire.h:53:10: note:   candidate expects 0 arguments, 2 provided

C:\Users\HUA.DELLV-PC\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.5\libraries\Wire\src/Wire.h:54:10: note: candidate: void TwoWire::begin(uint8_t)

     void begin(uint8_t);

          ^~~~~

C:\Users\HUA.DELLV-PC\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.5\libraries\Wire\src/Wire.h:54:10: note:   candidate expects 1 argument, 2 provided

C:\Users\HUA.DELLV-PC\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.5\libraries\Wire\src/Wire.h:55:10: note: candidate: void TwoWire::begin(int)

     void begin(int);

          ^~~~~

C:\Users\HUA.DELLV-PC\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.5\libraries\Wire\src/Wire.h:55:10: note:   candidate expects 1 argument, 2 provided

exit status 1

no matching function for call to 'TwoWire::begin(int, int)'



This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

Hi,
compiled here and did not give any error.
What version of your IDE?

1 Like

Thanks.
sorry, I just correct the link.

Hi,
Did you delete the code I tested?

1 Like

Yes, it was wrong.

Hi,
this line has no parameters:
Wire.begin(5, 4);

it should be like this
Wire.begin();

You already defined the pins earlier.

int pinSDA = 5;
int pinSCL = 4;

1 Like

Thanks.
removed the parameters got compiling passed, I wonder many of the sketch used Wire.h don't have pin define:

int pinSDA = 5;
int pinSCL = 4;

if I just defined here as this without any relative to Wire.begin setting, can the Wire searching the pin define itself? even not a standard pin name here.

Hi,

on UNO/nano/mini/Mega, the I2C has its fixed pins.

UNO/Nano/mini A4 (SDA), A5 (SCL)
Mega 20 (SDA), 21 (SCL)

See: Wire - Arduino Reference

2 Likes

Thanks.
I am using ESP32S3 which seems doesn't have default I2C pins, with DS3231 hooked to 21 and 43, and can't get readings stuck here.

The Wire library knows which pins are the I2C pins. And you can't use other pins with it. If you need I2C on other pins you can implement I2C in software using bit-banging.

For boards with multiple hardware I2C implementations, the story might be slightly different. I'm not familiar with them but I can imagine that there is e.g. a Wire1.begin() (similar to Serial and Serial1 for boards with multiple UARTs).

1 Like

Thanks.
yes, for default pins, we don't define.
The board I used is TTGO S3, which I didn't see default SDA pin as picture attached. and I searched that Rui used this way: Wire.begin(I2C_SDA, I2C_SCL);

The Wire library for AVR processors (e.g. the Uno) is different from the Wire library for the ESP32. The library that is used for Uno does not take parameters as there is only one hardware I2C interface.

1 Like

Great!
Thank you.
I've tested a few libraries, none of them works for ESP32S3, any clue please.

I think you should be using the version of the wire library bundled with the board package. It should be called by the #include <Wire.h>

If you turn on verbose output for compilation what library do you see being used.

It should show the sketch library from the boards package. This is what I saw when compiling the WireScan library program for the ESP32S3 Dev Module

Using library Wire at version 2.0.0 in folder: C:\Users\yourName\AppData\Local\Arduino15\packages\esp32\hardware\esp32\2.0.6\libraries\Wire

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.