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

Why STM32 I2C slave is returning unwanted NACK or indefinitely clock stretching?

I am trying to write an STM32 as an I2C slave device with a simple interface that works like this: So master will always send a registered address every time, then either write or read from that register address. The slave then needs to always…
4
votes
3 answers

Copy larger size variable unit16 to equivalent smaller array size unint8 by casting in c

I have variable uint16_t value, I want to copy it to uint8_t buffer[3]. Is it possible to do (Little endian): *buffer=*(uint8_t *)&value; Instead of: buffer[0] = highByte(value); buffer[1] = lowByte(value); Since this replacement cause stm32f7 I2C…
mohammadsdtmnd
  • 330
  • 1
  • 11
4
votes
2 answers

I2C: How to write to a 16-bit register address using i2cset

Trying to write a byte to a 16-bit address on an I2C device (OV5640 camera) using i2cset. I can successfully read from the device registers using the following sequence (read byte from device address 0x3c and register address 0x3030): i2cset -v -f…
JBG
  • 81
  • 1
  • 4
4
votes
4 answers

Raspberry Pi 4 I2C bus not working correctly

the I2C Bus on my Raspberry Pi 4 Model B is not working altough i2cdetect does detect targets. I tried to use the Raspberry Pi Sense HAT and the SSD1306 OLED display and the PCA9685 Servo Driver with the I2C Bus. Everything works fine. I don't know…
Micha93
  • 628
  • 1
  • 9
  • 22
4
votes
1 answer

Raspberry PI - I2C not writing data

I'm trying the new System.Device.Gpio library in .NET Core (https://github.com/dotnet/iot) with my Raspberry PI 3B+, but the I2C communication is giving me some trouble. I want to reproduce the behavior of some python code that is already working,…
4
votes
1 answer

Read a block of data from a specific register(fifo) using c/c++ and i2c in raspberry Pi

I need to read 4 bytes of data from MAX30100 chip using c/c++ and I2C on raspberry Pi. I tried doing it with python. And it worked. But the problem was the data rate is too slow, I need to update the data at least 250 times/sec, which means the…
Ting Wang
  • 221
  • 3
  • 13
4
votes
1 answer

Building a i2c device controller

I am building a PCB that will communicate via a i2c bus with a UDOO x86 running ubuntu 18.04 with two accesible i2c buses and multiple GPIO the device has 3 i2c chips on it i2c-mux-pca954x PCA9543 - level shifting bus switch SC18IS602B - i2c…
fireblade
  • 39
  • 7
4
votes
1 answer

What is General Call Address and what is the purpose of it in I2C?

I wonder what is General Call Address in I2C (0x00). If we have a master and some slaves can we communicate with these slaves through our master with this address?
4
votes
2 answers

USB to GPIO/SPI/I2C/UART for regular Windows 10 PC? Simular to Raspberry Pi 40 pin Header

Does anybody know of a company that sells a USB cable for a normal non-IoT windows 10 PC that can converts a USB to all of the following buses on 40 pin Raspberry pi like header? Example: USB->GPIO,I2C,SPI,UART, etc.. The closest thing I can find…
Bimo
  • 5,987
  • 2
  • 39
  • 61
4
votes
4 answers

How do I dim the backlight on an 20x4 LCD Display with the i2c connected on an arduino uno

How to dim the backlight on an 20x4 LCD Display with the i2c connected to an Arduino uno? I use the LiquidCrystal_I2C.h library and I can turn on and of the light with lcd.backlight(); and lcd.noBacklight(); But I don't want to turn off completely…
Xasevu
  • 51
  • 1
  • 1
  • 4
4
votes
1 answer

SMBUS on the RPI gives IOError: [Errno 121] Remote I/O error

I have tried communication between RPi3 and stm32 over I2C. First of all I have installed i2c-tools and python-smbus. All in All I have used python script on the RPI as below: import smbus bus = smbus.SMBus(1) address = 0x0A data =…
J. Doe
  • 77
  • 1
  • 1
  • 8
4
votes
3 answers

ESP8266 to ESP8266 i2C Communication

I am trying to get my ESP8266's connect and send messages over an i2c bus. I am using a NodeMcu Development Board. Pins D1,D2 and GND are connected to each other. The code on my master is : #include void setup() { Wire.begin(D1,D2); //…
user3599285
  • 91
  • 1
  • 7
4
votes
2 answers

Sending a JSON String via I2C between Arduino and RPi IOT C#

I'm trying to send a JSON String via I2C from an Arduino Uno to a RaspPi running Win IOT Core. The connection works fine, I've registered an event handler on the Arduino side which is called fine when the master (rpi) requests data. void…
Peter Kozak
  • 63
  • 2
  • 5
4
votes
1 answer

Writing kernel module(s) for a multi-function i2c device - one or many?

I have an i2c device, built into the board I am developing for. It responds to a single address, but the first byte (sometimes called "command" or "register") dictates the function addressed. I can use the device from userspace with…
fadedbee
  • 42,671
  • 44
  • 178
  • 308
4
votes
1 answer

Raspberry Pi i2c Read/Write Error

Like many people, I've had a Pi for a while but never really done anything with it. I've finally got round to hooking up an MPU6050 IMU to play around with. It uses i2c for communication so I followed the guide by Adafruit regarding enabling i2c…
thetree
  • 326
  • 1
  • 2
  • 11