Questions tagged [i2c]

I2C is a two-wire serial bus. It is used to interface with low-speed peripherals in embedded systems and computer motherboards.

Use this tag when asking questions concerning the I2C bus or SMBus, which is a more strictly defined subset of I2C.

Devices you can communicate with using I2C might include the temperature and voltage sensors on your motherboard. In embedded systems, a vast amount of devices ranging from memory chips to camera modules use I2C for control and data transfer.

I2C bus consists of two signals: SCL and SDA. SCL is the clock signal, and SDA is the data signal.

I2C connection schematics

The clock signal is always generated by the current bus master; some slave devices may force the clock low at times to delay the master sending more data (or to require more time to prepare data before the master attempts to clock it out). The common clock frequency of I2C bus is 100KHz (100Kbps) and 400KHz (400 Kbps). There are high speed versions with clock frequency at or greater than 1MHz (1Mbps) available which is product specific by the semiconductor manufacturers.

The bus is a multi-master bus, which means that any number of master nodes can be present. Additionally, master and slave roles may be changed between messages (after a STOP is sent).

At any given time only the master will be able to initiate the communication. Since there is more than one slave in the bus, the master has to refer to each slave using a different address. When addressed only the slave with that particular address will reply back with the information while the others keep quit. This way we can use the same bus to communicate with multiple devices.

The voltage levels of I2C are not predefined. I2C communication is flexible, means the device which is powered by 5v volt, can use 5v for I2C and the 3.3v devices can use 3v for I2C communication. A 5V I2C bus can’t be connected with 3.3V device. In this case voltage shifters are used to match the voltage levels between two I2C buses.

There are some set of conditions which frame a transaction. Initialization of transmission begins with a falling edge of SDA, which is defined as ‘START’ condition in below diagram where master leaves SCL high while setting SDA low. After this all devices on the same bus go into listening mode.

In the same manner, rising edge of SDA stops the transmission which is shown as ‘STOP’ condition in above diagram, where the master leaves SCL high and also releases SDA to go HIGH. So rising edge of SDA stops the transmission.

I2C conditions

With I2C, data is transferred in messages. Messages are broken up into frames of data. Each message has an address frame that contains the binary address of the slave, and one or more data frames that contain the data being transmitted. The message also includes start and stop conditions, read/write bits, and ACK/NACK bits between each data frame:

I2C message format

Address Frame: A 7 or 10 bit sequence unique to each slave that identifies the slave when the master wants to talk to it.

Read/Write Bit: A single bit specifying whether the master is sending data to the slave (low voltage level) or requesting data from it (high voltage level).

ACK/NACK Bit: Each frame in a message is followed by an acknowledge/no-acknowledge bit. If an address frame or data frame was successfully received, an ACK bit is returned to the sender from the receiving device.

More information:

I2C Standards Doc

I2C primer

1748 questions
9
votes
2 answers

Getting IOError: [Errno 121] Remote I/O error with smbus on python (raspberry) while trying to get data over I2C from Arduino

I am facing problems, that pyhton throws me on my raspberry pi 3 sometimes this IOError during starting a script which is requesting data from an Arduino over I2C. Electrical connection is perfect so this is not the issues. Furthermore I also dont…
d s
  • 237
  • 1
  • 2
  • 11
9
votes
2 answers

NACK and ACK responses on I2c bus

My recent project requires the use of i2c communication using a single master with multiple slaves. I know that with each data byte (actual data) sent by master,the slave responds with Nack\Ack(1,0). I am confused that how this Nack and ACK are…
amit kr
  • 117
  • 1
  • 1
  • 7
9
votes
1 answer

SELinux rules for i2c files in sysfs in Android

I created kernel driver as loadable module for one of my I2C devices. The driver creates few sysfs file under I2C corresponding folder (/sys/devices/i2c/i2c-0/0-0008/) using instantiation through new_device file…
DmitryF.
  • 91
  • 1
  • 4
9
votes
1 answer

I2C device linux driver

