Skip to main content
temperature voltage data acquisition and logging  to CSV tutorial using Arduino and Python for Linux ubuntu OS

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.

 

Serial Port Datalogging to a CSV file on Linux using Arduino Uno and Python

 

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.

Python data acquisition system based on Arduino UNO running on a Ubuntu System logging time stamped temperature data

 

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.

  1. Detailed line by line explanation of the code of Arduino Python Data Acquisition System can be found here.
  2. 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.

 

check whether pip is installed on system

 

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

 

install pip on ubuntu using apt

 

Now install pip 

sudo apt install python3-pip
installing pip on ubuntu using apt

 

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
check installed modules on pip

 

Installing Pyserial using Pip

After installing the pip installer you can install the pyserial module using the following command

pip install pyserial
how to install pyserial using pip on ubuntu

 

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

 

how to identify the serial port number of Arduino connected to a Linux PC

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

adding auser to tty and dialout groups so they can access the serial port on linux and avoid access denied errors

Now logout and Login to the system for the changes to take place.

 

Running the Python CSV Datalogger on Linux 

  1. Download the appropriate Arduino C code  to the Arduino board from the repo
  2. Connect the Arduino board  to the PC 
  3. Find out the correct serial port number of Arduino (explained above).Here it is ttyACM0 ,may change with Arduino
  4. connect the temperature sensors to the Arduino board 
  5. Run the Python code using the following command
python3 your_python_logger_code.py
coding a python data logging sytem that logs temperature data from Arduino serial port to a CSV file on Linux

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

arduino data acquisition system logging temperature data to a csv text file on linux

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.

text file containing logged values from Arduino in a csv format created by python data logger

 

References