Device as I2C master and slave at the same time

[SOLVED] - but may be worth going through all of it, the project went in a different direction...
Hi,
I want to make a control board with some buttons and a 0.96" 128*64 oled display (I2C or SPI) for my arduino projects.

I want to use a Arduino UNO as the master and another device as a slave - the "control board IC". Then I want to the buttons and display to this "control board IC". The "control board IC" would run some code mainly fo an UI on the display - doesn't need to be fast.

Say I'm using just I2C - UNO is the master for the "control board IC", which is the master for the display (+ has the buttons connected to it).

Is it possible in that way over I2C (or another protocol), ideally with an IC like the Attiny85?

Probably would be good to say I'm a complete noob, but willing to learn.

Thanks, Viktor

So you have 2 masters on the bus. If a master also has to act as a slave then start implementing it as a slave. Adding master code is possible all the time, in any place. Consider that the bus is blocked while your "control board IC" communicates with the display.

You must be sure you only have one master active at any given time. The Arduino can be either master or slave but only one at a time. The reason the master generates the clock. If two or more are active the odds of it locking up are quite high as well as the communications will be garbage. If the wires are much more than 12" in length expect noise and intermittent problems.

For the distance - up to 4-5".

And the other thing: If my understanding of I2C is correct, you can have as many slaves as you have unique addresses and multiple slaves. They will handle the data collision on their own (wait 'till no other master is sending). Is that right?

The question here is more like can the same device act as a master and slave at the same time, eg. switch between them quickly? I know i is possible with devices with 2 I2C controllers (Due, Mega), but I'd like something cheaper and smaller.

If it is possible, I would appreciate a tutorial too...

Btw. thanks for your help

Oh, I'm sorry, I completely missed your reply...

That makes sense, but I have no idea how could that be done. Is it so I initiate it as a slave (with a address) and than in some parts of the code use it as a slave and then in other parts just act like it was initiated as a master?

And is it possible that if the UNO requests to send/receive data the function for "control board IC" - display communication is interrupted (not necessarily immediately, but at end of the loop)?

Edit: How do you quote a reply, like you did?

That would work but it gets very convoluted in the coding portion. On the I2C the master sends out an address with a read or write bit. The slave will respond with the correct information but the master clocks the data. There is nothing stopping you from sending a command then reading the response.

You can basically have as many devices as slaves as you want. There are 127 addresses available, some may be reserved. You can expand this with multiple buses and/or selects.

China clones of the Nano can be gotten very cheap and most work OK.

Try searching for this with your browser:"youtube arduino tutorial I2C"

Two Arduino boards is not twice as complex, but four times as complex.
When there is a I2C bus between two Arduino boards, then it is no longer four times as complex but ten times as complex.
A multi-Master I2C bus is so complex that I have not seen a good implementation yet. There is not a single experienced Arduino user who thinks that a multi-Master I2C bus is a good idea.

Can you tell about your project ? Please give us a broader view.
Whatever decision you make, make one that can do your project with a single Arduino board :wink:

Try searching for this with your browser:"youtube arduino tutorial I2C"

I don't think you understand my problem... I know how to setup a simple 1 master I2C bus, the problem is, that I don't want to control the display with the UNO, so that I can click though the interface eve when the UNO is busy...

Sorry, I can't explain it better, than in the first post...
It doesn't have to be multimaster I2C, but any protocol/combination of protocols, that could let me communicate between a Arduino UNO and the "control board IC" (a Attiny?, which could communicate with a I2C/SPI display and some buttons and run some code...

It is very hard to answer your question as there are a lot of possibilities. You can use a given protocol over whatever physical layer you want. Most of my stuff is on CAN, not that expensive but very reliable up to a few thousand feet.

Basicly the master sends a command, and will wait for a response if required for a period of time, if no result it will fault. The slave will respond to that command and notify the master if required. The master will pool the slaves to determine if they have any information available. If my memory is correct there are some examples of this in the Arduino Cookbook.

At this point we are shooting in the dark as we have no idea of what data is being sent, how much, how fast it is needed, etc...

I assumed that you had a project in mind.
A smart display with a I2C bus had many consequences. Both in software and hardware are many things that can go wrong.
My advice: don't do it, use the I2C bus for sensors.

Each I2C device acts as a slave, by default. It becomes a master when a transaction is started in code, and only for the time of that transaction.

If multiple masters want to start a transaction the garbage on SDA is detected. Each master that actually wants to transmit a 1 (HIGH) but senses a 0 (LOW) on SDA knows that another master is also active and aborts his transaction. The aborted masters have to wait until the winning master releases the bus and then try to begin their own transaction once more. The result of endTransmission() or requestFrom() will tell succ/fail.

Select the text you want to quote and click on the appearing "Quote" button.

Thanks a lot, so far you are the most helpful one...
I will try what I think is the solution you ment, but now I don't the time nor the resources for it... I`ll post an update, if it worked or not.
But thanks to all of you

Ok, but that isn't that helpful... What way of communication do you think I should use?

Do you know the Nextion displays ? They use a Serial interface. When a Arduino Uno is used, then there is no spare Serial port, but with SoftwareSerial or the AltSoftSerial library it is possible to combine a Uno with a Nextion display.
The downside is that the SoftwareSerial library takes over the complete Arduino Uno and there is not much else that can be done.
I have a Arduino Uno with AltSoftSerial connected to a Nextion display. It works, but it is just a simple clock.

The best solution would be two Arduino boards that both have a spare Serial port. One as part of the display module and one as the main Arduino board. Search for a "Pro Micro" board.

Can a ATtiny85 get the right baudrate without having a crystal ? I don't know. The I2C bus does not need a accurate crystal for a baudrate :grimacing:

The advantage of Serial with RX and TX is, that it is possible to connect to a computer with a usb-serial module and to a Raspberry Pi or a ESP32. Take care when you connect a Serial port from a 5V board to a Serial port of a 3.3V board.
The Serial RX,TX can be upgraded to RS-485 for a twisted pair cable. The RS-485 is for longer distances and is a very old standard. But it is still a good solution for today for short distances as well.

These look very cool! But I that is not what I have in mind:

  1. I don't need color and a small one is enough, so I'd rather use the a lot cheaper
  2. I want it to be as light-weight in code as possible on the Arduino UNO side. So the SoftSerial is not a option, as you say:

The use case of it would be mainly some testing/debugging, final application would be secondary.
That's why I wanted to have there only a custom library to send data (strings to display) to a slave board, which would do the UI stuff, print it to the display, and send back data from the buttons (on/off, maybe some ints from a rotary encoder).

I knew this board already, but I didn't realize it could be used for this... And I could interface it with I2C to the UNO and SPI to the display, right?
And also I found a "pro mini", which has more ports (14 instead of 12), but no USB port. Are these the only differences?

The "Pro Mini", "Uno", "Nano" have a ATmega328P microcontroller. If you need an extra serial port, then there are software solution, but those are not ideal. The "Pro Mini" is missing the usb-serial chip.

The "Leonardo" and "Pro Micro" have a ATmega32U4 with a spare Serial port. They have a usb-serial device inside the ATmega32U4.

Are you bringing the I2C bus back to communicate between Arduino boards ?

Actually I talk about I2C, because I know only 3 protocols: I2C, SPI, UART - SPI has more wires, whis run of of digital pins on arduino UNO, With UART I couldn't use the serial monitor/it would use up all processing power with SoftSerial, and I2C has just two wires of of analog ports, which I use way less.
If you know any other way to exchange data easily, let me know!
PS: I've noticed you don't like I2C, but I don't really know why...

Because the I2C bus weak, it is not supposed to go through a cable and an Arduino board as I2C Slave has consequences. Projects on this forum fail when there is a I2C between Arduino boards and there are also motors in the project.

You are right, I used the name "Serial" or "RX / TX", but "UART" is the more appropriate name :nerd_face:

The Leonardo and Pro Micro have a spare hardware UART port. Also the Mega, ESP32, MKR boards and others have spare UART ports.

The AltSoftSerial is less demanding. The maximum baudrate depends on the interrupt routines (for example a interrupt from a library) :grimacing:
Could the AltSoftSerial library be combined with interrupts from rotary encoders :thinking: my feeling says that it should work.

When an Arduino board is used as a I2C Slave and the interrupts are turned off for some time, that might cause trouble. The libraries that turn off interrupts are: DHT, Neopixel, OneWire, and others.

If you use the I2C bus between Arduino boards, then the Slave can no longer use a I2C display or I2C sensors, because then there would be two Masters on the I2C bus. As I wrote before, I have not yet seen a good implementation of a Multi-Master I2C bus with the Arduino Wire library.

Conclusion: A Leonardo or Pro Micro with UART communication gives the most freedom in my opinion. If you want a bare-bone solution with a SPI display and buttons to a ATtiny/Uno/Nano/Pro Mini with I2C interface, then go ahead, it will work if you take care of the weak points of the I2C bus.

Ok, so you suggest to use UART between the UNO and a Pro micro and whatever for the display (SPI or I2C). Edit: AltSoftSerial serial for the UNO, hardware for the Pro micro

One more thing what exactly do you mean by spare UART port? I see one pair of RX/TX pins on Pro micro and Pro mini as well - the 0 and 1 pins...