Sudden Wire.begin() errors

Tried to upload an unchanged sketch that worked fine yesterday. Got this whole host of errors that seem to be related to the Wire.h library. Thought maybe the library got corrupted, so reloaded it and got exactly the same errors.

Arduino: 1.8.16 (Windows 10), Board: "Arduino Nano, ATmega328P"

Z:\01-Arduino projects\MPU-6050 tilt\Tilt_Temp_Tx\Tilt_Temp_Tx.ino: In function 'void setup()':
Tilt_Temp_Tx:225:31: error: no matching function for call to 'TwoWire::begin(int, int)'
  Wire.begin(OLED_SDA, OLED_SCL) ;
                               ^
In file included from C:\Users\biz-1\OneDrive\Documents\Arduino\libraries\Adafruit_BusIO/Adafruit_I2CDevice.h:5:0,

                 from C:\Users\biz-1\OneDrive\Documents\Arduino\libraries\Adafruit_BusIO/Adafruit_BusIO_Register.h:9,

                 from C:\Users\biz-1\OneDrive\Documents\Arduino\libraries\Adafruit_MPU6050-master/Adafruit_MPU6050.h:21,

                 from Z:\01-Arduino projects\MPU-6050 tilt\Tilt_Temp_Tx\Tilt_Temp_Tx.ino:9:

C:\Users\biz-1\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.6\libraries\Wire\src/Wire.h:53:10: note: candidate: void TwoWire::begin()
     void begin();
          ^~~~~
C:\Users\biz-1\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.6\libraries\Wire\src/Wire.h:53:10: note:   candidate expects 0 arguments, 2 provided

C:\Users\biz-1\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.6\libraries\Wire\src/Wire.h:54:10: note: candidate: void TwoWire::begin(uint8_t)
     void begin(uint8_t);
          ^~~~~
C:\Users\biz-1\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.6\libraries\Wire\src/Wire.h:54:10: note:   candidate expects 1 argument, 2 provided

C:\Users\biz-1\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.6\libraries\Wire\src/Wire.h:55:10: note: candidate: void TwoWire::begin(int)
     void begin(int);
          ^~~~~
C:\Users\biz-1\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.6\libraries\Wire\src/Wire.h:55:10: note:   candidate expects 1 argument, 2 provided
Tilt_Temp_Tx:259:31: error: no matching function for call to 'SPIClass::begin(int, int, int, int)'
  SPI.begin(SCK, MISO, MOSI, SS);
                               ^
In file included from C:\Users\biz-1\OneDrive\Documents\Arduino\libraries\Adafruit_BusIO/Adafruit_SPIDevice.h:9:0,
                from C:\Users\biz-1\OneDrive\Documents\Arduino\libraries\Adafruit_BusIO/Adafruit_BusIO_Register.h:10,

                 from C:\Users\biz-1\OneDrive\Documents\Arduino\libraries\Adafruit_MPU6050-master/Adafruit_MPU6050.h:21,

                 from Z:\01-Arduino projects\MPU-6050 tilt\Tilt_Temp_Tx\Tilt_Temp_Tx.ino:9:

C:\Users\biz-1\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.6\libraries\SPI\src/SPI.h:159:15: note: candidate: static void SPIClass::begin()
   static void begin();
               ^~~~~
C:\Users\biz-1\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.6\libraries\SPI\src/SPI.h:159:15: note:   candidate expects 0 arguments, 4 provided
Multiple libraries were found for "Wire.h"

 Used: C:\Users\biz-1\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.6\libraries\Wire
 Not used: C:\Users\biz-1\OneDrive\Documents\Arduino\libraries\Wire-master

Multiple libraries were found for "OneWire.h"
 Used: C:\Users\biz-1\OneDrive\Documents\Arduino\libraries\OneWire
 Not used: C:\Users\biz-1\OneDrive\Documents\Arduino\libraries\OneWireNg

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.

I have no idea what is wrong. The IDE halts and highlights the line containing "Wire.begin(xx), about 20 lines into setup(). Here's all the code:

//
// --------------  "01234567890123456789"
char this_file[] = "Tilt_Temp_Tx" ;
char descp[] =     "ESP32 TTGO LoRa" ;
char ver[] =       "ver 1.1  12/31/22 " ;
char ver2[] =      "Tilt + 2 Temps" ;
char ver3[] =      "8 samples/50 ms loop" ;

#include <Adafruit_MPU6050.h>

//Libraries for LoRa
#include <SPI.h>
#include <LoRa.h>

