Cheatography
https://cheatography.com
I2C cheatsheet for Python and Linux
This is a draft cheat sheet. It is a work in progress and is not finished yet.
Basics
Slaves have addresses by which they respond, address might be configurable |
Master requests data from slaves or sends data to them, slaves answer |
SDA/SCL wired in parallel, each connected to 3.3V through a resistor (called pullup resistor) |
Resistor is needed because lines can only be made low, neither master nor slave can make a line high |
No need to use Python GPIO functions because there's a separate Raspberry Pi block able to speak I2C which we can use to communicate in I2C language |
|
|
Usage
Communication between microcontrollers |
Getting data from sensors |
Reading/writing memory chips |
Adding GPIO to your project |
Configuring I2C-enabled devices |
i2cdetect
Call "i2cdetect -y bus_num" in command line to scan for devices:
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- 27 -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- UU
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --
Hexadecimal number means that device responded. UU means this address is taken by the Linux kernel.
|
I2C buses and bus numbers
Do "ls /dev/i2c*" in command-line to list available I2C buses (separate I2C lines, Raspberry Pi has 1 available). /dev/i2c-1 means bus number 1.
Most likely, bus number will be 1, but check anyway.
|
CLI utilities for reading&writing I2C
#Read a byte from I2C device:
i2cget -y bus_num dev_addr register # If unsure, leave register at 0
#Write a byte to I2C device:
i2cget -y bus_num dev_addr register byte_value
#Dump all registers of I2C device:
!!!!!!!!!!!i2cfump -y bus_num dev_addr register byte_value
|
Debugging problems
Check that the I2C driver has been loaded: "sudo modprobe i2c-dev". |
|