There is often a need to capture data from an Arduino device and write this data to a 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.
Here are the major components for this project
- Arduino Nano 33 BLE
- Laptop running Python
- USB cable
These programs will work with any board in the Arduino family; all it needs is a Serial port that supports the Arduino Serial protocol.
Although not tested, It should also work with Linux, Windows or Mac operating systems.
pySerialpySerial 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 recive data from these devices.
To run pySerial, Pyhton 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)
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
You can check if pySerial is installed with
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
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.
SchematicReally simple
Connect Nano port to computer port using a USB cable
Flow ChartHere 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.
Here are the steps needed
- Open the sketch “LoggingSketch.ino” in the Arduino IDE
- Upload to the sketch to the Nano
- 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.
- In a terminal window, start the python program
- 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
Comments
Please log in or sign up to comment.