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
3
votes
1 answer

How do I send multiple 32 byte strings to I2C master?

I have multiple variable length strings which do not exceed 32 bytes each which need to be sent via I2C between 2 Arduino Nanos. I use # to terminate each string. I can successfully send and receive the first string, but for the life of me I can't…
Roger McClurg
  • 51
  • 1
  • 5
3
votes
0 answers

difference between struct i2c_driver vs struct platform_driver in linux kernel?

I'm trying to understand audio codec, while going through different audio codec source code i came across register of codec in two different ways struct platform_driver and struct i2c_driver. In audio codec aic3101 module_i2c_driver is used for…
GBiradar
  • 161
  • 1
  • 11
3
votes
0 answers

IOError: Errno 110 Connection timed out

I've try to connect a Heart Rate Sensor with raspberry pi Zero. to do it, I use the I2C port communication. I have a sensor in 0x50 adress and work correctly, but when I did a loop for a long time, the error appear (110). For example, I put a while…
3
votes
3 answers

I2C & SPI driver

Recently, I have been writing a camera driver use I2C interface read/write registers, and use spi interface to write firmware data to it. So my question is how to obtain a spi_device pointer in the I2C driver? Thanks.
cloundliu
  • 31
  • 3
3
votes
1 answer

Linux's i2c dev interface with multiple processes

I've written a quick userspace program to access i2c devices using the i2c dev interface described here: https://www.kernel.org/doc/Documentation/i2c/dev-interface The problem is that I'm not sure how to make this multi-process and multi-thread…
Splaty
  • 604
  • 1
  • 8
  • 21
3
votes
1 answer

Issue with Raspberry pi 3.0 i2c SMBus

I am new to the raspberry pi and I have been working through some i2c examples. I am receiving an IO Error: [Errno 22]Invalid argument when I run the code below: import smbus bus = smbus.SMBus(1) addr = 0xa0 data = 0x20 bus.write_byte(addr,…
J.Hand
  • 31
  • 2
3
votes
2 answers

arudino web server too slow - possibly use queue?

I have implemented a basic light controller for Livolo light switches using an ESP8266 running a web server and a library i found for the livolo switches that uses an RF433 transmitter. i dont have any problem with the code as such.. (and i can post…
Paul O'Brien
  • 171
  • 1
  • 11
3
votes
1 answer

Linux, ARM: Why is gpiochip only created if I2C GPIO Expander is present at boot?

Using Linux 3.14.52 on a imx6sx hardware platform (NXP embedded ARM). The problem is that PCF8575 I2C GPIO expanders specified in the device tree are not instantiated as devices in the /sys/class/gpio structure unless they are present during kernel…
Bruce
  • 33
  • 4
3
votes
2 answers

I2C-dev: Change write and read parameters

I'm currently working on I2C between a Raspberry Pi 3 and a PsoC 4. I'm using the I2C-dev library to handle the I2C communication. So the I2C bus is up and running, and the read and write function has been implemented correctly. I want however to…
3
votes
1 answer

I2C not working in Raspbian

I have a RaspberryPi3 connected to an Adafruit MPL3115A2 over the I2C bus. I have the latest version of Raspbian Jessie downloaded today. The only thing I have done is run sudo rasp-config to enable the i2c bus then reboot and install the i2ctools…
user3344434
3
votes
2 answers

What's the correct way to access I2C devices in a multi-threaded app, in Window 10 IoT Core?

Does anyone have an example of using multiple I2C devices in multiple threads? I'm having a bit of a problem with mine, it's one of those where if I single step it everything works and if I run it full sped everything messes up - clearly a race…
Tim Long
  • 13,508
  • 19
  • 79
  • 147
3
votes
1 answer

why repeated start based i2c operation are not supported in linux?

I want to read from i2c slave which need multi start operation to read its register values. As up-to some extent I have traced I2C driver in Linux kernel 3.18.21, I found it do not support multi start operation and I have no way to read from this…
coder007
  • 317
  • 1
  • 5
  • 12
3
votes
1 answer

Windows IoT and DS3231 RTC clock

For my project I need the current time & date. Unfortunately the RP2 loses everything when it's turned off. The next thing is, that I won't have an internet connection to use NTP. For this reasing I need to implement a DS3231 RTC module. The…
TRS
  • 228
  • 4
  • 15
3
votes
1 answer

Embedded software program block, I2C?

I have experienced a very strange problem in developing my application for the ST Microelectronics iNemo. My applications consists in: Gyroscope reading with SPI Accelerometer and Magnetometer (in the same device) reading with I2C Attitude…
Daniel
  • 141
  • 3
  • 14
3
votes
1 answer

How to access a registered I2C device in the linux kernel from userspace

I want to be able to modify registers in a I2C slave device. The device has a driver in the kernel, and the driver registers an I2C client with the address of it. The driver is very basic and does output all the device functionality. I want to…
Arnon Charas
  • 108
  • 8