Introduction: Capture Data From Arduino to CSV File Using PySerial

About: Retired Electrical Engineer pursuing my hobby

There is often a need to capture data from an Arduino device and write this data to a CSV file which can be used in other applications. An example would be data gathered by the Arduino as an edge device which is required to train a AI model. Completed model can then be deployed back on the Arduino device. Alternatively, the CSV file can be imported into a spreadsheet program for further analysis.

In this project, a Nano 33 BLE device is connected to a laptop and data from the Nano is captured via the Serial port by the laptop and written to a CSV file.

These programs will work with any board in the Arduino family; all it needs is a Serial port that supports the Arduino Serial protocol.

It should also work with Linux, Windows or Mac operating systems.

Supplies

Here are the major components for this project

  1. Arduino Nano 33 BLE
  2. Laptop or Computer running Python
  3. USB cable

Step 1: PySerial

pySerial is a python lbrary that allows communication over any serial port on a computer. Because all Arduino devices use the Serial port for programming and communication, pySerial can be used to send and receive data from these devices.

To run pySerial, Python must be installed on your computer. Many resources exist on the internet explaining how to install Python. (Most Linux distributions come with Python already installed)

www.python.org

To read the pySerial documentation:

https://pyserial.readthedocs.io/en/latest/index.html

pySerial also need to be installed before it can be used. This is easiest done in a terminal window with the pip command

python -m pip install pyserial

or more simply (if you have pip installed)

pip install pyserial

You can check if pyserial is installed with:

pip show pyserial

The pySerial commands used in this project are:

import serial – imports the library

serial.Serial(‘Port description’) – defines a serial port object and opens up communication

.write(data) – writes a single byte of data to the defined serial port

.readline(data) – reads a complete line of data from serial port; stops when \n encountered

.flushInput() - clears the buffer after a write operation

Step 2: Python CSV Library

Python standard distribution includes the CSV module which implements classes to read and write tabular data in CSV format from and to files. Complete official documentation

https://docs.python.org/3/library/csv.html

This library will be used to write data received from an Arduino to a CSV file.

Step 3: Schematic

Really simple

Connect Nano port to computer port using a USB cable

Step 4: Flow Chart

Here is a flow chart of how the two systems interact in this project

The actual programs used are included in this project with comments.

The program exchanges integer values which are generated in the program. It is possible to send other types of variables (float, strings, characters, unsigned integers). The decode routine in the python program needs to be changed to suit.

When using pySerial, the name of the port that the Arduino is connected to must be specified in the command

ser = serial.Serial('/dev/ttyACM0')

The above is for a Linux system. In Windows, the port will be of the form ‘COM3’ or similar, so would require

ser = serial.Serial('COM4')

This description can be found in the Arduino IDE under “Tools → Port”

Step 5: Communicate and Log

Here are the steps needed

  1. Open the sketch “LoggingSketch.ino” in the Arduino IDE
  2. Upload to the sketch to the Nano
  3. Once this is done, close the IDE. If the python program is run while, the IDE is open, an error will occur. Two programs cannot access the same Serial port.
  4. In a terminal window start the python program
python3 ReadSerial.py

The program will run and print out the values received from the Nano in the terminal window. After 9 iterations, the ‘stop’ received from the Nano will stop the python program.

In the same directory as ReadSerial.py, there will now be a file ‘logging.csv’. Open the file in Text Editor or a spreadsheet application to see the results.

Easy