Go Down

Topic: Arduino as I2C Slave ? (Read 10263 times) previous topic - next topic

JAy1st

Has anyone ever made an Arduino as a Slave I2C ?

I'm planning to use 2 minis as slaves connected to a master Arduino(main cpu + lcd & ethernet).

Is there anything specific I should be aware of ? like I2C address settings or what ever ?

Tnx....

kg4wsv

It's fairly straightforward.  Check the wiring documentation under "Wiring (TWI)" for the master and slave sender and receiver examples.

-j


JAy1st

mmmm, tnx for the link.

Does it allow sending/receiving in the same sketch as well and how to I set up the address ?

Ah ok, adress is the Wire.begin(2) thingy in brackets is it ?
So that means I can use any adress as long there is no conflict.



kg4wsv

Here's a rather idiotic proof of concept I threw together, mostly by copying and pasting code from the wiring.org.co documentation pages.  It's a producer on the slave and a consumer on the master.  The only thing the master sends is the number of bytes requested, which it then prints to the serial port.

A real application would probably need a bit of protocol and probably more bidirectional data flow, but it really depends on what you're doing.

Hope this helps a bit.

-j

i2c_master.c:
Code: [Select]
// i2c/wiring proof of concept code - tie two Arduinos together via
// i2c and do some simple data xfer.
//
// master side code
//
// Jason Winningham (kg4wsv)
// 14 jan 2008

#include <Wire.h>

void setup()
{
 Wire.begin();        // join i2c bus (address optional for master)
 Serial.begin(9600);  // start serial for output
 Serial.print("master sleeping...");
 delay(2000);
 Serial.println("go");
}

void loop()
{
 Wire.requestFrom(2, 1);    // request data from slave device #2

 while(Wire.available())
 {
   char c = Wire.receive(); // receive a byte as character
   Serial.print(c);         // print the character
 }

 delay(100);
}


i2c_slave.c:
Code: [Select]
// i2c/wiring proof of concept code - tie two Arduinos together via
// i2c and do some simple data xfer.
//
// slave side code
//
// Jason Winningham (kg4wsv)
// 14 jan 2008

#include <Wire.h>

void setup()
{
 Wire.begin(2);                // join i2c bus with address #2
 Wire.onRequest(requestEvent); // register event
 Serial.begin(9600);           // start serial for output
}

void loop()
{
 delay(100);
}

// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void requestEvent()
{
 static char c = '0';

 Wire.send(c++);
 if (c > 'z')
   c = '0';
}


JAy1st

Ah funny my name is Jason too  ;D


Ok I got it.

The plan is to have an arduino running with an ethernet shield (have it already and plyed with) but it's taking lots of memory...
So I'll hook-up a 2nd arduino as a I/O master.

But, an arduino slave can control an I2C RTC or anyotherthing over the same bus ?

I would like to keep the serial for the ethernet one, easing the troubleshooting and debugging....

John_Ryan

Quote
Ah funny my name is Jason too  ;D


Ok I got it.

The plan is to have an arduino running with an ethernet shield (have it already and plyed with) but it's taking lots of memory...
So I'll hook-up a 2nd arduino as a I/O master.

But, an arduino slave can control an I2C RTC or anyotherthing over the same bus ?

I would like to keep the serial for the ethernet one, easing the troubleshooting and debugging....


http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1205243372

We've created a PCB with that application in mind. 2 IC's, 2 free serial bus's, with an inline i2c header.



JAy1st

Yes, I did read it and it's good !
(willing to send a bord ?  :D )

So I guess I'll make the "ethernet arduino" a slave and the other one a master to gain access to other I2C bus and take control of RTC and GPIO expanders.......


kg4wsv

Quote
But, an arduino slave can control an I2C RTC or anyotherthing over the same bus ?

I think that's referred to as a "multiple master" situation, and the answer is, I don't know how it all works.  I'm pretty sure the ATmega can do it, but I don't know how to do it, or if the Wiring library is compatible with multi-master operation.

You can always let the master talk to the clock, then tell the slave what time it is.

-j


JAy1st

As the master/slave is made by coding it should not be to difficult to implement.

I'm gonna order a min a give it a try .

Tnx for your precious help !

Go Up