## Synchronous Hardware Buses: Communicating with Integrated Circuits
As projects grow in complexity, connecting multi-sensor breakout boards using separate analog or digital pins for every signal rapidly exhausts available microcontroller I/O pins.
To solve this pin scarcity, the semiconductor industry established two universal synchronous serial communication standards: I2C (Inter-Integrated Circuit) and SPI (Serial Peripheral Interface).
1. I2C vs SPI vs UART Architecture
+---------------------------------------------------------------------------------+
| SERIAL BUS ARCHITECTURAL COMPARISON |
+-------------------+-----------------------+--------------------+----------------+
| Parameter | UART (Async Serial) | I2C (Two-Wire Bus) | SPI (4-Wire) |
+-------------------+-----------------------+--------------------+----------------+
| **Wire Count** | 2 wires (TX, RX) | 2 wires (SDA, SCL) | 4 wires (MOSI, |
| | | | MISO, SCK, CS) |
+-------------------+-----------------------+--------------------+----------------+
| **Clock Line** | None (Agreed baud) | Shared Clock (SCL) | Shared (SCK) |
+-------------------+-----------------------+--------------------+----------------+
| **Speed** | Up to 115.2 kbps | 100 kHz - 400 kHz | 1 MHz - 10 MHz+|
+-------------------+-----------------------+--------------------+----------------+
| **Multi-Device?** | Point-to-point only | Multi-Drop (127 IDs| Multi-Drop |
| | (2 devices) | on same 2 wires) | (Needs 1 CS pin|
| | | | per device) |
+-------------------+-----------------------+--------------------+----------------+
| **Duplex** | Full Duplex | Half Duplex | Full Duplex |
+-------------------+-----------------------+--------------------+----------------+2. How the I2C Bus Operates (Wire Library)
The I2C bus utilizes two open-collector/open-drain lines pulled high to 5V via 4.7k ohm resistors:
- SDA (Serial Data): Bi-directional data line on Pin A4.
- SCL (Serial Clock): Clock pulses driven by the Master on Pin A5.
+5V VCC
|
+-----------+-----------+
| (4.7k Pull-up) | (4.7k Pull-up)
| |
SDA: -----+-----------------------+----------+--------------------+
| | |
SCL: -----|-----------------------+----------|--------------------+
| | | |
+---------------+ +---------------+ | +---------------+
| Master (MCU) | | OLED Display | | | Temp Sensor |
| (Arduino Uno) | | Address: 0x3C | | | Address: 0x68 |
+---------------+ +---------------+ + +---------------+Addressing Mechanism
Every I2C slave component has a hard-coded 7-bit hexadecimal address (e.g., 0x3C for OLED displays, 0x68 for MPU6050 accelerometers). When the Master transmits 0x3C, only the OLED acknowledges; all other devices ignore subsequent data bytes.
3. Precision Sensor Telemetry: HC-SR04 Ultrasonic Ranging
The HC-SR04 ultrasonic distance sensor measures the round-trip flight time of a 40 kHz acoustic sound burst:
[ HC-SR04 SENSOR ] ===== (40 kHz Sonic Pulse: 343 m/s) =====> [ OBSTACLE ]
<===== (Echo Reflection Returned) <=========Distance Calculation Physics
The speed of sound in air at room temperature (20^circC) is:
Complete Multi-Sensor Telemetry Implementation
// =============================================================================
// ULTRASONIC RANGEFINDER & I2C TELEMETRY
// =============================================================================
#include <Wire.h>
const uint8_t TRIG_PIN = 9;
const uint8_t ECHO_PIN = 10;
void setup() {
Serial.begin(115200);
Wire.begin(); // Join I2C bus as Master
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
digitalWrite(TRIG_PIN, LOW);
}
void loop() {
// 1. Send clean 10-microsecond trigger pulse
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
// 2. Measure high pulse duration on Echo pin in microseconds
// Timeout after 30,000 microseconds (~5 meters max range)
uint32_t duration = pulseIn(ECHO_PIN, HIGH, 30000);
// 3. Convert duration to distance in centimeters
float distanceCm = duration / 58.2;
if (duration == 0) {
Serial.println(F("[SONAR] Out of range (> 500 cm)"));
} else {
Serial.print(F("[SONAR] Obstacle detected at: "));
Serial.print(distanceCm, 1);
Serial.println(F(" cm"));
}
delay(100); // 10 Hz sampling rate
}