I'm trying to communicate with 7 arduino slaves with one master arduino, each of these slaves is then connected to 6 sensors via I2c as well. I'm using the I2C master library. Basically my end goal is to be able to select which of the 7 arduinos i want to receive data from at any point, and then be able to send data to that arduino slave telling it to increase or decrease its pwm duty cycle. However when I have the SCL SDA VCC and GND connected I cant seem to communicate with the master arduino. I open the serial monitor but cant read anything or write anything.
I'm quite sure this line is not doing what you expect it to do.
I2c.write(20,TWAR);
Actually it sends the content of the Two Wire Address Register (which is 254 after reset) to the I2C slave with address 20.
Am I getting your description right, you have 7 Arduinos connected to one I2C bus and 42 additional sensors on the same bus? If this is not correct, please provide a wiring schemata of your project.
If my interpretation of your description is correct, your setup won't work this way. The I2C will collapse before any communication happens.
Please provide a link to the I2c master library you're using because there are several different versions of libraries available in the Net under this name.
Also the 6 sensors that are connected to each of the 7 arduinos all have the same address. They are 6 thermistors connected to one arduino all six sets have the following address:
int tmpAddress1 = 0x48;
int tmpAddress2 = 0x49;
int tmpAddress3 = 0x4A;
int tmpAddress4 = 0x4C;
int tmpAddress5 = 0x4F;
int tmpAddress6 = 0x4E;
I was thinking that since I'm only querying the thermistors when I call for one of the arduinos that they would not conflict.
Also I know when using the wire library I can assign each arduino a unique address by wire.begin(##). I cant seem to figure out how to do that using the I2C library I posted above. Or how to initiate the master slave configuration. The reason I want to avoid the wire library is that I have a board with 2 blown thermistors and the wire library seems to hang on the acknowlegment from those two thermistors .
Your library is called I2C Master because it only implements the master part of an I2C bus, so you can use it only on the master but not on the slaves.
The reason I want to avoid the wire library is that I have a board with 2 blown thermistors and the wire library seems to hang on the acknowlegment from those two thermistors .
The only way I can imagine that the bus will let the Arduino hang is by the clock stretching feature. So if your sensors are touching the clock signal the Arduino is doing what it should do, wait until the sensor allows the bus to be used again.
If you have faulty hardware on the bus and you want the Wire library to reinitialize the hardware if the faulty sensors are blocking the bus again, use the patch provided in this thread: http://forum.arduino.cc/index.php/topic,19624.0.html.
To be able to imagine how your project looks like, please describe how the Arduinos are interconnected and how the sensors are connected to the Arduinos. If you connect all that stuff to one I2C, it won't work. Please post a wiring diagram or a photo of the setup to get an idea.
Sorry for the late response. I thought I had gotten it to work. But it seems that after some time ~1 to 1.5 hours. The I2C communication freezes. I brought down the number of slaves because I figured it might be an issue with the I2C lines being too long. Attached is a rough diagram of how my setup is. As well as my updated code for the slaves and the master.
If the posted schematics is correct, the setup probably never worked as expected. All temperature sensors would be connected to the same I2C bus and probably all of them would try to answer if the were asked to send the temperature. If you connect the PWM outputs and the thermistors to the 3 Arduinos you should connect the Arduino to each other and the master using another bus system. Depending on the distance between them I would choose SPI or an RS-485 bus.
Regarding your code: You should learn to work with C/C++ arrays. You can eliminate around 70% of your code with them.
I see, Thanks for the response. I did see some weird things happening like all the arduinos kept sending me the same 6 values for all their thermistors, probably because only the same six always answered. How would I connect the arduino slaves to the master using SPI?
For SPI you need 3 bus wires plus one selection wire for every device you want to connect. The bus wires are MISO, MOSI and SCKL (found on D11, D12 and D13 on an UNO). The SS pin (on pin D10 on an UNO) is fixed for the slave but you can use any pin on the master, just ensure every slave gets it's own.
At the moment you're using/wasting a lot of pins for your dip switch and other stuff that can easily be changed to a port expander chip (p.e. MCP23017) without loosing any functionality.
What distance do you have between the Arduinos? Are they all close together?
I was wondering if it would be possible to use software I2C on the master end and connect three slaves to three different pairs of I/O? I would use 6 pins instead of two.
I was wondering if it would be possible to use software I2C on the master end and connect three slaves to three different pairs of I/O? I would use 6 pins instead of two.
Theoretically that may work but you have to write a lot of software because that functionality is not supported by standard libraries. None of the I2C software emulations I know of as well as the Arduino Wire library support multi-mastering the I2C bus. This is a rather complex area, are you sure this is worth the effort?
Use SPI between master and the 7 slaves (will require 3 bus wires plus 7 CS for the master and 3 bus wires plus 1 CS (= slave CS) for each slave (="node") )
Use I2C on each "node" to connect the dedicated 6 sensors
Collect the sensor data on an SD card and request those data with an adequate time cycle from each node (SD card connection via SPI with CS=SS=10 for Uno as master CS)
Depending on the data feedback send a PMW-modify command to the related "node" and delete the old data on the SD card to free space (maybe only needed every 1000th query?)
Use SPI between master and the 7 slaves (will require 3 bus wires plus 7 CS for the master and 3 bus wires plus 1 CS (= slave CS) for each slave (="node") )
Use I2C on each "node" to connect the dedicated 6 sensors
Exactly what I proposed, so yes.
Collect the sensor data on an SD card and request those data with an adequate time cycle from each node (SD card connection via SPI with CS=SS=10 for Uno as master CS)
This does not work because SS is used for the slave SPI interface and for the slave this cannot be changed. BTW, I don't see a need for an SD card on every slave.
Depending on the data feedback send a PMW-modify command to the related "node" and delete the old data on the SD card to free space (maybe only needed every 1000th query?)
I don't know what a PMW is but anyway, I think this is completey unnecessary. Just get the measurement values by SPI from the slaves. As SPI is at least 10 times as fast as I2C there shouldn't be any problem to get every measurement in a timely manner.