In this tutorial we will learn how to build a serial port based data logging program that will log data coming from a Arduino to a CSV file (Comma Separated Variable file) on a Linux system.
This can be used to build simple Arduino data acquisition system to monitor analog parameters like temperature, pressure, humidity etc and log it a CSV file.
Here we will be talking about the Linux specific parts. For the full tutorial check the below link.
The system we are going to build has 4 channels that will monitor four LM35 analog temperature sensors and send the digital values to a Linux system through Arduino serial port.
PC side code is written in Python 3.x.x and uses the pySerial Library to communicate with the serial port. Arduino side code is written in Arduino C.
Please note that this section only deals with the Linux specific parts of the Python Datalogging program like how to configure and run the code on a Linux system ,How to enable permissions to access the serial port.
- Detailed line by line explanation of the code of Arduino Python Data Acquisition System can be found here.
- Video of the assembly of the Arduino 4 channel Python Data Acquisition System can be found here.
Source codes
All the Source codes can be downloaded from our GitHub repo using the below links.
Please use the full source codes from our GitHub Repo to avoid errors
Installing pip package installer on Linux
Make sure that you have Python 3.x installed on your Linux distro. After that we have to install the pip package installer in order to install the PySerial library.
You can check whether pip is installed on your system by typing pip on your command line.
In our system, pip is not installed, So we are going to install it.
First we update the repos by typing the below command and give the password for your system when asked.
sudo apt update
Now install pip
sudo apt install python3-pip
After the installation is complete ,you can check the version of your pip installation by typing
pip --version
You can also view the installed modules using the list command.
pip list
Installing Pyserial using Pip
After installing the pip installer you can install the pyserial module using the following command
pip install pyserial
Identifying Serial Ports on Linux
Serial ports on Linux are named
- as ttyS0,ttyS1 for hardware serial ports
- ttyUSB0 ,ttyUSB1 for USB to serial converter based ports Eg FT232 based chips
- ttyACM0 for Arduino
Connect your device (USB to serial converter)to the Linux PC and issue the dmesg command to check for hardware changes. Make sure to use "sudo"
sudo dmesg | tail
From the output we can see that the device connected is an Arduino and the port number is ttyACM0.
Now when we are trying to open the serial port ,you have to give the full name /dev/ttyACM0.
Adding the user to tty and dialout groups
Unlike other operating systems like Windows, Linux do not provide the user with default access to serial port.
To access the serial port you have to be members of tty and dialout groups.
You can add the user to those groups using usermod command.
sudo usermod -a -G tty username
sudo usermod -a -G dialout username
eg here username is rahul
Now logout and Login to the system for the changes to take place.
Running the Python CSV Datalogger on Linux
- Download the appropriate Arduino C code to the Arduino board from the repo
- Connect the Arduino board to the PC
- Find out the correct serial port number of Arduino (explained above).Here it is ttyACM0 ,may change with Arduino
- connect the temperature sensors to the Arduino board
- Run the Python code using the following command
python3 your_python_logger_code.py
When you enter the name of the port ,give the full name eg :/dev/ttyACM0
After which the system will start logging data as shown below
You can stop the the data logging by pressing CTRL + C.
After the logging is stopped you can find the logged data in the form of a CSV text file on the same directory as the python script.
References
- DIY 4 Channel Arduino Based CSV datalogging system using Python and pySerial
- Operating System Signals (SIGBREAK,SIGINT) programming using Python Signal Module
- Serial Port Programming on Linux using Python and pySerial
- Serial Port Programming on Windows using Python and pySerial
- Log in to post comments