Now one can send a stream of data to the second, while the second sends back a message if the value on analog port 0 drops below 512. This uses Wire.read rather Wire.receive.Įxample of connecting two Unos together to communicate via I2C Updated February 2012 to allow for version 1.0 of the Arduino IDE.
This was displayed on LEDs plugged into pins D2 through to D7 using an appropriate resistor in series with each one (eg.
The slave code has nothing in the main loop, because the two-wire interface generates an interrupt when data arrives. called by interrupt service routine when incoming data arrives
The code to produce this (in Arduino's C++ language) was: The transmission is ended by the "Stop condition" (labelled Stop) which is sent by releasing the SDA line to allow it to be pulled up while SCL stays high.More data bytes could be transmitted, but are not shown here.Again, after the 8 bits of data, the master checks that the slave acknowledges by pulling the SDA line low.Then the data byte (0x53 in this case) is transmitted, most significant bit first.This counts as a NAK (negative acknowledgement). If there is no slave connected and powered on, or it does not have the required address, then it will ignore the address, leaving the SDA line high (by the pull-up resistor). The master then waits for the slave to pull the SDA line low which is an ACK (acknowledge) that a slave of that address exists and is ready to receive data.0 means write (master to slave) and 1 means read (slave to master). Then the read/write bit is transmitted.The logic analyser reports the address as being 0x54 but that is really 0x2A (this is, 42) shifted left one bit, so that the "write" bit (0) is in the least-significant bit place. The 7-bit address of the required slave is then transmitted, most significant bit first.This is when SDA (serial data) is pulled low while SCL (serial clock) stays high. The transmission starts with the "Start condition" (labelled Start).It shows the character "S" (0x53) being sent from the Arduino to the device at address 42.įrom the above graphic note the following points of interest: Let's start with an image - this is a screenshot taken with a logic analyser. Note also that the Atmega specifies a maximum bus capacitance of 400 pf, so that would rule out long cable runs.
In other words, you don't swap pins (like you do with serial communications, where you connect Tx to Rx and vice-versa). This is because the pull-up resistors keep the lines high until one of the devices wants to use it by pulling a line low.
The pins should be connected together (that is, pin 4 to pin 4, and pin 5 to pin 5, if you are connecting Arduino Unos together). Of course, you also need to connect the GND (ground) pins to complete the circuit. Warning: for multiple I2C devices, or longer cable runs, the 4.7K pull-up resistor (for each line) is recommended.Īlso see further down in this thread for some screen-shots of the effect of using different pull-up resistors. The Atmega328 is configured to use internal pull-up resistors which may be adequate for short cable runs. These pins may require pull-up resistors (that is, connect them to +5v via something like a 4.7K resistor each). They are also connected to D2 (SDA) and D3 (SCL). On the Arduino Leonardo, the SDA and SCL pins are separate pins, so marked, on the board (next to AREF). On the Arduino Mega, SDA is digital pin 20 and SCL is digital pin 21 (they are marked SDA and SCL on the board itself).
The Two-Wire interface is extremely useful for connecting multiple devices, as they can all share the same two pins (plus a ground return). A lot of the details however will be of more general interest. This post describes how the I2C (Inter-Integrated Circuit, or "Two-Wire") interface works, with particular reference to the Arduino Uno which is based on the ATmega328P microprocessor chip. This page can be quickly reached from the link: A summary of everything shown below is available further down this page: