Build a Linux Based Data Acquisition and Logging System Using Arduino and Python

by xanthium-enterprises in Circuits > Arduino

953 Views, 3 Favorites, 0 Comments

Build a Linux Based Data Acquisition and Logging System Using Arduino and Python

Serial Port Datalogging to a CSV (comma separated file) file on Linux using Arduino Uno and Python

A data acquisition system (DAS) is a specialized setup designed to collect, measure, monitor, and record various types of physical phenomena or signals for analysis, processing, and visualization.

It's commonly used in scientific, industrial, engineering, and research applications where accurate and timely data collection is essential.


Original Article can be found here


The system is comprised of hardware and software components working together to convert analog signals from the real world into digital data.

Here in our system ,

  1. hardware components consists of an Arduino and 4 LM35 temperature sensors that log the data from the environment and gets digitized by the ADC of the Arduino .
  2. The digitized digital data is send to the PC over a virtual COM port serial connection to a Linux PC.
  3. On the PC side ,a Python3 script reads the data and saves the timestamped data in the form of a CSV file to the disk

Supplies

linux-data-logging-csv-file-thumbnail-2.jpg

Installing Pip Package Installer on Linux

check-pip-version.jpg
install-pyserial-pip.jpg
install-python-pip-apt.jpg
python-pip-install-check.jpg

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.

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


Installing Pyserial Using Pip

install-pyserial-pip.jpg

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

pip install pyserial


Identifying Serial Ports on Linux

dmesg-tail-arduino-linux.jpg

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

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.

Block Diagram of the Setup

arduino-python-pc-temperature-data-logger.jpg
python-data-logger-software-architecture (1).jpg

The system comprises of 4, LM35 temperature sensors connected to a LM324 amplifier board which acts as an analog front end for the Arduino board.

The Analog signal from the amplifier board are digitized by the ADC on the Arduino and send to a PC MAC or Linux computer.

A Python script running on the Linux PC will monitor the serial port and receive the temperature values from the Arduino.

The script will write the received values to a CSV file 

You can terminate the script by using the CTRL + C Keyboard combination


Adding the User to Tty and Dialout Groups

add-user-tty-dialout.jpg

Unlike other operating systems like Windows, Linux do not provide the user with default access to serial port.

To access the serialport 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

run-python-arduino-data-logger.jpg
logging-data-arduino-python.jpg
python-csv-file-output.jpg
  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).
  4. Here it is ttyACM0 ,may change with Arduino

connect the temperature sensors to the Arduino board 

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 above

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.