Languages

Experiment: I2C communication between two Arduino boards

I had this (wrong) idea that I could communicate with more than two Arduinos through the RX/TX ports using Serial communication. One of the issues is, (1) that it's not possible to do so :) ... and (2) even if it would, it would not be possible to identify the nodes in the network. Chatting on the Arduino IRC channel, I was suggested to use a protocol such as I2C or 1-Wire.

The main advantage of using I2C is that it is very simple to set up. You only need pull-up resistors and wires. You can then use the Wire library for Arduino. There is a very good example of how to use it here, together with schemas. The main drawback of I2C is its short range (max. 2 meters, according to one user on the channel, over which communication might become unreliable).

As for 1-Wire, I chose not to investigate it for now since it requires additional components.

I thus implemented a small Master-Slave configuration with two Arduinos. The master (on the right-hand side of the image and video) just sends an integer to the slave (on the left-hand side). The integer is represented by LED blinks. It works pretty well.

IMPORTANT The VCC and the Ground need to be shared by both circuits, otherwise the communication won't work.

If we have several modules linked to one another, we can all set them as Masters (we just need to add the 1.5k pull-ups). However, we would need to find a way to connect all of their VCC and GND together. I guess that could be done by adding two wires (which mean that 4 wires would pass between each pair of nodes), I'm just worried about the implications of plugging the 5V outputs all together in parallel (I think it's ok though, it would just allow for more amperage, but wouldn't do any short-circuit).

The other question is: does that work at a low voltage (e.g. 3.3V) like what gives a solar panel.

I had this (wrong) idea that I could communicate with more than two Arduinos through the RX/TX ports using Serial communication. One of the issues is, (1) that it's not possible to do so :) ... and (2) even if it would, it would not be possible to identify the nodes in the network. Chatting on the Arduino IRC channel, I was suggested to use a protocol such as I2C or 1-Wire.

It is always advisable to

It is always advisable to keep track on the system. - Mint Springs Farm

Distance

Wondering, If the arduinos were connected only with wire, how long the distance that could hold up the data transmitting process ?

A very interesting post. Nice

A very interesting post. Nice you share with all you knowledge. Thanks

Alice

Serial communication with more than two Arduinos

I just want to point out, that it is possible to set up a serial communication with more than two Arduinos. I2C is definitely much better, but here is what you would have to do. You have to setup a ring. TX 1 => RX 2, TX2 => RX 3, TX3 => RX 4, .... TX n => RX 1. Each Arduino needs to know its ID. Each send message needs to send the ID of the receiver. When an Arduino receives a message it compares its ID with the message ID. When equal you react to the message (consume), if not you send it to the next node. Not very efficient, but definitely possible.

Nice you share with all you

Nice you share with all you knowledge. You save time today to me. Thanks.

Pullup resistors

Thank you for your great example. This is what I was looking for. I am still very confused about pullup resistors/I2C en the Arduino. I have found many sites/examples where they state that you don't need to pull-up the I2C lines because the Wire library uses the internal pull-up resistors of the Arduino.

I have tried this example without the pull-up resistors to the 5V line and it is working great for me. So I guess the internal pull-ups are working?

Re:

I2C is pronounced "I squared C" and stands for Inter-Integrated Circuit. This protocol was designed by Phillips Semiconductors around 1992 to allow easy communication between components on the same circuit board and can achieve transfer rates of up to 400 kbit/sec. Is is a 2 line (plus common ground) communication method for one master device to control up to 112 slave devices. While it is possible to have multiple masters on the same I2c bus, this page will only deal with a one master configuration.

Interesting link on hackmod

Another comment: other bus types

So you had the "wrong" idea about the serial being a bus?

In fact, it is wrong an idea when we are talking about the RS232 or the logic-level serial on the Arduino.

But there are serial bus systems. Research the RS485 standard for example.
This can be implemented using the AVRs serial port and a simple diver chip like the SN75176 or its low power cousins from Maxim.
the tricky part is the bus arbitration: no two senders may send at the same time. Usually this involves a master-slave scheme of some kind.

I am using RS485 type buses for all the long-haul communications where TWI is really at hte limit.
When using 5bit datawords (5n2 is the smallest the AVR serial supports, iirc), then the clock difference between the devices on the bus may even be 3% or so - usually communication is stable even with devices running off the RC oscillator (without a crystal).

For linking a bunch of Arduinos this is a very good choice for hte long distance: cheap drivers, good isolation (worst case: a driver chip burns out) and loooong distance is possible (one through your garden and back).
Drawback is bandwidth: on a long line only like 4800 or 2400 bit/s are really usable.

hase

Answers to some of your questions :-)

It is possible to run I2C (or TWI as Atmel calls it - I2C is a trademark of Philips/NXP) between 3V3 devices just as for 5V devices.
The iportant thing is that all devices on the bus have teh same Voltage level.
It is also possible to have a 3V3 and a 5V bus coupled together using a level shifter. In the TWI cas that would simply be two small FETs like the BSN20, one for each line (clock and data).

Imho it is *not* necessary to have the 5V lines of both devices on teh bus connected, a ground connection should be sufficient.
But a device powered down while on the bus may disturb the bus.

Optical isolation souinds like a good idea - but sadly it does not work with TWI as both signals, data and clock are bi-directional. This is very hard to buffer ot isolate.

The actual pull-up resistors used are sometime a bit critical.
The problem is, they must be as large and as small as possible at hte same time.
To elaborate: the smalles value for the resistors is defined by the ability of the bus devices to sink current. This is 20 mA for an AVR pin but may be a lot less for other devices like sensors.
so 5.5V / 20 mA = 275 Ohms - any smaller will fry the output in the AVR.
This value would make sure the bus charges up to a HIGH level (near 5V) very quickly, even when the capacitance of the bus is high.
Still, this would require a lot of power while communicating, so larger values for the resistors are desired.
But the larger the resistor value, teh less current will be available to charge the bus line to the HIGH level.

I found that values between 2k2 and 4k7 are usually good; if I get problems with communications, I lower the value to 1k or 470 Ohms if the devices allow for the current.

keep experimenting and posting!

hase

Delay

Is the delay time between the LED's in the video because of the delay()'s in your code, or is that how slow the i2c is? I'm looking at connecting several Arduinos, but my communication needs to be very low delay. Thanks

re: Delay

The delay is caused by the code. Actually, there is hardly any i2c-delay, but the master only tells the slave to blink x number of times after it is done doing it's own blinks (including a delay after the last blink).

try this

Shared VCC isn't required

IMPORTANT The VCC and the Ground need to be shared by both circuits, otherwise the communication won't work.

You could opto-isolate them instead:

http://www.standardics.nxp.com/products/p82b/datasheet/p82b96.pdf

Source code?

It would enlighten us much more if you could post several lines of code, as Arduino's docs on I2C and the Wire library is very limited. And even more limited is the implementation of slave devices. Most people only write arduino masters. Example slave code is rare.

no

no

Code in attachment

I've published the code for both master and slave in attachment to this post (see the .pde files at the end).

Where did you publish the code?

Please publish it to my site:
http://ruzz.ca/diarrhea-symtoms-treatment/

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <pre> <h3> <h4> <h5> <h6>
  • Lines and paragraphs break automatically.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Fill in the blank
By submitting this form, you accept the Mollom privacy policy.