Raspberry Pi Logger


It’s time to use the Raspberry Pi ! We do not spend words to describe this single-board linux computer : on the Internet there are hundreds of excellent sites / information on this card, so refer to the documentation that you can easily find online.
In this post we focus instead on an application of the Raspberry Pi in the scientific field: the data logger.
A data logger is a data recorder, usually acquired by one or more sensors and stored in an internal memory. It is a perfect application for the Raspberry Pi : with its low cost, the small power required to operate, the small physical dimensions and the rich open source software, this device is wonderfully suitable for use as a data collection point.
In our case we have collected data through a serial interface. Data are acquired with a PSoC 5lp micricontroller and sent to Raspi through a serial interface. In our case the data are the cosmic ray count that are captured by a scintillation sensor, this sensor has been described in the post : Scintillation Detector for Cosmic Muons.
The same HW and SW configuration of the Raspberry can however be used for any type of data. The image below shows the logical scheme of the links.

HW Setup

To use the serial interface of the Raspberry, it is necessary to take into account the fact that Raspi works at 3.3V. Using different voltage levels, such as the classic 5V, could lead to malfunctions or cause permanent damage to the processor.
The PSoC microcontroller that acquires the data, works instead at 5V, for this reason we decided to use a level shifter, between the Raspi and the PSoC, in order to make compatible the respective serial lines. The image below shows the pins used on the terminal board of our Raspberry.

The level shifter is a simple circuit, often on a breakboard, which modifies the voltage of the logic levels on the lines used for the transmission of binary data. In the level shifter circuit the respective working voltages of the two lines are supplied, in our case the 3.3V from the Raspberry and the 5V from the PSoC. The connections are shown in the figure below.

SW Setup

Let’s start from a Raspberry Pi 3 Model B+ with Linux operating system loaded from SD card in basic configuration. It is necessary to make some configuration changes to enable (and to make it run) the serial port.
An example can be found at the following link : raspi-data-logger.
Linux must be configured beforehand to enable the serial port and the GPIOs (leaving the other options disabled, otherwise interference will be created), you can do it through the utility:

sudo raspi-config

The system must obviously be updated :

sudo apt-get update
sudo apt-get upgrade

In the /etc/modules file check the presence, or add, the following lines:


In the file /boot/confix.txt it is necessary to add the following lines :


The first line disables Bluetooth, the second line enables serial communication, the third line sets the processor clock frequency to 250MHz. Since the serial baudrate is connected to the system clock we want it to remain constant, otherwise serial communication can not work properly. With the configurations described above the serial port will be available on /dev/serial0.

Example Code

After the hardware preparation and software configurations of Linux you can start using the serial by writing an appropriate program in python. Of course all the necessary modules / libraries must be downloaded using the appropriate commands, for example :

sudo apt-get install python-matplotlib

The lines of python code shown below show the basic instructions for using the serial line. We highlight the serial port definition instruction in which the mount point in the file system “/dev/serial0” and the baud rate are specified, the baud rate must correspond to the serial speed on the PSoC, 115000, in our case.

The data on the serial line is read with the readline() statement, which reads the entire line up to the terminating characters. The data is converted into a float format and inserted into a data array.

import serial
import numpy as np
ser = serial.Serial('/dev/serial0', 115200)
CPM = np.float32(ser.readline())   # converts the CPM string to a float
DataArray[nDataPoints] = CPM
nDataPoints = nDataPoints +1

As an example, we report below the part of the PSoC code in which the value of the event count per minute (CPM) is written to serial. The numeric value of the count, minuteMuonCount, is preceded by the prefix CP, and followed by the termination characters “\r\n

/* Send out the data in the serial channel */
char TransmitBuffer[TRANSMIT_BUFFER_SIZE];
/* Send out the data with prefix CP */

Download of python software for serial port acquisition of cosmic ray counts : CosmicPI_1.1. Based on Aurora-Monitor.

Data Acquired

Data that are acquired via serial connection by the Raspberry Pi can simply be stored on file to be processed later, or can be viewed in “real time” as they are acquired. The methods of storing, processing and displaying data depend mainly on the specific type of application and above all on the time interval of data collection. For short times (minutes) the display can be used in real time; for long times (hours or days) it may be more appropriate to store the data on files and to process them later.
Python is suitable for processing and viewing collected data. The numpy, matplotlib and scipy libraries contain all the necessary functions; the obspy package can be conveniently used for storing and processing data in miniseed format (initially designed for geophysical data).

For the installation of the obspy package refer to the github page obspy (Raspbian release wheezy).

The graphs below show the cosmic ray counting rate (events per minute) recorded during two days.

If you liked this post you can share it on the “social” Facebook, Twitter or LinkedIn with the buttons below. This way you can help us! Thank you !


If you like this site and if you want to contribute to the development of the activities you can make a donation, thank you !

Check Also

CALL for Donations

The PhysicsOpenLab adventure has lasted for several years now. They were beautiful years in which …