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

I’m testing a OLED 128x64 display on a MEGA2560 and run I2C_Scanner. If I use Wire.begin(20, 21); which is the defalt for the MEGA I get the error “no matching function for call to TwoWire::begin(int, int)”. If I leave addresses out, Wire.begin(); it compiles and runs but the scanner reports a device with address 0x3C although the OLED has an address of 0x78. If I run the scanner on a esp32 WEMOS LOLIN32 board and write Wire.begin(5, 4); it compiles. So, what is the difference.

  1. Are there different versions of the Wire library?
  2. Why does the scanner repord the wrong address?

I2C_Scanner code:

#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);
}

If I use Wire.begin(20, 21); which is the defalt for the MEGA I get the error

Wire.begin() only takes one parameter at most: the 7-bit slave address (optional) or if not specified, join the bus as a master.

Wire.begin(5, 4);

Is valid code for an ESP8266, but not for a Mega2650. Where did the code come from?

The scanner reports the right address. Your OLED is at 0x3C :smiley_cat:
Arduino uses the 7-bit shifted I2C address.
Some manufacturers use also the 7-bit shifted I2C address in their datasheets and some use the 8-bit address (inclusive the r/w bit).

J-M-L, groundFungus

OK, I got it!

Koepel

  1. I read about 7-bit addresses and it adds up, 0x78 minus LSB give 0x3C.
  2. When I run the scanner on the esp32 board with nothing wired up I get 'no devices'. It should display the address of the on-board OLED display but it doesn't. Have I fried the display?

SOLVED.
For the esp32 board I need to specify pin, like Wire.begin(5, 4);, otherwise it doesn't work. On the MEGA2560 I have to leave them out. This leads me to another question:
How do I determine type of board in the code. I've seen some solutions where you test for "#if defined(AVR_ATmega32U4)", It looks like a way but where do I find the different board values, for a esp32 board? Are there fixed values for each type of board and where do I find them?

hcccs:
.
2. When I run the scanner on the esp32 board with nothing wired up I get ‘no devices’. It should display the address of the on-board OLED display but it doesn’t. Have I fried the display?

More likely you’ve got the wrong I2C pins for the board you have.

They seem very . . .mobile on ESP32.

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