This Python script achieves the following functionality: ### Purpose: The code aims...
March 30, 2025 at 04:28 PM
This Python script achieves the following functionality:
Purpose:
The code aims to read current sensor data from an Arduino device via a serial port, store it in a CSV file, and display it in real-time on a live plot. It uses threading to handle multiple tasks concurrently, including data acquisition, user input for stopping the script, and real-time plotting.
Step-by-step Functionality:
-
Imports Necessary Libraries:
serial
: To communicate with the Arduino over a serial connection.csv
: For writing sensor data to a CSV file.threading
: To run data acquisition and user input tasks concurrently.datetime
: For timestamping sensor readings with the current date and time.matplotlib
: To create real-time line plots.
-
Configuration:
- Defines the
serial_port
(e.g.,"/dev/ttyACM0"
, Arduino connection) andbaud_rate
(communication speed, 9600 bps) to connect to the Arduino. - Specifies
filename
("INA219-output.csv"
) as the CSV output file to log sensor data.
- Defines the
-
Serial Port Initialization:
- Opens a serial connection to the specified port and baud rate to communicate with the Arduino.
-
Global Variables:
- Initializes
data_list
to store sensor readings as tuples(timestamp, current_value)
. - Uses
data_lock
to synchronize access todata_list
across multiple threads safely. - Creates a
stop_event
flag to signal when to exit the data acquisition loop.
- Initializes
-
Data Acquisition:
- A thread (
read_data
) continuously reads data from the serial port:- It decodes the incoming data.
- Timestamps the data.
- Writes it to the output CSV file (
INA219-output.csv
). - Parses the data as a floating-point number (current value in amperes) and appends it to
data_list
.
- A thread (
-
User Input to Stop:
- Another thread (
wait_for_enter
) waits for the user to press "Enter" to signal the script to stop. - When triggered, it sets the
stop_event
flag, which stops the data acquisition loop.
- Another thread (
-
Real-Time Plotting:
- Uses
matplotlib
to display sensor data in real time:- Initializes a plot with time (x-axis) and current (y-axis).
- Handles live updates through
matplotlib.animation.FuncAnimation
. Theupdate
function fetches the latest data fromdata_list
and redraws the plot every second. - Formats the x-axis as time using
mdates.DateFormatter
.
- Uses
-
Threads and Plot Execution:
- Starts the two threads: one for data acquisition and one for user input handling.
- Displays the real-time plot (blocking until the plot window is closed).
- Waits for the threads to finish execution cleanly after the user presses "Enter" or the program finishes.
-
Cleanup:
- Closes the serial connection and prints a termination message before exiting.
Key Functional Highlights:
-
Data Acquisition:
- Reads actual sensor data from the Arduino via serial communication.
- Stores time-stamped data into both a CSV file and a Python list for live plotting.
-
Real-Time Display:
- Dynamically updates a plot with changing current readings over time.
-
User Interaction:
- Allows the user to stop the script by pressing "Enter," ensuring a clean exit.
-
Thread-Safe Data Sharing:
- Uses threading with locks (
data_lock
) to safely share data (data_list
) between threads handling data acquisition, real-time plotting, and user input.
- Uses threading with locks (
-
Dynamic Plot Updates:
- Automatically adjusts plot bounds (x-axis and y-axis) to fit the incoming data.
Typical Use Case:
- This script would be used for logging and visualizing sensor data in real time, where the sensor is connected to an Arduino sending measurements over a serial connection (e.g., current readings from an INA219 sensor).
Generate your own explanations
Download our vscode extension
Read other generated explanations
Built by @thebuilderjr
Sponsored by beam analytics
Read our terms and privacy policy
Forked from openai-quickstart-node