Hardware Reference
In-Depth Information
When the Arduino slave receives an I 2 C communication, the Wire library
calls this function with the number of bytes received. To receive individual
bytes, call Wire.read() .
data = Wire.read();
Just as when communicating as a master device, Wire.read() reads 1 byte
from the I 2 C buffer and returns that data. Similarly, to know the amount of
remaining bytes in the I 2 C buffer, call Wire.available() .
number = Wire.available();
It is, of course, possible to mix the two functions together.
while(Wire.available())
{
data = Wire.read();
// Do something with data
}
Sending Information
When a slave Arduino is asked for information, the Wire library calls the function
previously registered by Wire.onRequest() . Again, the name of the function can
be anything you want, but this one takes no parameters and returns nothing.
void sendData(void)
{
// Put your code here
}
Wire.onRequest(sendData); // Create the callback
You must then provide the data required by the master, using Wire.write() ,
explained previously.
Example Program
For this example program, you use two Arduinos: one acts as an I 2 C master,
and the second acts as an I 2 C slave. Both connect together using the I 2 C bus.
Because Arduinos have internal pull-up resistors, the resulting schematic is
extremely simple. The SDA pins of both devices are connected together, and
the SCL pins are also connected together. There is one last, important stage:
Both grounds are also connected—yes, three wires between the two devices. I
said that I 2 C is a two-wire solution, and it is. It was designed to be used inside
a single device, where the power supply and ground is normally identical. It
 
Search WWH ::




Custom Search