TinyWire and SoftwareSerial

Hello all, I am trying to figure out a way to us I2C and Software serial at the same time. I have been able to make two attiny85s talk to each other using TinyWire. Now I'm trying to add software serial to the mix. It gives me and error and says I can't use those two libraries together.

Here is the code:

#include <TinyWire.h>
#include <SoftwareSerial.h>

int masterAddress = 8;
const uint8_t rx = 4;
const uint8_t tx = 5;

SoftwareSerial bluetooth(rx, tx);       // RX, TX pins for the Bluetooth module

void setup() {
  TinyWire.begin();
  bluetooth.begin(9600);
}

void loop() {
  checkBluetooth();
}


void checkBluetooth() {
 char charBuffer[20];
       bluetooth.listen();


  if (bluetooth.available() > 0) {
    int numberOfBytesReceived = bluetooth.readBytesUntil('\n', charBuffer, 19);
    charBuffer[numberOfBytesReceived] = '\0'; // Null-terminate the string
   
     if (strstr(charBuffer, "LED")) {
      TinyWire.beginTransmission(masterAddress);
      TinyWire.send('1');
      TinyWire.endTransmission();
      delay(1000);
     }
  
}
}





I have tried using the SoftwareWire library, but it doesn't seem to work either. If anyone has any advice, it would be greatly appreciated.

Thank you

Show the error.

1 Like

It doesn't give me a text error, but it doesn't compile. I looked it up and it says the two libraries cant be used together.

How do you know this?

Show this.

An ATTiny has extremely limited resources.

Assuming you're using DrAzzy's core and this TinyWire library, the following applies regarding your problem.

Both libraries use the pinchange interrupt PCINT0 and have an interrupt service routine for that; you can only have one interrupt service routine for a given interrupt.

Search results for ISR in the two libraries

  D:\_ArduinoPortable\arduino-1.8.19-attiny\portable\sketchbook\libraries\TinyWire-master\twi.cpp (5 hits)
	Line 667: ISR( USI_START_VECTOR )
	Line 722: } // end ISR( USI_START_VECTOR )
	Line 727: ISR( USI_OVERFLOW_VECTOR )
	Line 817: } // end ISR( USI_OVERFLOW_VECTOR )
	Line 821: ISR( PCINT0_vect )
  D:\_ArduinoPortable\arduino-1.8.19-attiny\portable\sketchbook\libraries\TinyWire-master\twi.h (1 hit)
	Line 12: * The master functionality is done directly (not in an ISR) and in case of a requestFrom blocking. The slave
  D:\_ArduinoPortable\arduino-1.8.19-attiny\portable\packages\ATTinyCore\hardware\avr\1.5.2\libraries\SoftwareSerial\SoftwareSerial.cpp (7 hits)
	Line 226: ISR(PCINT0_vect)
	Line 233: ISR(PCINT1_vect, ISR_ALIASOF(PCINT0_vect));
	Line 237: ISR(PCINT2_vect, ISR_ALIASOF(PCINT0_vect));
	Line 241: ISR(PCINT3_vect, ISR_ALIASOF(PCINT0_vect));
	Line 326:     // ISR vector table) until the first delay. After the delay, there
	Line 342:     // time for ISR cleanup, which makes 115200 baud at 16Mhz work more
	Line 360:     // can be used inside the ISR without costing too much time.

The error that I get

C:\Users\Wim\AppData\Local\Temp\arduino_build_665508\libraries\TinyWire-master\twi.cpp.o (symbol from plugin): In function `USI_TWI_Master_Start()':
(.text+0x0): multiple definition of `__vector_2'
C:\Users\Wim\AppData\Local\Temp\arduino_build_665508\libraries\SoftwareSerial\SoftwareSerial.cpp.o (symbol from plugin):(.text+0x0): first defined here
collect2.exe: error: ld returned 1 exit status

If you're not using the core or library that I mentioned above, please state what you're using.

Why use TinyWire when using ATTinyCore?

1 Like

Are you sue that ATtiny85 contains a Hardware UART Port or does it support Software UART Port? The ATtiny has a USI Interface.

Both UART and I2C use the USI interface to software emulate those functions. As far as I remember they can not be used concurrently.

If both are required I would recommend selecting a processor which hardware supports both like any of the newer series one ATtinys.