//Libraries for DS18B20 sensor
#include <OneWire.h>
byte temp1 = 21 ; // needs ~2.2K pullup
byte temp2 = 13 ; // needs ~2.2K pullup
byte temp3 = 33 ; // needs ~2.2K pullup
				  // may need an external pullup
				  // if used for a discrete input
byte door = 23 ;  // discrete switch input, internal pullup
byte disc = 36 ;  // a discrete input, but must have
				  // an external pullup resistor
				  // same for 35, 37, 38, 39, maybe 34

byte button = 17 ; // pushbutton I/O pin

//Libraries for OLED Display
#include <Wire.h> 
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
 
//define the pins used by the LoRa transceiver module
#define SCK 5
#define MISO 19
#define MOSI 27
#define SS 18
#define RST 14
#define DIO0 26  

#define BAND 915E6 //915E6 for North America

//OLED pins
#define OLED_SDA 4
#define OLED_SCL 15 
#define OLED_RST 16
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

Adafruit_MPU6050 mpu;

Adafruit_SSD1306 display(128, 64, &Wire, OLED_RST);
// Adafruit_SSD1306 display = Adafruit_SSD1306(128, 64, &Wire);

const int MPU_addr=0x68;
int16_t AcX,AcY,AcZ,Tmp,GyX,GyY,GyZ;
 
int minVal=265;
int maxVal=402;
 
// float x;
// float y;
float z;

int xsamp[26] ;
int ysamp[26] ;
int xavI ;      // integer angle in hundredths
int yavI ;      // integer angle in hundredths
float xav ;
float yav ;
int xsum ;
int ysum ;
int xmax ;
int xmin ;
int ymax ;
int ymin ;

int Temp_1 ;
int Temp_2 ;
int Temp_3 ;

byte sync_byte ;
byte cmd_byte ;
byte counter ; // loop counter

byte i ;
String LoRaData ;
double LoRa_Tx_time ;
double Tilt_clk ; // max 10 minutes
bool Tilt_flag ;  // true=tilt mode, false=temps mode
double temp_clk ; // sets the trigger for sending temps
double ds_clk ; // allow 1 sec conversion time between starts

 

  display.setRotation(x)
  // x = 0,1,2,3 for 0, 90, 180, 270 degrees of rotation
  
*/ // ########################################################################

//
// =======================================================================
void setup() {
	
	pinMode(button, INPUT_PULLUP) ; // external switch
	pinMode(door, INPUT_PULLUP) ;   // external discrete
	
	Serial.begin(9600);
	// while (!Serial);
	delay(100) ;

	Serial.println() ;
	Serial.println(this_file) ; //file name
	Serial.println(descp) ;     //description
	Serial.println(ver) ;       //version and date
	Serial.println(ver2) ;      //version and date
	Serial.println(ver3) ;      //version and date
	Serial.println() ;
 
//reset OLED display via software
	pinMode(OLED_RST, OUTPUT) ;
	digitalWrite(OLED_RST, LOW) ;
	delay(20) ;
	digitalWrite(OLED_RST, HIGH) ;
  
//initialize OLED--force use of non-standard SDA, SCL
	Wire.begin(OLED_SDA, OLED_SCL) ;
	
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
	if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C, false, false)) {
		Serial.println(F("SSD1306 allocation failed")) ;
		for(;;); // Don't proceed, loop forever
	}
 
	display.display() ;
	display.setTextSize(1) ;
	display.setTextColor(WHITE) ;
	display.setRotation(0) ;

// display the file information
	display.clearDisplay() ;
	display.setCursor(0,0) ;
	display.print(this_file) ;
	display.setCursor(0,15) ;
	display.print(descp) ;
	display.setCursor(0,30) ;
	display.print(ver) ;
	display.setCursor(0,40) ; 
	display.print(ver2) ;  
	display.display() ;

	delay(3000) ; // reading time  

	if (!mpu.begin()) {
		Serial.println("Sensor init failed");
		while (1)yield() ; // halt here
	}
	Serial.println("Found a MPU-6050 sensor");

	//SPI LoRa pins
	SPI.begin(SCK, MISO, MOSI, SS);
	//setup LoRa transceiver module
	LoRa.setPins(SS, RST, DIO0);

	if (!LoRa.begin(BAND)) {
		Serial.println("Starting LoRa failed!");
		while (1);
	}

	display.clearDisplay();
	Serial.println("LoRa Initializing OK!");
	display.setCursor(0,10);
	display.print("LoRa Initializing OK!");
	display.display();
	delay(2000);
  
	Tilt_flag = false ;
	LoRa.setSpreadingFactor(12); // temp mode

	display.setTextSize(3);
	LoRa_Tx_time = millis() ; // start the clock
	temp_clk = millis() ; // start the clock (~5 sec)
	
	readtemps() ; // start the first conversion
	ds_clk = millis() ; // start the 1 sec conversion time
  
}

