Skip to content

Instantly share code, notes, and snippets.

@anttivs
Created July 22, 2013 17:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anttivs/6055828 to your computer and use it in GitHub Desktop.
Save anttivs/6055828 to your computer and use it in GitHub Desktop.
Notes on connecting an Arduino as an I2C slave
Arduino Uno speaks i2c from A4 (data, SDA) and A5 (clock, SCL).
These need to be connected to Bus Pirate MOSI (data) and CLK (clock) lines.
Setting arduino as a slave:
#include <Wire.h>
void setup()
{
Wire.begin(42); // i2c bus address 42
Wire.onRequest(requestEvent); // Called when the master sends a request
}
void loop()
{
delay(100);
}
void requestEvent()
{
Serial.print("Slave received request...");
Wire.write('A');
}
On the Bus Pirate side, set up Bus Pirate to do I2C:
HiZ> m
Select I2C and 100 kHz.
Scan the devices on the bus:
I2C> (1)
Searching I2C address space. Found devices at:
0x54(0x2A W) 0x55(0x2A R)
You will see two addresses, one for read and one for write. This is the 8-bit address which is created from the 7-bit address in the Arduino sketch by a left bit shift and addition of a zero or one LSB that indicates a read or write.
(In the sketch, 42 decimal is 0x2A hexadecimal; 0x54 is 0x2A shifted left, 0x55 is 0x2A shifted left plus 1.)
You will be addressing the Arduino slave through these 8-bit addresses.
The sketch is waiting for our command; the I2C protocol starts with a start bit, and a device read address:
I2C> [0x55 r]
I2C>[0x55 r]
I2C START BIT
WRITE: 0x55 ACK
READ: 0x41
NACK
I2C STOP BIT
The 0x41 is the 'A' written out in the sketch.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment