Hi, I have more atmega328p as slave in I2C protocol connected to Arduino as master. Now I need use more sensors via I2C connected to my atmega328p, but I2C is occupied.
Idea:
Is possible create I2C bridge to for example SPI?
For example via some ATtiny (ATtiny20/40 haas one SPI and one I2C)
or some existing chip?
I need something small, not module, not expensive, onthe pcb.
But I am only beginer, and I dont know how to solve it. Cansomebody help me?
@ UKHeliBob, @ PaulRB
atmega328p now comunicate as slave with Arduino as master, I not have free I2C, so can not connect I2C sensors to atmega328p.
I need alternative solution:
get data from sensor via I2C to chip, or MCU, and send to atmega328p via SPI.
Just trying to clarify your setup: you want a 328P to be an I2C master to several I2C sensors. At the same time you also want the 328P to be an I2C slave device to another Arduino (which one?).
Can't the Arduino do all the work and avoid the separate 328P?
If everything stays on the same pcb board (without long wires to sensors), then there is no problem to connect about 20 devices to the I2C bus. Take care of the voltage level and (internal) pullup resistors and keep SDA away from SCL and about 30 devices should be possible. If you keep the pcb board small and stack the I2C devices, then even more is possible.
In theory, the I2C bus can deal with almost 127 devices.
Yes,
328P is slave for Arduino, and also master for sensors,
I have in 328P more logic, and I have more 328P,
And no, Arduino can not do it, it is only master and ask data from slave (328P)
I have 25 of 328P (slaves), there are some logic for buttons,..., and now I need add sensors there
And I have one arduino as master, and Arduino switch between 328P (slaves), and ask data.
So I need connect sensors for each 328P (slaves), and IC is used for comunication with master.
Do you have a schematic or can you make a drawing on a piece of paper and make a photo of it ?
I assume that there is not a real hardware multiplexer to switch between the Slaves ?
Each Slave could use a software I2C library to talk to sensors. You might have to change the source of the library for that sensor.
Each Slave can also use SPI, but there is no standard "bridge" software to connect the SPI bus to the I2C bus, you have to do that yourself for every sensor.
Adafruit has BusIO library to be able to use a single interface for many different sensors (SPI and I2C). It might be of interest for you.
Writing a driver for the hardware spi-to-i2c chip and changing every library for the sensors or using a (almost) drop-in replacement for the I2C libraries when using a software I2C library.
I know what I would do.
Either way, you have to do some work for each and every sensor.
I don't remember anyone doing this on such a scale, so I don't know a working example.
This topic looks familiar from another forum. Final advice was SC18IS600 like @markd833 wrote.
However, the price for this chip is a bit high. It's close to price for ATmega328P so you an use some cheap ATtiny with SPI and I2C (TWI) as a bridge. This require some effort on SW level, but it will pay off in this amount.
Another solution, which I found just out of curiosity, could be use of LTC4317 - Dual I2C/SMBus Address Translator. The price for this chip seems to be more reasonable.