// ============================ LOOP() ====================================
void loop() {
int packetSize ;

	sync_byte = 0 ;
	cmd_byte = 0 ;
	if(get_key()) change_mode() ; // change mode?
	if(Tilt_flag == false) {      // temp mode
		Temp() ; // read and send temperatures/door status
		//try to parse packet, look for mode switch command
		packetSize = LoRa.parsePacket();
		if (packetSize) { // packet received
		Serial.print("Command packetSize = ") ;
		Serial.println(packetSize) ;
			if (packetSize == 2) { //
				//read packet
				sync_byte = LoRa.read();  // sync byte
				cmd_byte = LoRa.read() ;  // command byte
				Serial.print("Sync byte = ") ;
				Serial.println(sync_byte, HEX) ;
				Serial.print("Cmd byte = ") ;
				Serial.println(cmd_byte, HEX) ;
				if((sync_byte == 0xAB) && (cmd_byte == 0x05)) {
					change_mode() ; // go to tilt mode
				}		
			} 
		}
	} else { // tilt mode
	// you can only leave the tilt mode via pushbutton or
	// when the clock runs out.
		// auto revert to temp mode, 10 minutes
		if(((millis() - Tilt_clk) < 600000)) { 
			Tilt() ;
		} else {
			LoRa.parsePacket(); // dump leftover packets
			change_mode() ; // go to temp mode
		}
	}
	
} // ========================= end of loop =================================

// --------------------------------------------------------------------------

bool get_key() { // button = I/O pin 17

bool key_flag = false ;
// most keypad functions only activate when the key is released.
// this routine activates once the debounce time has been met.
// the reason is that there are LoRa transmit times that can
// be longer than 1 second, so the pushbutton needs to be held
// down until the activation is apparent. The debounce hopefully
// will prevent a re-dection upon release of the button.
	// debounce 40 ms
	if(!digitalRead(button)) { // button down?
		delay(20) ;  // start a clock
		if(!digitalRead(button)) {
			delay(20) ;  // start another clock
			if(!digitalRead(button)) {
				key_flag = true ;
				Serial.println("Button push");
			}
		}
	}
	return key_flag ;
	
} // ---------------- end of get_key() ----------------------------

void change_mode() {
	
	Serial.println("Change Mode");
	
//setup LoRa transceiver module
	LoRa.setPins(SS, RST, DIO0);
	if(!LoRa.begin(BAND)) {
		Serial.println("Starting LoRa failed!");
		while (1); // halt here
	}
	if(Tilt_flag) { // tilt mode
	  LoRa.setSpreadingFactor(12); // temp mode	
	  Tilt_flag = false ;
	  Serial.println("Starting LoRa SF12 OK");
	} else {
	  LoRa.setSpreadingFactor(9); // tilt mode
	  Tilt_flag = true ;
	  Tilt_clk = millis() ; // auto revert to temp mode
	  Serial.println("Starting LoRa SF-9 OK");
	}
	
// display the mode
	display.clearDisplay();
	display.setTextSize(3);
	display.setCursor(0,15);
	if(Tilt_flag) {
		display.print("TILT") ;
	} else {
		display.print("Temp") ;
	}
	display.display(); 
	delay(5000) ; // reading time
	
} // ---------------- end of change_mode() -------------------

