Lesson 6 / 8
Module 3Lesson 6⏱ 50 min study

Serial Communication Buses: I2C (Wire), SPI & Sensor Telemetry

Deconstruct synchronous serial communications, compare I2C two-wire addressing vs high-speed SPI bus architectures, and interface complex environmental sensor suites.

✦ Key Architectural Takeaways
  • •Asynchronous serial (UART) requires matching baud rate clocks; synchronous buses (I2C, SPI) share a dedicated clock line, eliminating timing drift
  • •I2C uses two bi-directional open-drain lines (SDA and SCL) with pull-up resistors, supporting up to 127 addressed devices on the same two wires
  • •SPI uses four lines (MOSI, MISO, SCK, CS) for full-duplex communication at speeds exceeding 8 MHz, selecting target peripherals via dedicated Chip Select lines
  • •Interfacing ultrasonic sensors (HC-SR04) requires high-precision microsecond timing (pulseIn) to calculate round-trip sound wave propagation

## 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

TEXTSYSTEM SCHEMATIC
  +---------------------------------------------------------------------------------+
  | 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.
TEXTSYSTEM SCHEMATIC
                     +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:

TEXTSYSTEM SCHEMATIC
  [ 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:

∑Mathematical Formulation & Principle
THEORETICAL MODEL
v = 343 meters per second = 0.0343 centimeters per microsecond
∑Mathematical Formulation & Principle
THEORETICAL MODEL
ext{Distance (cm)} = (Time elapsed (mus) imes 0.0343 / 2)
∑Mathematical Formulation & Principle
THEORETICAL MODEL
ext{Distance (cm)} = (Time elapsed (mus) / 58.2)

Complete Multi-Sensor Telemetry Implementation

CPPSYSTEM SCHEMATIC
// =============================================================================
// 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
}