i2cBusScanner = {
// Common I2C sensors database
const i2cSensorDatabase = [
{ name: "BMP280 (Pressure)", addr: 0x76, altAddr: 0x77, category: "Environmental" },
{ name: "BME280 (Pressure/Humidity)", addr: 0x76, altAddr: 0x77, category: "Environmental" },
{ name: "MPU6050 (IMU)", addr: 0x68, altAddr: 0x69, category: "Motion" },
{ name: "ADXL345 (Accelerometer)", addr: 0x53, altAddr: 0x1D, category: "Motion" },
{ name: "BH1750 (Light)", addr: 0x23, altAddr: 0x5C, category: "Light" },
{ name: "SSD1306 (OLED Display)", addr: 0x3C, altAddr: 0x3D, category: "Display" },
{ name: "DS3231 (RTC)", addr: 0x68, altAddr: null, category: "Time" },
{ name: "PCF8574 (GPIO Expander)", addr: 0x20, altAddr: 0x27, category: "I/O" },
{ name: "ADS1115 (ADC)", addr: 0x48, altAddr: 0x49, category: "ADC" },
{ name: "INA219 (Current Sensor)", addr: 0x40, altAddr: 0x41, category: "Power" },
{ name: "SHT31 (Humidity)", addr: 0x44, altAddr: 0x45, category: "Environmental" },
{ name: "VL53L0X (Distance)", addr: 0x29, altAddr: null, category: "Distance" },
{ name: "MCP4725 (DAC)", addr: 0x60, altAddr: 0x61, category: "DAC" },
{ name: "PCA9685 (PWM Driver)", addr: 0x40, altAddr: 0x41, category: "Motor" }
];
const selectedSensor = Inputs.select(i2cSensorDatabase.map(s => s.name), { label: "Add Sensor to Bus:", value: "BMP280 (Pressure)" });
const useAltAddress = Inputs.toggle({ label: "Use alternate address", value: false });
const addSensorBtn = html`<button type="button">Add to Bus</button>`;
const clearBusBtn = html`<button type="button">Clear Bus</button>`;
const scanBusBtn = html`<button type="button" style="background: #16A085; color: white; font-weight: bold; padding: 8px 16px;">Scan I2C Bus</button>`;
let busDevices = [];
let scanResults = null;
let scanInProgress = false;
let currentScanAddr = 0;
let scanLog = [];
const output = html`<div aria-live="polite"></div>`;
const widget = html`<div class="i2c-bus-scanner">${selectedSensor}${useAltAddress}<div style="display: flex; flex-wrap: wrap; gap: 8px;">${addSensorBtn}${clearBusBtn}${scanBusBtn}</div>${output}</div>`;
function render() {
// Visual I2C Bus Display
const bus = html`
<div style="background: linear-gradient(135deg, #1a1a2e 0%, #16213e 100%); border-radius: 12px; padding: 20px; margin: 15px 0; font-family: 'Courier New', monospace;">
<!-- Bus Lines Header -->
<div style="display: flex; flex-wrap: wrap; gap: 10px; justify-content: space-between; align-items: center; margin-bottom: 20px;">
<div style="color: #16A085; font-weight: bold; font-size: 16px;">I2C Bus</div>
<div style="display: flex; gap: 20px;">
<div style="display: flex; align-items: center; gap: 5px;">
<div style="width: 40px; height: 4px; background: #E67E22; border-radius: 2px;"></div>
<span style="color: #E67E22; font-size: 12px;">SDA (Data)</span>
</div>
<div style="display: flex; align-items: center; gap: 5px;">
<div style="width: 40px; height: 4px; background: #3498DB; border-radius: 2px;"></div>
<span style="color: #3498DB; font-size: 12px;">SCL (Clock)</span>
</div>
</div>
</div>
<!-- Bus Lines -->
<div style="overflow-x: auto;"><div style="position: relative; height: 60px; margin: 20px 0; min-width: ${Math.max(320, 120 + busDevices.length * 100)}px;">
<div style="position: absolute; left: 0; right: 0; top: 15px; height: 4px; background: #E67E22; border-radius: 2px;"></div>
<div style="position: absolute; left: 0; right: 0; top: 40px; height: 4px; background: #3498DB; border-radius: 2px;"></div>
<!-- Master Controller -->
<div style="position: absolute; left: 10px; top: -20px; background: #2C3E50; border: 2px solid #16A085; border-radius: 8px; padding: 8px 12px; color: white; font-size: 11px; text-align: center;">
<div style="font-weight: bold;">ESP32</div>
<div style="color: #16A085; font-size: 10px;">Master</div>
</div>
<!-- Connected Devices -->
${busDevices.map((device, i) => html`
<div style="position: absolute; left: ${120 + i * 100}px; top: -25px; background: #2C3E50; border: 2px solid #E67E22; border-radius: 8px; padding: 6px 10px; color: white; font-size: 10px; text-align: center; min-width: 80px;">
<div style="font-weight: bold; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 75px;">${device.name.split(' ')[0]}</div>
<div style="color: #E67E22; font-family: monospace;">0x${device.addr.toString(16).toUpperCase().padStart(2, '0')}</div>
</div>
`)}
</div>
</div>
<!-- Pull-up Resistors -->
<div style="display: flex; justify-content: flex-end; gap: 30px; margin: 30px 20px 10px 0; color: #7F8C8D; font-size: 11px;">
<div style="text-align: center;">
<div style="width: 20px; height: 30px; background: repeating-linear-gradient(0deg, transparent, transparent 2px, #7F8C8D 2px, #7F8C8D 4px); margin: 0 auto;"></div>
<div>4.7k</div>
<div style="color: #E67E22;">to VCC</div>
</div>
<div style="text-align: center;">
<div style="width: 20px; height: 30px; background: repeating-linear-gradient(0deg, transparent, transparent 2px, #7F8C8D 2px, #7F8C8D 4px); margin: 0 auto;"></div>
<div>4.7k</div>
<div style="color: #3498DB;">to VCC</div>
</div>
</div>
${busDevices.length === 0 ? html`
<div style="text-align: center; color: #7F8C8D; padding: 20px; font-style: italic;">
No devices on bus. Add sensors using the dropdown above.
</div>
` : ''}
</div>
`
// Scan Results Display
const results = html`
<div style="background: #f8f9fa; border-radius: 8px; padding: 15px; margin: 15px 0; border-left: 4px solid #16A085;">
<div style="font-weight: bold; color: #2C3E50; margin-bottom: 10px;">Scan Results</div>
${scanResults === null ? html`
<div style="color: #7F8C8D; font-style: italic;">Click "Scan I2C Bus" to detect devices...</div>
` : html`
<div style="font-family: 'Courier New', monospace; font-size: 13px;">
<div style="color: #2C3E50; margin-bottom: 10px;">Scanning I2C bus...</div>
${scanLog.slice(0, 8).map(entry => html`
<div style="display: flex; align-items: center; gap: 10px; padding: 3px 0; ${entry.ack ? 'color: #16A085; font-weight: bold;' : 'color: #BDC3C7;'}">
<span style="width: 60px;">0x${entry.addr.toString(16).toUpperCase().padStart(2, '0')}</span>
<span style="width: 70px; flex-shrink: 0; white-space: nowrap;">${entry.ack ? 'ACK' : '-- NACK'}</span>
<span>${entry.ack ? entry.name : ''}</span>
</div>
`)}
${scanLog.length > 8 ? html`<div style="color: #7F8C8D;">... (${scanLog.length - 8} more addresses checked)</div>` : ''}
<div style="margin-top: 15px; padding-top: 10px; border-top: 1px solid #dee2e6;">
<strong style="color: #16A085;">Found ${scanResults.length} device(s):</strong>
${scanResults.map(d => html`
<div style="margin-left: 10px; color: #2C3E50;">
- <code style="background: #e9ecef; padding: 2px 6px; border-radius: 3px;">0x${d.addr.toString(16).toUpperCase().padStart(2, '0')}</code> - ${d.name}
</div>
`)}
</div>
</div>
`}
</div>
`
// Common I2C Addresses Reference
const reference = html`
<div style="background: #fff; border: 1px solid #dee2e6; border-radius: 8px; padding: 15px; margin: 15px 0;">
<div style="font-weight: bold; color: #2C3E50; margin-bottom: 10px;">Common I2C Sensor Addresses</div>
<div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 10px;">
${i2cSensorDatabase.slice(0, 12).map(sensor => html`
<div style="display: flex; align-items: center; gap: 8px; padding: 8px; background: #f8f9fa; border-radius: 6px; font-size: 12px;">
<code style="background: #2C3E50; color: white; padding: 3px 6px; border-radius: 4px; font-weight: bold;">
0x${sensor.addr.toString(16).toUpperCase().padStart(2, '0')}
</code>
<div>
<div style="font-weight: 500; color: #2C3E50;">${sensor.name.split('(')[0].trim()}</div>
<div style="color: #7F8C8D; font-size: 10px;">${sensor.category}</div>
</div>
</div>
`)}
</div>
<div style="margin-top: 15px; padding: 10px; background: #fff3cd; border-radius: 6px; font-size: 12px; color: #856404;">
<strong>Address Conflicts:</strong> Some sensors share default addresses (e.g., MPU6050 and DS3231 both use 0x68; INA219 and PCA9685 both use 0x40).
Use alternate addresses or I2C multiplexers (TCA9548A) to resolve conflicts.
</div>
</div>
`
output.replaceChildren(bus, results, reference);
}
addSensorBtn.addEventListener("click", () => {
const sensor = i2cSensorDatabase.find(s => s.name === selectedSensor.value);
if (sensor) {
const addr = useAltAddress.value && sensor.altAddr ? sensor.altAddr : sensor.addr;
const existing = busDevices.find(d => d.addr === addr);
if (!existing) busDevices = [...busDevices, { name: sensor.name, addr, category: sensor.category }];
}
render();
});
clearBusBtn.addEventListener("click", () => {
busDevices = [];
scanResults = null;
scanInProgress = false;
currentScanAddr = 0;
render();
});
scanBusBtn.addEventListener("click", () => {
if (busDevices.length > 0) {
scanInProgress = true;
scanResults = null;
scanLog = [];
currentScanAddr = 0;
const addresses = [0x20, 0x23, 0x27, 0x29, 0x3C, 0x3D, 0x40, 0x41, 0x44, 0x45, 0x48, 0x49, 0x53, 0x60, 0x68, 0x69, 0x76, 0x77];
let found = [];
let log = [];
for (let addr of addresses) {
const device = busDevices.find(d => d.addr === addr);
if (device) {
found.push({ addr, name: device.name, ack: true });
log.push({ addr, ack: true, name: device.name });
} else {
log.push({ addr, ack: false, name: null });
}
}
scanResults = found;
scanLog = log;
scanInProgress = false;
}
render();
});
render();
return widget;
}Interactive: I2C Bus Scanner
Interactive: I2C Bus Scanner
animation
sensors
protocols
Try Leave Add Sensor to Bus: on BMP280 (Pressure) and Use alternate address off; click Add to Bus, then press Check bus addresses to scan.
Observe The bus begins empty; after adding the default BMP280, the scan lists it at 0x76 in the found-device results.
Explain The scan checks whether an added device occupies each address in a fixed list of 18 and marks a match as ACK.
Technical boundaries. The scanner assumes devices respond solely according to its in-memory address list, without contacting hardware. It checks 18 preset addresses and silently rejects a second device at an occupied address; electrical timing and real address collisions are not simulated.
NoteInteractive: I2C Bus Scanner
This simulation demonstrates how I2C bus scanning works. Add devices to the bus, then click “Scan Bus” to see the master controller query each address and detect ACK/NACK responses.