Enabling I2C on Raspberry Pi - Time & Space Complexity
When enabling I2C on a Raspberry Pi, we want to understand how the steps involved grow as we add more devices or configurations.
We ask: how does the time to enable and use I2C change with more connected devices?
Analyze the time complexity of the following Raspberry Pi commands to enable I2C.
sudo raspi-config
# Navigate to Interface Options
# Select I2C and enable it
sudo reboot
sudo apt-get install -y i2c-tools
sudo i2cdetect -y 1
This sequence enables the I2C interface and scans for connected I2C devices on the Raspberry Pi.
Look for any repeated actions or scans in the process.
- Primary operation: The device scan command
i2cdetectchecks each possible address on the I2C bus. - How many times: It checks all 127 possible addresses one by one.
The scan checks every address regardless of how many devices are connected.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 addresses | 10 checks |
| 100 addresses | 100 checks |
| 127 addresses (full scan) | 127 checks |
Pattern observation: The number of checks grows directly with the number of addresses scanned.
Time Complexity: O(n)
This means the time to scan grows in a straight line as the number of addresses to check increases.
[X] Wrong: "The scan time depends on how many devices are connected."
[OK] Correct: The scan checks every possible address, not just the ones with devices, so time depends on address range, not device count.
Understanding how scanning operations scale helps you reason about device communication and performance in embedded systems.
What if we changed the scan to only check known device addresses? How would the time complexity change?
