nano_plot.py

Arduino Nano (Classic, ATmega328P) · File : nano_plot.py · Language : Python · Use case : Tracer les séries du Nano avec matplotlib

The IDE built-in plotter has limits: this Python + matplotlib bridge live-plots the first column of Nano readings, scrolling over 200 points. One extra dependency (matplotlib) for shareable curves.

Hardware

An Arduino Nano classic (ATmega328P at 16 MHz, 30-pin DIL breadboard footprint, mini-B USB, 5 V logic, RESET pin at the lower-left corner). PC with Python 3, pyserial, matplotlib; Nano emitting values over Serial (e.g. potentiomete-plot.ino).

Wiring

Standard USB cable; the sketch 115200 rate must match the script.

How it works

A fixed-length deque scrolls the window; set_data + draw_idle redraw without recreating the figure. Tab splitting tolerates extra debug columns.

Running it

In the Arduino IDE, select the Arduino Nano board and ATmega328P processor, pick the serial port, then upload. The board's 5 V rail suits 5 V sensors; otherwise use 3.3 V on compatible pins.

Variants

Save each window to CSV for later analysis, or plot several columns (sensor + setpoint) on the same graph.

nano_plot.py

#!/usr/bin/env python3
"""Trace en direct les valeurs serie d'un Arduino Nano.

Lit les lignes "valeur\t..." et dessine la premiere colonne.
Usage : python nano_plot.py [port]
"""
import sys
from collections import deque

import matplotlib.pyplot as plt
import serial

PORT = sys.argv[1] if len(sys.argv) > 1 else "COM7"
N = 200  # points affiches


def principal() -> None:
    carte = serial.Serial(PORT, 115200, timeout=1)
    serie = deque(maxlen=N)
    fig, ax = plt.subplots()
    ligne, = ax.plot([], [])
    ax.set_ylim(0, 5.2)
    ax.set_title("Nano A0 (V)")
    plt.ion()
    fig.show()
    while True:
        ligne_txt = carte.readline().decode(errors="replace")
        try:
            valeur = float(ligne_txt.split("\t")[0])
        except ValueError:
            continue
        serie.append(valeur)
        ligne.set_data(range(len(serie)), serie)
        ax.set_xlim(0, max(len(serie), 50))
        fig.canvas.draw_idle()


if __name__ == "__main__":
    try:
        principal()
    except KeyboardInterrupt:
        print("Arrete.")

← Back to the board