How to make a character device for i2c device, with open, close, ioctl etc. functions? I was looking for information about it last two weeks and couldn't find anything working. I found some information in Essential Linux Device Drivers, but it was…
mishaskt
  • 241
  • 1
  • 2
  • 9
8
votes
1 answer

iPad accessory communication through UART

We manufacture a new accessory for iPad/iPhone which should transfer commands to the iPad. We like to use UART (through a certain Apple-protocol called Lingo). My research shows that I can only use USB (30PIN Connector) and custom…
Sebastian Roth
  • 11,344
  • 14
  • 61
  • 110
8
votes
2 answers

I2C inside a docker container

I am trying to use the i2c pins on a raspberry pi inside a docker container. I install all my modules using RUN but when I use the CMD to run my python program i get an error that says Trackback (most recent call last): file "test.py", line 124, in…
Duncan C. Henke
  • 145
  • 1
  • 11
8
votes
1 answer

I2C Slave Driver for Beagle Bone Black

I would like to use the I2C bus on the Beagle Bone Black in slave mode. From searching around, the question gets asked in the comment section of random posts, but never answered as to whether it's possible or not. It appears using slave mode I2C…
cstack
  • 395
  • 1
  • 15
7
votes
4 answers

How to recover from I2C bus collision BCLIF?

I posted this a couple of days ago on the Microchip Forum (here) but the only response has been crickets. The I2C code below works most of the time but occasionally on power-up there is a bus collision (BCLIF) and the I2C module is unable to recover…
jacknad
  • 13,483
  • 40
  • 124
  • 194
7
votes
3 answers

How to use Multiple mcp23017 chips with the adafruit mcp23017.h library

I have been experimenting with I2C and the mcp23017 IO expander chip for my arduino ATMega2560 as I would rather use the IO on the arduino its self for other things I am just figuring out how to use the adafruit mcp23017.h library and cant figure…
ChrisPlusPlus
  • 118
  • 1
  • 1
  • 13
7
votes
2 answers

i2cdump works but not i2cget

I am trying to use a BMP180 over i2c on a Raspberry Pi. I detect the chip. I can take a i2cdump, but I cannot read out a single byte. Any idea what I am doing wrong? Print out: $ sudo i2cdetect -y 1 0 1 2 3 4 5 6 7 8 9 a b c d e …
user3769000
  • 139
  • 1
  • 3
  • 9
7
votes
2 answers

I2C write acknowledge polling in Linux Kernel

The lackluster response here made me wonder this. I've been saddled with a device (Analog Devices 525x) that (from the data sheet, pg 16): disables the I2C interface during the internal [EEPROM] write cycle. That doesn't seem fair, as in "I'm…
Jamie
  • 7,075
  • 12
  • 56
  • 86
6
votes
1 answer

How to prevent "error: 'symbol' undeclared here" despite EXPORT_SYMBOL in a Linux kernel module?

I'm embedding some driver into a Linux kernel when I get this error (I'm adding the device in the board file and registering it): error: 'kxtf9_get_slave_descr' undeclared here (not in a function) I located the function above in a driver…
stef
  • 737
  • 4
  • 13
  • 31
6
votes
1 answer

How are the HAL lock and unlock functions used and why?

I'm trying to understand code which is written by another programmer. It used I²C communication to write data on the EEPROM of an STM32 microcontroller. Generally I understood how his code works, but I can't understand why he used the HAL_LOCK and…
user8872869
6
votes
4 answers

How to Read Data from Arduino with Raspberry pi via I2C

I have connected Raspberry pi 2 model B with arduino uno via Bi-Directional Level shifter. Raspberry pi GND ---------- GND Arduino 3.3v ---------- 5v SCL ---------- A5 SDA …
shivam
  • 383
  • 8
  • 22
6
votes
5 answers

Arduino as slave with multiple i2c addresses

I would like to use an Arduino as an i2c slave. But I require that the Arduino acts as multiple devices by registering itself with multiple i2c addresses. This is probably not something one would normally do, but here is my reason for doing it: I…
trojanc
  • 171
  • 1
  • 7