@Willem43


Figure-1:

Yes! You are right! In Fig-1, it is clearly indicated that I2C Bus (Pin-5 SDA, Pin7 SCL) is an alternative function of USI Interface (Pin-5 DI, Pin-6 DO, Pin-7 USCK).

It is my understanding that I2C Bus is an hardware entitity based on my experince of doing the following project to display the voltage of the RESET/-pin on the I2CLCD Unit (Fig-2) and to observe at what voltage level the mCU enters into RESET state.


Figure-2:

Sketch:

#include<Wire.h>
#include<LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
#define LED 1    //PB1 = MISO pin onboard LED

void setup()
{
  Wire.begin();
  pinMode(LED, OUTPUT);
  analogReference(DEFAULT); //1.1V Vref

  //-------system reset indication-----
  for (int i = 0; i < 3; i++)
  {
    digitalWrite(LED, HIGH);
    delay(200);
    digitalWrite(LED, LOW);
    delay(200);
  }
  lcd.begin();//or lcd.init();
  lcd.backlight();
  lcd.setCursor(0, 0);  //DPos (0-15), LPos (0-1)
  //----------------------
  Wire.beginTransmission(0x27);
  byte busStatus = Wire.endTransmission();
  if (busStatus != 0)
  {
     	while(true)  //error message as LED blinks to means that LCD is not fond.
	{
	   	    digitalWrite(LED, HIGH);
    		delay(100);
    		digitalWrite(LED, LOW);
    		delay(100);
	}
  }
  lcd.print("I2CLCD is found!");
  delay(2000);
  lcd.clear();
}

void loop()
{
  lcd.setCursor(0, 0);
  digitalWrite(LED, HIGH);   //debug message to see MCU reset
  delay(1000);
  digitalWrite(LED, LOW);
  delay(1000);
  //------------------
  unsigned y = analogRead(A0);
  float testVolt = (5.0 / 1023) * y;
  lcd.print(testVolt, 1);
  lcd.print(" V");
}

In the following ATtiny85-OLED-DS1307 Project (Fig-1), I had to use all the tiny libraries (Tiny4kOLED.h, TinyRTClib.h, TinyWireM.h Library) so that the sketch is accomodated within 6 Kbyte user flash (6 KB user Flash + 2 KB Bootloader = 8 KB total) of the ATtiny85 of the Digispark Dev Board inspite of using ATTinyCore.


Figure-1:

Sketch:

#include <Tiny4kOLED.h>
#include<TinyRTClib.h>
#include <TinyWireM.h>

RTC_DS1307 rtc;  
DateTime nowDT;
uint8_t myHour, myMin, mySec;

void setup()
{
  TinyWireM.begin();
  rtc.begin();
  rtc.adjust(DateTime(__DATE__, __TIME__));
  //rtc.adjust(DateTime(2024, 01, 21, 12, 59, 57));//set date-time manualy:yr,mo,dy,hr,mn,sec
  //------------------------------
  oled.begin();
  oled.clear();
  oled.switchRenderFrame();
  oled.clear();
  oled.switchRenderFrame();
  oled.clear();
  oled.setFont(FONT8X16); //(FONT6X8);
}

void loop()
{
  oled.setCursor(25, 10);
  nowDT = rtc.now();
  //--------------------
  myHour = nowDT.hour();
  if (myHour > 12) myHour -= 12;
  oled.print(myHour);
  oled.print(':');
  //-----------------------
  myMin = nowDT.minute();
  if (myMin < 10)
  {
    oled.print('0');
  }
  oled.print(myMin);
  oled.print(':');
  //------------------------
  mySec = nowDT.second();
  if (mySec < 10)
  {
    oled.print('0');
  }
  oled.println(mySec);
  //---------------
  oled.on();
}

Thanks for the reply.

I have not used any of mentioned Tiny libraries so do not know whether TinyWire is a requirement. For all my uses I found the ATTinyCore Wire to be perfectly functional, hence the question.

What type of file the ATTinyCore is? Is it a Text File which contains the definitions of the symbolic names used in the sketch and the values of the fuse bits and lock bits? Does it contain the codes for the I2C functions, RTClib, and OLED functions?

A post was split to a new topic: Uno and Mega communication

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