// --------------------------- Tilt() ---------------------------
void Tilt() { // get the tilt x,y (left/right, up/down) angle data 
  
    Wire.beginTransmission(MPU_addr);
	Wire.write(0x3B);
	Wire.endTransmission(false);
	Wire.requestFrom(MPU_addr,14,true);
	AcX=Wire.read()<<8|Wire.read();
	AcY=Wire.read()<<8|Wire.read();
	AcZ=Wire.read()<<8|Wire.read();
	int xAng = map(AcX,minVal,maxVal,-90,90);
	int yAng = map(AcY,minVal,maxVal,-90,90);
	int zAng = map(AcZ,minVal,maxVal,-90,90);

int	x = 5729.6 * (atan2(-yAng, -zAng)+PI);
int	y = 5730 * (atan2(-xAng, -zAng)+PI);	 
//	x = RAD_TO_DEG * (atan2(-yAng, -zAng)+PI);
//	y = RAD_TO_DEG * (atan2(-xAng, -zAng)+PI);
	// z= RAD_TO_DEG * (atan2(-yAng, -xAng)+PI);
	 
//	Serial.print("X= ");
	if(x > 27000) x = x - 36000 ; // hundredths
	rollx(x) ;
		
//	Serial.print("Y= ");
	if(y > 27000) y = y - 36000 ; // hundredths
	rolly(y) ;
	
// Display packet information
  display.clearDisplay();
  display.setCursor(0,4);
  if(!(yav < 0)) {
		display.print("Up ");
  } else {
		display.print("Dn ") ;
		yav = -yav ;
  } 
  if((abs(yav)<10)) display.print(" ");
  display.print(yav, 1);
  display.setCursor(0,40);
  if(!(xav < 0)) {
		display.print("R  ");
  } else {
		display.print("L  ") ;
		xav = -xav ;
  }
  if((abs(xav)<10)) display.print(" ");
  display.print(xav, 1);
  display.display();

  if(LoRa.beginPacket() == 1) {
//  Loop time w/o Tx is 27 ms
//	Serial.print("MS Margin = ") ;
//	Serial.println(200-(millis()-LoRa_Tx_time)) ;
	  if(((millis() - LoRa_Tx_time) > 200)) {
		// Tx ~5 per sec, ~85 ms quiet time
		// Tx time is 31 ms at SF7 (default)
	    // Tx time is 104 ms at SF9
		LoRa_Tx_time = millis() ;   // restart Tx clock
		LoRa.write(lowByte(xavI)) ; // byte
		LoRa.write(highByte(xavI)); // byte
		LoRa.write(lowByte(yavI)) ; // byte
		LoRa.write(highByte(yavI)); // byte
		LoRa.endPacket() ; // /Tx the data
//		Serial.print("On air time = ") ;
//		Serial.println(millis()-LoRa_Tx_time) ;
//		Serial.println(" ----------------------- ") ;
	  }
  }
} // --------------------------- end of Tilt() ---------------------------

void rollx(int a) {
	for(i=16;i>0; i --) { // i max is 26
		xsamp[i] = xsamp[i - 1] ;
	}
	xsamp[0] = a ;
	xsum = 0 ;
//	xmax = 0 ;
//	xmin = 45 ;
	for(i=0;i<8; i ++) {
//		if(abs(xsamp[i]) > xmax) xmax = abs(xsamp[i]) ;
//		if(abs(xsamp[i]) < xmin) xmin = abs(xsamp[i]) ;
		xsum = xsum + xsamp[i] ;
	}
	xavI = xsum / 8 ; // divide by 8
	xav = xavI / 100.0 ;
}

void rolly(int a) {
	for(i=16;i>0; i --) {  // i max is 26
		ysamp[i] = ysamp[i - 1] ;
	}
	ysamp[0] = a ;
	ysum = 0 ;
//	ymax = 0 ;
//	ymin = 45 ;
	for(i=0;i<8;i ++) {
//		if(abs(ysamp[i]) > ymax) ymax = abs(ysamp[i]) ;
//		if(abs(ysamp[i]) < ymin) ymin = abs(ysamp[i]) ;
		ysum = ysum + ysamp[i] ;
	}
	yavI = ysum / 8 ; // divide by 8
	yav = yavI / 100.0 ;
}

