Practical Bench / Embedded & Edge AI · Intermediate · 60 min
I2C bus under the microscope with a logic analyser
Scan an I2C bus, read a register from an MPU6050, capture the traffic with a logic analyser, decode it and count the clock cycles in a real transaction.
Why it matters
Every sensor in a phone, car or medical device talks over a bus such as I2C, SPI or UART. Debugging them means looking at the wires, not guessing.
Toy or everyday hack
A low-cost 8-channel USB logic analyser with free PulseView software replaces a costly oscilloscope here
Safety
- Connect the analyser ground to the board ground first. Never connect probes to mains-powered circuits.
- Match the logic level (3.3 V or 5 V) between board and sensor: check the sensor board specification.
Parts list
| Part | Qty | Note |
|---|---|---|
| ESP32 DevKit or Arduino Uno | 1 | |
| MPU6050 breakout board (I2C) | 1 | Has pull-up resistors on board |
| USB logic analyser (8 ch, 24 MHz class) | 1 | Optional but strongly recommended |
| Jumper wires | 6 |
Tools and equipment
Connections
| From | To | Note |
|---|---|---|
| MPU6050 VCC / GND | 3V3 / GND | |
| MPU6050 SDA | ESP32 GPIO21 (Uno: A4) | |
| MPU6050 SCL | ESP32 GPIO22 (Uno: A5) | |
| Analyser ch0 / ch1 | SDA / SCL | Analyser GND to board GND |
What you should see
The measured SCL frequency is close to the value you set (a little lower is common). A register read is START, 8 address bits plus ACK, 8 register bits plus ACK, repeated START, 8 address bits plus ACK, 8 data bits plus NACK, STOP: 36 clock cycles. Going from 100 to 400 kHz makes the transaction about four times shorter.
Step by step
- Wire the MPU6050 and upload the sketch. The Serial Monitor should list a device at 0x68 and print WHO_AM_I = 0x68.
- Start a capture in PulseView: 2 MHz or faster, channel 0 = SDA, channel 1 = SCL, trigger on a falling edge of SDA.
- Add the I2C decoder. Identify START, the address byte (0x68 plus the write bit), ACK, the register 0x75, a repeated START, the read address, the data byte and STOP.
- Measure the SCL frequency from the capture and the duration of one complete transaction. Enter them in the logbook for 100 kHz.
- Change Wire.setClock to 400000, upload again and repeat. Enter the second line.
- The logbook counts the clock cycles in one transaction. A one-byte register read should use about 36.
- Optional: remove the pull-ups (or add 10 kΩ pull-ups on a bare bus) and observe the slower rising edge.
Challenge: Read the 6 accelerometer bytes in one burst and compare the transaction time with six separate one-byte reads.
Code (ESP32 DevKit (or Arduino Uno), Arduino IDE)
Check pin numbers against your board. Test the sketch on your own hardware before a class demo.
// Practical 8 - I2C scan + WHO_AM_I read (ESP32 or Uno). Capture SDA/SCL with a logic analyser.
#include <Wire.h>
const uint8_t MPU = 0x68; // MPU6050 address (AD0 low)
void setup() {
Serial.begin(115200);
Wire.begin(); // ESP32: SDA=21 SCL=22 Uno: SDA=A4 SCL=A5
Wire.setClock(100000); // Part A = 100 kHz. Part B: change to 400000
Serial.println("Scanning I2C bus...");
for (uint8_t a = 1; a < 127; a++) {
Wire.beginTransmission(a);
if (Wire.endTransmission() == 0) { Serial.print("Found device at 0x"); Serial.println(a, HEX); }
}
}
void loop() {
Wire.beginTransmission(MPU);
Wire.write(0x75); // WHO_AM_I register
Wire.endTransmission(false); // repeated START, no STOP
Wire.requestFrom(MPU, (uint8_t)1);
if (Wire.available()) { Serial.print("WHO_AM_I = 0x"); Serial.println(Wire.read(), HEX); }
delay(500);
}Readings logbook
Type your measured values. Calculated columns and the analysis update as you type, and your entries are saved in this browser.
If it does not work
| Symptom | Likely cause and fix |
|---|---|
| No device found | SDA and SCL swapped, no power, or wrong voltage. Check with a multimeter, then swap SDA and SCL. |
| WHO_AM_I is not 0x68 | Your board is a clone or a different chip (MPU6500 and similar return other IDs). The bus is working; check the datasheet of your chip. |
| Waveform has rounded edges | Pull-ups too weak or wires too long for the bus speed. Use 2.2 to 4.7 kΩ at 400 kHz. |
| Analyser shows noise | Missing common ground, or sample rate too low (use at least 4 times the bus clock). |