import time import smbus from collections import deque import matplotlib.pyplot as plt BUS=1 ADDR=0x48 REG_CONV=0x00 REG_CONFIG=0x01 bus = smbus.SMBus(BUS) # Continuous, AIN0, PGA ±4.096V, 860 SPS (chip side) config = 0x8000 | 0x4000 | 0x0200 | 0x0000 | 0x00E0 | 0x0003 bus.write_i2c_block_data(ADDR, REG_CONFIG, [(config>>8)&0xFF, config&0xFF]) FS_VOLTS = 4.096 def read_volts(): data = bus.read_i2c_block_data(ADDR, REG_CONV, 2) raw = (data[0]<<8) | data[1] if raw & 0x8000: raw -= 1<<16 return raw * FS_VOLTS / 32768.0 WINDOW_SECONDS = 0.5 ts = deque() vs = deque() plt.ion() fig, ax = plt.subplots() line, = ax.plot([], []) ax.set_title("ADS1115 AIN0 live (real-time window)") ax.set_xlabel("Time (s)") ax.set_ylabel("Volts") ax.set_ylim(-0.5, 3.5) last_draw = time.perf_counter() draw_interval = 1/30 t_start = time.perf_counter() while True: v = read_volts() t = time.perf_counter() - t_start ts.append(t) vs.append(v) # keep only last WINDOW_SECONDS while ts and (ts[-1] - ts[0]) > WINDOW_SECONDS: ts.popleft() vs.popleft() now = time.perf_counter() if now - last_draw >= draw_interval: # x-axis is "time relative to last point" t0 = ts[-1] xs = [ti - t0 for ti in ts] # ends at 0 line.set_data(xs, list(vs)) ax.set_xlim(-WINDOW_SECONDS, 0) fig.canvas.draw_idle() plt.pause(0.001) last_draw = now