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
5
votes
2 answers

I2C not detecting ? issues in hardware or any other?

I have been working through some i2c examples. Plugging it all together and I find that I need to install the i2c-tools package, then use raspi-config to enable the I2C system. The wiringPi gpio command has a shortcut to the i2cdetect command and…
Jerwin Prabu
  • 336
  • 1
  • 3
  • 16
5
votes
0 answers

Python - HowTo unittest i2c device wrapper class

I have a python class that abstracts a dedicated device (power sensor) connected over i2c. I use the python-smbus module for the I2c access. Within that class, of course, I have methods (i.e. current_ma that returns the current value in milli…
5
votes
1 answer

Raspberry Pi as Slave in I2C and arduino as master

I am trying to make write a code where i run my main program in arduino and take data from i2c bus from raspberry pi when required. Thus i need to configure my arduino as I2C Master and raspberry pi as I2C slave. Is it possible to do it in the same…
5
votes
1 answer

STM Nucleo I2C not sending all data

Edit: The solution was to turn down the I2C clock in the initialization block. Although the STM could handle it, the data sheet for the LCD stated it could handle only up to 10kHz. For the DMA there is an IRQ that must be enabled and setup in the…
5
votes
2 answers

Access device's register i2c

I recently bought a gy-521 board and I'm trying to use it with a Raspberry Pi 3 through the following connections RPi3 | GY-521 --------------------- 3.3V <-------> Vcc GND <-------> GND SCL <-------> SCL SDA <-------> SDA Using…
igng
  • 372
  • 1
  • 6
  • 17
5
votes
1 answer

How do I use the STM32CUBEF4 HAL library to read out the sensor data with i2c?

I want to use the latest HAL library instead of Standard Peripheral Library. And i want to readout the BMA250E G-sensor's chip_id, but it doesn't work. Value of aRxBuffer always keep at 0x00. But it should be 0xf9! What's wrong in my…
elecbuggy
  • 63
  • 1
  • 1
  • 5
5
votes
0 answers

Unit Test for I2C Driver without Hardware - How to interpret the correct value from slave?

I have a i2c driver which writes to the i2c Bus: The register address of the slave (temperature sensor) The value which have to be written into this address Also I perform a check (read back) of the value to ensure it is the correct one.…
JohnDoe
  • 825
  • 1
  • 13
  • 31
5
votes
1 answer

I2C onReceive-handler called only once

I'm having trouble communicating between Arduino's over I2C. For some reason, the onReceive handler is only called once. Master Code (sender): #include #include…
JorenHeit
  • 3,877
  • 2
  • 22
  • 28
5
votes
0 answers

Implementing I2C slave with FT201XB via USB

I've been trying to test a FT201XQ USB-I2C breakout board: UMFT201XB-01, so I can connect it to a master device such as an Arduino and sniff what that device is sending through I2C. To see the output of the slave device I have successfully…
5
votes
1 answer

usage of driver_data member of I2C device id table

I am trying to understand I2C client drivers. As per my understanding before registering I2C driver we have to define i2c_device_id table and device tree compatible table. I have following doubts. Could please help me to understand. 1) The…
user3693586
  • 1,227
  • 5
  • 18
  • 40
5
votes
2 answers

Is forcing I2C communication safe?

For a project I'm working on I have to talk to a multi-function chip via I2C. I can do this from linux user-space via the I2C /dev/i2c-1 interface. However, It seems that a driver is talking to the same chip at the same time. This results in my…
Nils Pipenbrinck
  • 83,631
  • 31
  • 151
  • 221
5
votes
4 answers

Using Python smbus on a Raspberry Pi - confused with syntax

I am trying to use python-smbus on a Raspberry Pi to communicate with an MMA7660 accelerometer chip using I2C. In the code below, I am reading registers 0x00, 0x01, 0x02 and 0x03 of the chip, and I am getting the exact same values for all. Looking…
M-V
  • 5,167
  • 7
  • 52
  • 55
5
votes
2 answers

Setting up i2c on Raspberry Pi for C?

I'm setting up i2c for an Application written in C, but I've not been able to find any how-to's for it. I'm running a model-b Raspberry Pi on Debian 6 with LXDE. Can anybody show me how to set up i2c so I can use it in a C Application? Thanks for…
sharksfan98
  • 547
  • 3
  • 9
  • 19
5
votes
2 answers

best common practice I2C register map

Just wondering what the best practice regarding I²C register maps in C or rather what other people use often/prefer. Up to this point, I have usually done lots of defines, one for every register and one for all the bits, masks, shifts etc. However,…
FRob
  • 3,883
  • 2
  • 27
  • 40
4
votes
4 answers

How to properly initialize I2C stm32?

I want to get data from ADXL345 accelerometer,but seems that I incorrectly connect it. SCL- PC6(with 10k resistor) SDA- PC7(with 10k resistor) SDO- GND CS - VCC GND - GND 3.3v - VCC Here is my code to initalize: void I2CG_Init(void) { …
SevenDays
  • 3,718
  • 10
  • 44
  • 71