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

How do I connect an Arduino Uno with Raspberry Pi using I²C

I'm trying to send data through the I²C interface from the Arduino Uno to the Raspberry Pi using I²C. This was the code I used. In Arduino: #include unsigned int watt; unsigned int watt1; byte watt2; byte watt3; void setup() { …
user17151
  • 2,607
  • 3
  • 18
  • 13
3
votes
0 answers

How to use the BH1750 support in Linux kernel?

I'm writing code in Raspberry Pi, trying to communicate BH1750 ambient light sensor connected in a I2C port. Most working ways are using Python SMBus library but I'm writing C++ code. After some search, I found that: The well-known wiringPi library…
jiandingzhe
  • 1,881
  • 15
  • 35
3
votes
1 answer

MPU6050 only outputs 0x00 on I2C with MicroPython

I've been trying to talk to my MPU6050 with a Pi Pico running MicroPython. I can verify the I2C is working because when running i2c.scan(), I get the MPU6050 address(0x68). However, when trying to read a specific register from the MPU6050, I always…
inzig0
  • 51
  • 1
  • 3
3
votes
2 answers

What is the bug in STM32 hardware I2C?

I am from China,I was told that STM32 hardware I2C has bug.So I always use software simulation I2C with two ordinary GPIO Pins. I was wondering what is the bug exactly?Is it a rumor ,or is it true?I hope get opinion from abroad website,because you…
ethan
  • 41
  • 2
3
votes
2 answers

Arduino ESP32 I2C can I set custom pins for SLAVE (Client)

tl;dr can someone tell me if it is possible to set custom pins for an I2C slave (client, peripheral) within the Arduino environment? Context On an ESP32 (ESP32-WROVER from Freenove) I am trying to communicate with 2 devices that are I2C masters…
3
votes
1 answer

Store object instances of PCA9635 in array

I am working with a hardware PWM-LED-Controler called "PCA9635". This chip can be controlled in Arduino with the Library "PCA9635". In the example provided in the library, an ledArray with the i2c address 0x70 is initiated by: #include…
3
votes
0 answers

Jetson Nano Not Detecting Grove Sensors Using Grove Base HAT

I recently got started trying to connect a Grove PIR Motion Sensor & Grove NFC Reader to my Jetson Nano, using the Grove Base HAT: I followed these two guides to get the necessary libraries set up in order to interface with the Grove…
Sam Skinner
  • 376
  • 3
  • 12
3
votes
1 answer

Ada for I2C on the BBC Micro:Bit with the MCP23017

I am trying to set up a very simple example using the ada I2C library with the MCP23017 IO expander on the micro:bit (V1.5) but I can't figure it out. For now, all I want to do is turn on an LED that is connected to a GPIOA pin. I have it working in…
toella
  • 55
  • 4
3
votes
1 answer

Interview question - How to continuosly read a buffer without losing data

I am an embedded engineer, with not much experience. I was at an interview and I was asked a question, which I guess is not new and already has answers: You have a peripheral which is constantly receiving input from external world, the peripheral…
Naveen
  • 73
  • 6
3
votes
1 answer

Is there a python-periphery library i2c code example or detailed explanation other than the documentation?

This is my first question in Stackoverflow if I have made a mistake while writing this question please give me a feedback to correct myself. I want to use i2c communication for my raspberry pi. I want to use python-periphery(I know there is smbus…
Merlin
  • 33
  • 4
3
votes
1 answer

linux driver for an i2c device -- two byte read

I'm trying to write a Linux driver for an I2C device that seems to be slightly different from a typical device. Specifically, I need to read two bytes in a row without sending a stop bit in between, like so: [S] [Slave Addr | 0] [A] [Reg Addr 1] [A]…
Ilya
  • 31
  • 1
  • 6
3
votes
1 answer

How to use I2C GPIO expander's pin instead of RTS to control the RS485 direction, in Linux AUART kernel driver?

I'm creating an embedded system based on i.MX287 processor from NXP(Freescale). I'm using a core processing board which is connected to my evaluation board via a mini PCIe connector. UARTs 0,3,4 are used as RS232 and UARTs 1,2 as RS485. The core…
embedded
  • 51
  • 7
3
votes
1 answer

I2C userspace communication on ArchLinux

I am trying to implement an application that will allow me to access the i2c bus from the user space similar to this https://www.kernel.org/doc/Documentation/i2c/dev-interface. Here it is stated that using smbus commands is preferred over using…
yash
  • 31
  • 3
3
votes
1 answer

Parallel (buffered) readout of multiple I2C devices, python 3

For experimental measurements of human balance, I would like to connect and read out multiple I2C sensors (gyro/mag/accelerometer, such as this one). I have managed to hook them up (libftdi+python3.7) and can read from single registers. However, I…
falkm
  • 31
  • 5
3
votes
0 answers

RaspberryPI I2C do not detect any address

I2C on my Pi(s) is not detecting any slave. I am obviously missing something here, the PI's I2C is "working" but do not detect the device. I did (and redid) everytghing suggested by Sirajo on I2C not detecting ? issues in hardware or any other? The…