Newer
Older
PiPins / i2c.txt
root on 18 Mar 2020 1 KB pin 1 marking
		 1](2
	Data	 3)(4
	Clock	 5)(6
		 7)(8
		 9)(10
		11)(12
		13)(14
		15)(16
		17)(18
		19)(20
		21)(22
		23)(24
		25)(26
EEPROM Data	27)(28	EEPROM Clock
		29)(30
		31)(32
		33)(34
		35)(36
		37)(38
		39)(40

I2C pins in BCM mode are: 2, 3
I2C pins in WiringPi are: 8, 9

- I2C - Inter Integrated Circuit -

The Raspberry Pi's I2C pins are an extremely useful way to talk to many different types of external peripheral; from the MCP23017 digital IO expander, to a connected ATmega.

The I2C pins include a fixed 1.8 kohms pull-up resistor to 3.3v. This means they are not suitable for use as general purpose IO where a pull-up is not required.

You can verify the address of connected I2C peripherals with a simple one-liner:

sudo apt-get install i2c-tools
sudo i2cdetect -y 1
You can then access I2C from Python using the smbus library:

import smbus
DEVICE_BUS = 1
DEVICE_ADDR = 0x15
bus = smbus.SMBus(DEVICE_BUS)
bus.write_byte_data(DEVICE_ADDR, 0x00, 0x01)	

Pins 27 and 28 (ID_SD (EEPROM SDA2) and ID_SC (EEPROM SCL2)) are also I2C. There are used by the Pi for internal functions, and also some HAT boards. 		

- RevEng communication -

This communication is similar to the SPI, but it uses only two wire for communication – SDA/SCL. Each device is accessed by using their internal i2c address. Here we will use an I2C EEPROM as an example and see how we can read and write to the memory. i2ctools comes as a part of the Linux package, so no need to install anything.

To find the address of your i2c slave device.

i2cdetect -y 1

Now use a tool called as eeprog to read and write to the EEPROM.

wget http://darkswarm.org/eeprog-0.7.6-tear5.tar.gz
tar -xvf eeprog-0.7.6-tear5.tar.gz eeprog-0.7.6-tear12/
cd eeprog-0.7.6-tear12/
make
sudo make install

To write data to it:
echo “hello” | ./eeprog -f -16 -w 0 -t 5 /dev/i2c-1 0x50
-w is the offset
-t is write delay

To read data from it
./eeprog  -x /dev/i2c-1 0x50 -16  -r 0x00:0x10