// -------------------------- Temp() -------------------------------
void Temp() { // read and send two temperatures
	
	// time for DS18B20 conversion
	if((millis() - ds_clk) > 1000) { // allow 1 second
		readtemps() ;
		ds_clk = millis() ;
	}
	if((millis() - temp_clk) > 10000) { // send temps ~10 secs
		temp_clk = millis() ; // reset the clock
		Serial.print("Sending packet: ");
		Serial.print(counter);
		Serial.print(":  ");
		Serial.print(Temp_1 / 100.0) ;
		Serial.print("  ");
		Serial.println(Temp_2 / 100.0) ;
	//	Serial.println(Temp_3 / 100.0) ;
		
	//Send LoRa packet to receiver/tow vehicle
		LoRa.beginPacket();
		LoRa.write(0x69) ; // user sync byte
		LoRa.write(lowByte(counter)) ;
		LoRa.write(lowByte(Temp_1));
		LoRa.write(highByte(Temp_1));
		LoRa.write(lowByte(Temp_2));
		LoRa.write(highByte(Temp_2));
		LoRa.write(digitalRead(door)) ; // Rx code fixed
// you could also read the gyro to see if the Airstream is
// moving (filtered or wind motion, etc.) and do an alert based 
// on that, rather than the door.
		LoRa.endPacket();

		display.clearDisplay();
		display.setTextSize(1);
		display.setCursor(0,0);
		display.print("SENDING: ");
		display.print(counter) ;
		if(digitalRead(door)) {
			display.print(" UP") ;
		} else {
			display.print(" DN") ;
		}
		display.setTextSize(3);
		display.setCursor(0,15);
		display.print(" ") ;
		if(!(Temp_1<0)) {
			if(Temp_1<10000) display.print(" ") ;
			if(Temp_1<1000) display.print(" ") ;
		} else {
			if(abs(Temp_1)<1000) display.print(" ") ;
		}
		display.print(Temp_1/100.0, 1);   
		display.setCursor(0,40);
		display.print(" ") ;
		if(!(Temp_2<0)) {
			if(Temp_2<10000) display.print(" ") ;
			if(Temp_2<1000) display.print(" ") ;
		} else {
			if(abs(Temp_2)<1000) display.print(" ") ;
		}
		display.print(Temp_2/100.0, 1); 
		display.display();

		counter++;
		if(counter > 99) counter = 0 ;
	}
	
} // -------------------- end of temp() ------------------------------

// ------------------ readtemps() -------------------------------------
void readtemps() {
	
// one sensor per I/O line, simple OneWire call
	Temp_1 = dallas(temp1) ; 
	Temp_2 = dallas(temp2) ; 
//	Temp_3 = dallas(21) ; 

} // ----------------- end of readtemps() -------------------

// --------------------- quick dallas() -------------------------------
// DS18B20 sensor chip driver code
// the standard library takes several seconds, this takes usecs.
// the logic is (1) read the chip (2) start the conversion.
// a one-second delay between calls is required to allow the
// conversion process to complete so that the next read is good.
// limited to only one chip per I/O pin

int dallas(byte x) {
	
OneWire ds(x) ;
int data0 ;
int data1 ;
int result ;

	ds.reset() ;
	ds.write(0xCC) ;  // skip command
	ds.write(0xBE) ;  // Read 1st 2 bytes of Scratchpad
	data0 = ds.read() ;
	data1 = ds.read() ;
	result = (data1 << 8) | data0 ;
//	Serial.print("===== ") ;
//	Serial.print(result, HEX) ;
//	Serial.println(" ======") ;
	if(result > 4095) result = (result - 0x0ffff) -1 ;
	result = (((result * 90)+0x04) >> 3 ) + 3200 ;  // F x 100
	ds.reset() ;
	ds.write(0xCC) ;   // skip command
	ds.write(0x44,1) ; // start conversion
	return result ;
	
} // ----------------- end of dallas() -------------------

I tried this with a similar sketch that also loaded fine yesterday and it generates the same errors.

The standard Wire library for a Nano does not support a .begin() call that allows you to specify arbitrary pins for SCL and SCK. There are other libraries out there that implement the Wire library using arbitrary pins and software rather than the build in hardware (e.g. SoftWire or something like that).

When you compiled previously, was it for a different target processor?

1 Like

OMG I am LOL here. I had uploaded that sketch so many times in the last three days that I forgot that I had done some technical writing and had looked at the Tools dropdown for another reason. I inadvertently changed the processor and never thought about it. It is amazing to me how sneaky stupidity can creep in. :grimacing: Duh

Thanks loads.

It happens - whenever I suddenly get unexpected errors I check the board. Amazing the errors you can get when switching between ESP and AVR based boards when using incompatible features.

1 Like

The Wire library must have been updated because it works for the ESP32 (when you are smart enough to select the correct processor). I haven't loaded anything other than the standard Wire library. Maybe there is some code in the Adafruit sensor libraries that is covering for me.

The Wire library is different between the ESP32 and the AVR processors. The compiler points to different include paths based on platform and many libraries have lots of conditional #if statements to include/exclude features based on platform. For the Wire library, if you have an AVR processor, I see

C:\Users\brianh\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.5\libraries\Wire

for ESP, it is here

C:\Users\brianh\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\3.0.2\libraries\Wire

and the files have different features

1 Like

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