Sub topic 15
Clock Stretching
This chapter assumes you understand Arduino sketches, I2C START and STOP conditions, ACK responses, and the Wire library. Clock stretching is an I2C timing feature used when a target device needs more time to prepare data.
What clock stretching does
An I2C controller normally generates the clock on the SCL line. The controller pulls SCL low, releases it, and lets the pull-up resistor raise it high. The controller then samples SDA and continues the transaction.
A target can delay the next clock pulse by holding SCL low after the controller releases it. This action is called clock stretching. The target releases SCL when it is ready, and the controller continues.
A target might stretch the clock while measuring a sensor value, copying data from a slow memory device, or handling an interrupt. The controller must wait instead of treating the low level as a bus failure.
How the timing works
Clock stretching can occur during a byte transfer. It is especially common before a target sends a response. The controller releases SCL for a high period, but the target keeps the line low. Because I2C uses open-drain signaling, either side can pull the line low safely.
| Bus condition | Meaning |
|---|---|
| SCL rises normally | The target is ready |
| SCL remains low | A target may be stretching the clock |
| SCL remains low too long | Wiring, power, or device failure is also possible |
The controller should apply a timeout. Without one, a disconnected or damaged target can hold the bus low indefinitely.
Using `Wire` with stretching
Most Arduino Wire implementations handle ordinary clock stretching inside the hardware I2C peripheral. Your sketch usually does not need to detect each stretched pulse.
Some Arduino cores provide a timeout API. When available, set a limit so a stuck bus does not block the program forever.
#include <Wire.h>
const uint8_t SENSOR_ADDRESS = 0x40;
void setup() {
Serial.begin(9600);
Wire.begin();
// Supported by many Arduino cores; check your board's Wire documentation.
Wire.setWireTimeout(25000, true); // 25 ms, reset the peripheral on timeout
}
void loop() {
Wire.beginTransmission(SENSOR_ADDRESS);
Wire.write(0x00); // Sensor command or register
byte result = Wire.endTransmission();
if (result != 0) {
Serial.print("I2C write failed: ");
Serial.println(result);
}
delay(1000);
}The timeout value must exceed the target's documented maximum stretch time. A value that is too short can create false failures. A value that is too long makes a real bus fault slow to detect.
Common problems
Some controllers, bridges, and software I2C implementations do not support clock stretching correctly. Check the documentation when connecting a target that depends on it. Also check that every device shares a compatible voltage and that pull-up resistors allow SCL to rise quickly.
If SCL stays low, disconnect devices one at a time. A defective target, a short circuit, or a target that never releases the line can all produce the same symptom.
Exercise
Why should an I2C controller use a timeout when clock stretching is allowed?
Show answer
A target can hold SCL low while it works, but a failed device or wiring fault can hold it low forever. A timeout lets the controller recover or report the fault.