1
\$\begingroup\$

I have 2 Arduino microcontrollers, each with a network of I2C devices connected to them (one has 2 ADCs and the other an LCD display and a RTC). How can I use the I2C connection to transfer the values obtained by the first uC from the ADCs to the second ? Both uC are masters on their I2C busses. I was thinking of making a software I2C on the second uC and connect it as a slave to the first one (so the second controller would have 2 I2C ports: one hardware and one software). Problem is, I can't find any software I2C library that works as a slave. All are masters.

Waiting for your ideas.


Question is, can I make 2 I2c networks using one Arduino Mega2560 ? One as master on the hardware port to communicate with the LCD and the RTC and one as a slave on a software port on 2 other pair of pins (for SCL and SDA) for receiving data from another master arduino...


After analyzing all the data, I reach the conclusion that the 2 I2C busses cannot be linked together. On the external I2C port I have available on the data acquisition uC, I will connect another arduino as slave that will receive the information an pass it on by using a wireless adapter (probably a NRF24N01). That way, I don't need to have wires from my solar controller to the arduino that reports the production to pvoutput website.

\$\endgroup\$
  • \$\begingroup\$ Which Arduino (actually which micro processor)? \$\endgroup\$ – st2000 Dec 29 '16 at 22:44
  • \$\begingroup\$ Arduino Pro mini is connected to the ADCs. Arduino Mega2560 is the one with etc and LCD. \$\endgroup\$ – Mihai Dec 29 '16 at 22:53
0
\$\begingroup\$

From the comments you want to be able to connect only to the I2C bus on each of your separate projects and transfer data.

Wirning lets you run an Arduino as either an I2C Master or a Slave. You can't run both the master and slave Wiring software on a single micro since they both want to use the USI hardware, but since you only have one USI, you can connect to only one bus anyway.

I'd suggest an effective way would be to use an ATTiny85 as a I2C slave interface on each bus and then connect them together via a software UART. There is a very nice TinyWire library available for ATTiny85 from Adafruit....they also have a very small board called the Trinket too that you could use.

There are a bunch of ATTiny85 boards (like Digispark) available that can hook up to the Arduino programming environment so this should be a simple and cheap way to create an I2C slave.

While some may say this is overkill, it would be extremely simple to implement and would not require any hardware mods to your project.

\$\endgroup\$
1
\$\begingroup\$

IIC is not a good choice for peer to peer communication.

The easiest way to connect to microncontrollers is probably with UARTs. Note that provides a separate and asynchronous channel in each direction. You also aren't stuck with standard baud rates. Use something fast that can be derived directly from both clocks. You can easily do a MBaud or more between UARTs on the same board.

Especially if you crank up the baud rate, it might be a good idea to implement flow control. Some microncontroller UARTS have RTS/CTS built in, but even if not, this kind of capability is easy to add in firmware. Make sure that the reciever can buffer at least as many characters as there are in the hardware FIFO of the sender. That way the flow control line can be used in the sender to simply not write more data to the hardware. Microcontrollers tend to have small UART output FIFOs (usually just one or two, rarely more than 4), so this is not much of a problem.

\$\endgroup\$
  • \$\begingroup\$ An Arduino Pro mini is connected to the ADCs and it forms the display unit of a solar charge controller (it has a special LCD connected serially). The only connection I can use is the I2C (or I have to make a mess out of the PCB is on). An Arduino Mega2560 is the one with the RTC and LCD and it also has an Ethernet shield. This data transfer is not necessary to be fast, I just need to send 2 values (volts and amps) each second or so. \$\endgroup\$ – Mihai Dec 29 '16 at 23:03
0
\$\begingroup\$
  • You can only have one I2C master on the I2C bus. (There maybe some advanced protocols where you can switch who is the master.)
  • Many embedded processors can be configured as either I2C master or slave. (I do not believe the stock Arduino I2C library allows for an easy switch between master and slave. There may be Arduinos which use processors with no support for slave mode.)
  • These types of protocols (SPI or I2C) should be used by parts sharing the same board and power supply. They are generally not robust enough to suffer the complications of (for example) partial power failures, hot-plugging or excessively long cables.

All that said, look here for how to connect two Arduino Uno boards (one master and one slave) using an I2C bus.

\$\endgroup\$
  • \$\begingroup\$ I was viewing this whole system as 2 separate I2C networks: first one with Arduino promini as master + ADC and Arduino Mega2560 as slaves. The second network made up from Arduino Mega2560 as master + LCD and etc as slaves. As you can see, I want Mega2560 to have 2 I2C connections, one as slave (software) and one as master on the hardware port. \$\endgroup\$ – Mihai Dec 29 '16 at 23:10
  • \$\begingroup\$ It is possible to implement a software slave I2C port. You either have to devote your program to nothing but monitoring the CS, Clock & Data lines during a transfer. Or slow the SPI down until the slave can handle the transfer and what ever else you need to do on the slave processor. \$\endgroup\$ – st2000 Dec 29 '16 at 23:21
  • \$\begingroup\$ Oops, I need to switch from SPI to I2C in my answer. Also, there is no CS (chip select) for I2C. I2C uses slave addressing in its protocol. \$\endgroup\$ – st2000 Dec 29 '16 at 23:25
  • \$\begingroup\$ I see there are some libraries for a software I2C, but all are masters... No slave... I could use the software I2C for communicating to the LCD and RTC but I doubt the libraries made for these modules would work with a software port... \$\endgroup\$ – Mihai Dec 29 '16 at 23:29
  • \$\begingroup\$ Consider a situation where you know exactly what you want to send between your 2 Arduino boards and how fast you want to send it. Consider that it may be easier to implement some custom hand shaking interface implemented in software on both processors. See, I don't think you need (for instance) the addressing overhead of the I2C protocol. After all, you only have 1 master and 1 slave. And you may only need to send data in 1 direction. So you don't need to go to the trouble of tristating lines and pulling them up with resistors. \$\endgroup\$ – st2000 Dec 29 '16 at 23:47
0
\$\begingroup\$

What @olin has suggested is something you should consider.

Incase, you are in need of two modes of I2C ( both master and slave ), then read on.

The arduino can be configured as either master or slave at any given point in time. So, i would try this. The arduino which has to take both roles (call it Arduino A) can be configured as slave by default. By this way, the Arduino will not miss any commands from the Arduino B (the arduino who will master always).

The Arduino A can decide itself when to change its role to Master. This depends on the periodic intervals it has to read data from. Once data is read from the sensors or data is written to LCD, the arduino A can take up role of slave again.

One catch is that, while Arduino A has taken up role of the master, the Arduino B cannot communicate with the Arduino A. This can be solved in many ways:

  1. Using a spare GPIO, Arduino A informs to B that it is busy
  2. The Arduino B can retry after a small period
  3. Arduino A can inform Arduino B when it is ready for communication (example: letting know the B when the data to be communicated is available via GPIO).
  4. Program the Arduino B to periodically write the data what Arduino A expects.

Consider http://www.gammon.com.au/i2c if helpful. There are good information about configurations. Everything in one page. Please don't get lost while navigation.

Finally, the factors which influence the decision of logic is your own application demands. The frequency of communication -which might be once in a while or contiguous, criticality of response time, not the least amount of resources one would like to spare,in case one decides to implement I2C software slave also.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.