I2C Wire Chat Between Two Arduinos

Someone asked me the other day if it was possible to have a ‘chat’ between Arduinos using the I2C Wire library. I came up with  ‘proof of concept’ code called ‘I2C Wire Chat Between Two Arduinos’. Here is the sketch:

I2C Wire Chat Between Two Arduinos

Setup two Arduinos connected by a length of CAT5 cable (you really only need 3 wires). Connect Analog Pins 5 (SCL), 4 (SDA) and Ground as shown in this Fritzing diagram:

I2C Wire Chat Between Two Arduinos

I2C Wire Chat Between Two Arduinos

Make sure each serial port is set to 9600 BAUD and type: #s in one Arduino and #m in the other making sure that ‘New Line’ is sent as the end character. This will put one in slave mode and the other in master mode. Due to the design of the I2C Wire library one has to be master and one slave. Only the master can send data (the slave receives), so it is not simultaneous transmit/receive. Whatever is typed in the master serial console is sent to the slave which displays it on its serial console. To switch back and forth (master <=> slave) just alternate the #s and #m. So you can in essence have a ‘chat’ with the I2C Wire library.

It basically functions the same as a Walkie-talkie radio.

One could even take the sketch further to automatically switch the master to slave and slave to master. For example, when the master is finished it could send a # as the last character which could function as the toggle. I will leave it to the reader to implement this. ğŸ˜Ž

Well.. OK.. just so you can see how the code develops, here is the final version with the ‘toggle’ master/slave being the ‘#’ as the last character in the transmission buffer:

I2C Wire Chat Between Two Arduinos  [Final Version]

Over and Out!

Leave a Reply

Your email address will not be published.