How to use an ATtiny85 as a "buffer"/combiner for two sensors and the main I2C bus

I in a sense want to combine an analogue output from a sensor and another sensor which is i2c into one sensor using an attiny.

The reason for this is those two sensors are part of a highly specialised module for my project and I would like to take their data and process it and plot it over time until my main bus decides to request the data to store or be used in its own application.

The primary bus is i2c and has a multimaster system that changes masters between two (aux master and the core master) depending on the needs of the primary master as it has many other processes it handles.

So having the attiny become a master does not seem practical because if the primary master and the tiny want to communicate at the same time the primary master may miss its window to communicate with the other device it wants to.

I essentially want the attiny to master only one i2c device and read from an analogue device to receive information from them and then act as its own separate i2c device able to communicate the processed data when requested by the primary bus.

I have come up with two ideas, however im not sure which if either are actually possible or practical.

First i could have the Attiny run as an i2c master on two pins to request the data occasionally from the sensor and then immediately switch to a slave on two separate pins to await a request and in the meantime it processes the data.

A problem i can see is during the master portion the sensor will miss requests by the bus and be inaccessible.

The other solution is using a library I found called tinywire that is a composite master/slave library and will function on the same pins but the problem is that as a master the tiny will control the aux bus blocking its use from other masters and I'm not sure if the library includes functionality for an i2c request data from a composite master/slave.

Are there other solutions I am not seeing?

OP's post:

I had to split it into paragraphs to make it readable.

I would use what appears to be your first option. That is the ATtiny collects the data from the sensors, including an I2C slave. It then, itself, presents itself as a slave and allows itself to be interrogated by a master. Multiple I2C masters can co-exist on a single bus.

Here is the definitive guide to Arduino I2C: Gammon Forum : Electronics : Microprocessors : I2C - Two-Wire Peripheral Interface - for Arduino

1 Like

Thanks for the feedback, I edited my post with your paragraph splits for others to read it easier. Id also like to clarify with you what you are suggesting. You said
"ATtiny collects the data from the sensors, including an I2C slave. It then, itself, presents itself as a slave"
and go on to talk about having multiple masters present on one i2c bus. The idea is to have the i2c bus the attiny uses to talk to the sensor separate from the primary bus the attiny will be a slave on as to not use the entire bus when just reading sensor data and to isolate the sensor from being accessed by any device outside of the tiny.

Were you just saying that the attiny while being a master to the sensor should have no problems being on the other bus while it collects data? Because technically while reading the sensor data in my method it would be removed from the other bus as it reads data as i would need to initialise a new bus on separate pins through a software serial.

As I see it, there is only one I2C bus.
Various nodes on that bus can have the role of master and slave at different times.
As I understand it, one node, an Attiny, will, in its role as a master, collect data from a number of slaves. That same Attiny, when contacted by the master, will give up the data that it has collected.
That should all be possible.

Yes that is possible, however that does not match with what I am saying. The attiny should never be a master collecting from slaves on the primary i2c bus because that would result in the primary I2C bus having to wait for the tiny to finish collecting the data before another master can take control. In that scenario my primary master (a teensy 4.1) for the primary i2c bus may miss its window of opportunity to act as master as the primary master is doing Realtime calculations and processing and need to communicate on that i2c bus when it has spare time or really needs specific data. This is why I would like to use a separate i2c bus only connected between the tiny and the sensor and have the tiny swap i2c busses when its ready to act as a slave.

The Teensy4.1 can support three I2C buses in hardware.
The ATtiny85 has a software implementation of I2C only. No ATtiny has direct hardware support for two I2C buses. If you want a separate MCU to collect data from an I2C peripheral and make it available to another I2C master on demand, then you have to look at software implementation of I2C to create an additional I2C bus, if that matches your performance requirements. There I can't help much.

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