Arduino Uno Simultaneous Multiple Soft I2C Library - Page 1
Forum section: electronics / projects

author: Pak_Engineer
author_rank: Contributor
timestamp: September 10, 2012, 10:21:50 am
content: Hi,There are a lot of I2C libraries for Arduino on internet but there is one problem - none of them supports multiple I2C to communicate to devices simultaneously. I needed a library of I2C that could communicate to multiple sensors (having same address) using a single Arduino and much to my disappointment, I couldn't find one. I kept on searching and the best closest thing I could find is 'softi2c' library by fat16lib.http://forums.adafruit.com/viewtopic.php?f=25&t=13722I tweaked it a little bit to be able to handle multiple I2C buses at the same time. All buses have software selectable pins and they can either have a shared or independent SCL pins, depending on user requirement. The example code is for VCNL4000. In the example I haven't written different functions for read and write but of course it can be modified. I did it because the sensor is in proximity mode and the Command Register needs to be written each time prior to a valid read. The code is tested for Arduino 1.0. Hopefully this helps.

author: ZmeiGorynych
author_rank: Newbie
timestamp: November 10, 2015, 10:19:36 am
content: Thanks a lot!I tried it just now and it wouldn't compile because it couldn't find Wprogram.h.The fix is simple - replace the line#include <Wprogram.h>in TwoWireBase.h with#if defined(ARDUINO) && ARDUINO >= 100#include "Arduino.h"#else#include "WProgram.h"#endifbuilt nicely for me after that

author: eneuro
author_rank: Super Contributor
timestamp: November 10, 2015, 11:58:18 am
content: Does it support multiple masters on the same SDA/SCL pins with arbitrage support on the same I2C bus?I need multiple masters available in my I2C design, so probably I will write support for this based on oryginal I2C specyfication, but I used another I2C core soft library-for many ATTiny85's working in parallel, each one should be capable ask single ADXL345 3-axis accelerometer sensor and RTC clock and i2C compass module, so i need support for multiple masters sharing the same I2C bus...

author: ZmeiGorynych
author_rank: Newbie
timestamp: November 10, 2015, 12:26:08 pm
content: Which other library did you use?I just need to drive several I2C chips (SN3218), all of which have an identical (hardwired) I2C address.

author: eneuro
author_rank: Super Contributor
timestamp: November 10, 2015, 01:19:11 pm
content: I found among others this I2C master AVR implementation as good starting point to tweek to fit my neeedshttp://homepage.hispeed.ch/peterfleury/doxygen/avr-gcc-libraries/group__pfleury__ic2master.htmlbut this is pure AVR library.QuoteHowever it doesn't supported mutiple masters sharing the same i2C bus, so after looking into NXp I2C docshttp://www.nxp.com/documents/user_manual/UM10204.pdfI've found how I2C arbitrage should like in I2C:However, you have other issue-you have many slaves at... the same I2C address, so even multiple master capable I2C library doesn't help...Look for other tricks how to deal with thisAnyway this can be helpfull to everyone playing with i2C buses.Troubleshooting I2C Bus Protocol

author: eneuro
author_rank: Super Contributor
timestamp: November 10, 2015, 02:13:30 pm
content: Quote from: ZmeiGorynych on November 10, 2015, 12:26:08 pmUnfortunatelly, looking into its datasheethttp://www.si-en.com/uploadpdf/s2011517171720.pdfthere is no chance pull down any of its pins to get alternate addressThe only trick I see I I wanted use this device on only 2 pins (shered I2C bus) is use its SDB pin to turn all devicess off, but this one you want to communicate using another I2C GPIO extender like in this threadI2C bus interfacing with two slaves that have the same address.QuoteHowever, it might be not an option since you will be only able to control LEDs connected to this one active device.I'd look rather for I2C devices which has capability to have at least two alternate addresses-by togging one of its pins to GND or VCC.Update: Or mentioned in link above additional I2C multiplexers switches-I've choosen I2C devices wihich have alternate address capability like SDO pin in ADXL345http://www.nxp.com/products/interface_and_connectivity/i2c/i2c_multiplexers_switches/I like my ADXL345 while by toggling its SDO pin to GND or VCC I can select its adress to 0x1D or 0x53, so we can imagine that using only one additional I2C GPIO extender, If I wanted use many of such accelerometers connected to the same shared I2C bus, depending on timings needed in application, I'd send I2C message to GPIO extender, select given accelerometer adresses by toggling ADXL345 SDO pin (all the same but the one I want to communicate to different one), so in next step I should be able connect to selected I2C address , using this ONLY one ADXL345 different address eg. 0x1D, while other will be 0x53

author: Fungus
author_rank: Super Contributor
timestamp: November 10, 2015, 03:17:13 pm
content: Quote from: eneuro on November 10, 2015, 01:19:11 pmA basic I2C master is very easy to code. If you're only targeting one particular chip it's even easier (eg. you could assume it will always supply the ACK signal and not bother to check that - just send it whatever works and no more!)If you need to connect multiple chips which don't have address address pins you'll need two Arduino pins per chip.If you're really handy at coding you can use a single pin for the clock signal going to all the chips. Save pins by updating them in parallel :-)Quote from: eneuro on November 10, 2015, 02:13:30 pmThat's quite typical.Some chips even have multiple address pins so you can connect many chips at once (eg.PCF8574has 3 address pins - connect 8 chips!)

author: eneuro
author_rank: Super Contributor
timestamp: November 10, 2015, 06:11:03 pm
content: Quote from: Fungus on November 10, 2015, 03:17:13 pmThis is probably why thread creator made possible use many I2C pin pairs on his Arduino, but I sugested use somethign else to share the same I2C bus and add I2C GPIO expander, since we can save many MPU pins and no need to change many already available I2C master libraries, eg, those with hardware support , not this software version shown in first threadInstead of I2C expander one could use additional MPU pins (one per device) to change those alternate I2C addresses by toggling its address pin directly from MPU, so we save one MPU pin and no need to tweak I2C master library-just choose I2C devices capable to have at least two alterbnate addresses configurable, by changing this address pin.However, now I realized and didn't checked it in ADXL345 if... this SDO pin can be changed after I2C device restart on fly-I mean, I hope this address is updated whenever address pin changes, because of this is a must if we want this trick to work