Hello everyone, I am Lin, an embedded software developer with many years of experience. Today, I will share the essential I2C protocol interview questions that you should master, hoping to help you.
Click below to follow my account
Follow me
Introduction: The interviewer asked some I2C questions, and I couldn’t answer them.
As an embedded engineer, is the I2C protocol a “killer question” or a “giveaway question” in interviews?
95% of candidates lose points for not being able to articulate details, but as long as you master the following 10 high-frequency questions, you can not only respond confidently but also impress the interviewer!
1. Basic Timing of I2C
-
Start Condition (START): When SCL is high, SDA transitions from high to low to indicate the start of communication.
-
Stop Condition (STOP): When SCL is high, SDA transitions from low to high to indicate the end of communication.
-
Data Transmission: When SCL is low, SDA can change (preparing data). When SCL is high, SDA must remain stable (data is valid). Data is transmitted with MSB first.
-
Acknowledgment Mechanism (ACK/NACK): After transmitting 8 bits of data, the receiver must provide an acknowledgment bit (ACK). Low level: indicates successful reception; high level: indicates reception failure or end of transmission.
2. Why Does I2C Use Open-Drain Output?
-
Line and Functionality: Open-drain output allows multiple devices to share the same bus without causing conflicts.
-
Level Shifting: Devices in different voltage domains can communicate on the same bus by using pull-up resistors that match the highest voltage.
-
Clock Synchronization: The open-drain structure allows slave devices to extend the clock period by pulling the SCL line low (clock stretching), enabling slower slave devices to synchronize with the master device.
-
Arbitration Mechanism: In a multi-master environment, the open-drain structure allows for lossless arbitration (low level priority).
3. What Are the Communication Rates of I2C?
The I2C protocol defines several standard communication rates:1. Standard Mode: Rate: 100 Kbps, the earliest I2C specification, suitable for most low-speed peripherals.2. Fast Mode: Rate: 400 Kbps, currently the most widely used mode, suitable for most embedded system applications.
3. Fast Mode Plus: Rate: 1 Mbps, enhanced driving capability, suitable for higher-speed application scenarios.
4. High-Speed Mode: Rate: 3.4 Mbps, requires special hardware support, suitable for high-speed data transmission needs.
5. Ultra Fast Mode: Rate: 5 Mbps, unidirectional communication, mainly used for specific application scenarios.
When selecting the rate, consider: bus capacitance, line length, anti-interference requirements, and device compatibility.
4. Why Does I2C Need Pull-Up Resistors?
-
Line and Logic: The I2C bus uses “wired-AND” logic, allowing multiple devices to share the same bus. Pull-up resistors pull the bus to a high level (idle state), and any device can send a signal by pulling the bus low.
-
Open-Drain/Open-Collector Output: I2C devices use open-drain (CMOS) or open-collector (TTL) structures, which can only actively pull the bus low and cannot actively pull it high. Pull-up resistors provide the ability to pull the bus back to high level.
-
Prevent Bus Conflicts: When multiple devices access the bus simultaneously, pull-up resistors combined with open-drain output can prevent short-circuit situations, ensuring bus safety.
-
Define Bus Idle State: Pull-up resistors ensure that the bus remains high when no devices are communicating, which is the idle state defined by the I2C protocol.
The typical value for pull-up resistors is 4.7kΩ, but the specific value needs to be chosen based on the following factors:
-
Bus capacitance
-
Communication rate
-
Number of devices
-
Power supply voltage
5. What Are the I2C Address Formats?
I2C supports the following address formats:
1. 7-bit Address Format (most common): Address range: 0x00-0x7F (128 addresses), with approximately 112 usable addresses (some addresses are reserved). It occupies one byte during transmission: 7-bit address + 1-bit read/write flag (R/W).
2. 10-bit Address Format (extended): Address range: 0x000-0x3FF (1024 addresses). It occupies two bytes during transmission: the first byte: 1111 0XX + R/W (XX are the high 2 bits of the 10-bit address), the second byte: the remaining 8 bits of the address.
3. Reserved Addresses: 0x00: broadcast address, all devices respond; 0x78~0x7F: used for SMBus protocol or special functions.
Note: Users should avoid using reserved addresses.
6. How Does I2C Achieve Multi-Master Communication?
I2C multi-master communication is achieved through the following mechanisms:
1. Bus Arbitration: Multiple masters can attempt to initiate communication simultaneously. By monitoring the SDA line level, a master that attempts to send a high level but detects a low level will lose arbitration, following the low-level priority principle.
2. Clock Synchronization: The SCL output of all masters is open-drain. The slowest clock will control the bus clock frequency, achieving synchronization by monitoring the actual SCL level.
3. Conflict Detection: Each master checks the actual level of the SDA line when sending each bit of data. If the detected level differs from the expected sent level, it will lose arbitration.
7. What Are Clock Synchronization and Clock Stretching in I2C?
Clock Synchronization:
-
The SCL on the I2C bus is the result of “wired-AND” logic from all devices.
-
When any device pulls SCL low, the bus SCL is low.
-
Only when all devices release SCL (high impedance state) will SCL become high.
-
This ensures that the slowest device can keep up with the communication pace.
Clock Stretching:
-
Slave devices can extend the clock period by holding SCL low.
-
This is mainly used when slave devices need more time to process data.
-
The master device must wait until SCL actually goes high before continuing.
-
This is the mechanism by which slave devices control the communication speed in the I2C protocol.
8. Common Issues and Solutions in I2C Communication
-
Bus Deadlock: Symptoms: SDA or SCL is held low by a device. Solutions: Software reset: The master generates 9 clock pulses to attempt to complete the interrupted transmission; Hardware reset: Reset all I2C devices; Power cycling: Turn off and then turn on the power.
-
Address Conflict: Symptoms: Multiple devices use the same address. Solutions: Use devices with address selection pins; Use I2C address converters; Use multi-bus designs.
-
Timing Issues: Symptoms: Data errors during high-speed communication. Solutions: Reduce pull-up resistor values (note increased power consumption); Reduce bus capacitance (shorten line length, reduce the number of devices); Lower the communication rate.
-
Noise Interference: Symptoms: Unstable communication, sporadic errors. Solutions: Use shielded cables; increase filtering capacitors; optimize PCB layout to avoid I2C lines running parallel to high-speed signal lines.
9. Differences Between I2C and SPI
The differences are as follows:
Feature |
I2C |
SPI |
Number of Signal Lines |
2 (SDA + SCL) |
4 (MOSI + MISO + SCK + CS) |
Topology |
Multi-master, multi-slave |
Single-master, multi-slave |
Speed |
Lower (≤3.4MHz) |
Higher (up to 50MHz+) |
Hardware Complexity |
Simple (no chip select lines required) |
Complex (each slave requires CS) |
Applicable Scenarios: |
I2C is suitable for low-speed, multi-device scenarios; | SPI is suitable for high-speed, point-to-point communication. |
10. How to Analyze I2C Timeout Issues
1. Use an oscilloscope/logic analyzer to capture waveforms:
a) Check if the START condition is complete, and whether the rise/fall times of SCL/SDA meet protocol requirements.
b) Confirm whether the slave device returns ACK (is SDA low during the 9th clock cycle).
2. Check pull-up resistors.
Calculate pull-up resistor values based on bus capacitance, typically 4.7kΩ~10kΩ.
3. Verify the slave device address.
Confirm whether the address is 7-bit or 10-bit, and whether it includes the read/write bit.
4. Lower the clock frequency.
Try reducing the master device clock frequency to 100kHz (standard mode) to rule out rate compatibility issues.
5. Perform bus reset operations.
If the bus is deadlocked (SCL is pulled low), the master device can send the 9th clock pulse to force the bus to release.
6. Check the status of the slave device.
Confirm whether the slave device has completed initialization (some sensors may require a power-up reset time).
Finally, if you find this helpful, I hope you can give a thumbs up (share, like, and follow). Your recognition is my motivation to continue producing content, thank you very much!
Feel free to reach out, please mention “public account”.