Data Acquisition and analysis for Physics education with Raspberry Pi
This is the English version of the documentation.
This python3 code provides some basic functionality for data acquisition and visualisation like data logger, bar-chart, XY- or oscilloscope display and data recording on disk.
In addition to the GPIO inputs/outputs of the Raspberry Pi, the analogue-to-digital converters ADS1115 and MCP3008 and PicoScope USB-oscilloscopes are supported as input devices for analogue data, as well as a number of digital sensors using protocols like I²C or SPI.
The package provides an abstraction layer for measurement devices and sensors connected to a Raspberry Pi. Dedicated classes for each device provide a simple, unified interface, containing only the methods
closeDevice(). Simple examples with minimalist code illustrate the usage. The graphical user interface
phypi.py and the script
run_phypi.py provide a configurable environment for more complex measurements.
After installation - see below - a number of unified classes for data acquisition, visualisation and recording is available from the sub-directory
Each supported device needs a specific configuration, which is read from configuration files in sub-directory
./config. The overall configuration is given in files of type
.daq, specifying which devices and display modules to use, the readout rate, calibrations or analytical formulae to be applied to recorded data, or ranges and axis labels of the graphical output.
The graphical user interface
phypi.py aids in the administration of the configuration options and can be used to start data acquisition. In this case, configurations and produced data files are stored in a dedicated sub-directory in
$HOME/PhyPi. The sub-directory name is derived from a user-defined tag and the current date and time.
Data acquisition may also be started via the command line:
If no configuration file is given, the default
PhyPiConf.daq is used.
./examples contains a number of simple python scripts illustrating the usage of data acquisition and display modules with minimalist code.
Configuration files for PhyPiDAQ
run_phypi.py allows users to perform very general measurement tasks without the need to write custom code. The options for configuration of input devices and their channels as well as for the display and data storage modules are specified in a global configuration file of type
yaml markup language), which contains references to device configuration files of type
Main configuration file
A typical, commented example of the main configuration file is shown below. Note that text following a ''#''-sign is ignored and contains descriptive comments or alternatives.
# -- Configuration Options for PhyPiDAQ # -------------------------------------- # # -- configuration files for hardware devices # DeviceFile: config/ADS1115Config.yaml # 16 bit ADC, I2C bus # optional: #DeviceFile: config/MCP3008Config.yaml # 10 bit ADC, SPI bus #DeviceFile: config/MCP3208Config.yaml # 12 bit ADC, SPI bus #DeviceFile: config/groveADCConfig.yaml # 12 bit ADC on grove RPI shield #DeviceFile: config/PSConfig.yaml # PicoTechnology USB scope #DeviceFile: config/MAX31865Config.yaml # Pt 100 sensor #DeviceFile: config/GPIOCount.yaml # frequency count #DeviceFile: config/DS18B20Config.yaml # digital temperature sensor #DeviceFile: config/MAX31855Config.yaml # thermo element #DeviceFile: config/BMP180Config.yaml # pressure/temperature sensor #DeviceFile: config/INA219Config.yaml # Voltage/Current sensor #DeviceFile: config/MMA845xConfig.yaml # Accelerometer #DeviceFile: config/VL53LxConfig.yaml # ToF distance sensor ## an example of multiple devices #DeviceFile: [config/ADS1115Config.yaml, config/GPIOCount.yaml] # Demo options: #DeviceFile: ToyDataConfig.yaml # simulated data #DeviceFile: config/ReplayConfig.yaml # data from File # # -- configuration options for Channels # ChanLabels: [U, U] # names for channels ChanUnits: [V, V] # units for channels ChanColors: [darkblue, sienna] # channel colours in display # eventually overwrite Channel Limits obtained from device config ##ChanLimits: ## - [0., 1.] # chan 0 ## - [0., 1.] # chan 1 ## - [0., 1.] # chan 2 # calibration of channel values # - null or - <factor> or - [ [ <true values> ], [ <raw values> ] ] #ChanCalib: # - 1. # chan0: simple calibration factor # - [ [0.,1.], [0., 1.] ] # chan1: interpolation: [true]([<raw>] ) # - null # chan2: no calibration # apply formulae to (calibrated) channel values #ChanFormula: # - c0 + c1 # chan0 # - c1 # chan1 # - null # chan2 : no formula # # -- configuration options for graphical display # Interval: 0.1 # logging interval #NHistoryPoints: 120 # number of points used in history buffer DisplayModule: DataLogger # history of channel signals #DisplayModule: DataGraphs # text, bar-graph, history and xy-view #DisplayModule: null # no graphical display #Title: Demo # display title #XYmode: false # enable/disable XY-display ## if more than two channels active: #Chan2Axes: [0, 1, 0] # assign channels to axes #xyPlots: # define which axes to show # - [0, 1] # in xy-plot # - [0, 2] # - [1, 2] # # -- start in running or paused mode # startActive: true # start in running mode # # -- configuration options for output to file # #DataFile: testfile.csv # file name for output file, DataFile: null # null to disable #CSVseparator: ';' # field separator, set to ';' for German Excel # enable buffering of latest data (depth NHistoryPoints from above) #bufferData: PhyPiData # file name to track latest data and eventually #bufferData: null # store them, or null to switch off # enable output to fifo (a linux pipe) to send data to other processes DAQfifo: null #DAQfifo: PhyPiDAQ.fifo
Device configuration files
Typical, commented examples of device configurations are shown below. The device configuration file for the analogue-to-digital converter ADS1115 specifies the active channels, their ranges and single or differential operation modes.
# example of a configuration file for ADC ADS1115 DAQModule: ADS1115Config # phypidaq module to be loaded ADCChannels: [0, 3] # active ADC-Channels # possible values: 0, 1, 2, 3 # when using differential mode: # - 0 = ADCChannel 0 # minus ADCChannel 1 # - 1 = ADCChannel 0 # minus ADCChannel 3 # - 2 = ADCChannel 1 # minus ADCChannel 3 # - 3 = ADCChannel 2 # minus ADCChannel 3 DifModeChan: [true, true] # enable differential mode for Channels Gain: [2/3, 2/3] # programmable gain of ADC-Channel # possible values for Gain: # - 2/3 = +/-6.144V # - 1 = +/-4.096V # - 2 = +/-2.048V # - 4 = +/-1.024V # - 8 = +/-0.512V # - 16 = +/-0.256V sampleRate: 860 # programmable Sample Rate of ADS1115 # possible values for SampleRate: # 8, 16, 32, 64, 128, 250, 475, 860
The USB-oscilloscope PicoScope can also be used as data logger. In this case the average of a large number of measurements at high rate is taken. Choosing a measurement time of 20 ms very effectively eliminates 50 Hz noise.
# example of a configuration file for PicoScope 2000 Series DAQModule: PSConfig PSmodel: 2000a # channel configuration picoChannels: [A, B] ChanRanges: [2., 2.] ChanOffsets: [-1.95, -1.95] ChanModes: [DC, DC] sampleTime: 2.0E-02 Nsamples: 100 # oscilloscope trigger trgActive: false # true to activate trgChan: A #trgThr: 0.1 #pretrig: 0.05 #trgTyp: Rising #trgTO: 1000 # time-out # internal signal generator # frqSG: 100.E+3 # put 0. do disable frqSG: 0.
Examples of other devices like the analog-to-digital converter MCP3008, of rate measurements via the GPIO pins of the Raspberry Pi or temperature measurements with the 1-wire digital thermometer DS18B20, PT100 sensors and the resistance-to-digital converter MAX31865 or thermocouples and the thermocouple-to-digital converter MAX31855 are also contained in the configuration directory, see files
Installation of PhyPiDAQ on a Raspberry Pi
Get PhyPiDAQ code and dependencies
After setting up your Raspberry pi with the most recent version of the Debian Release stretch, enter the following commands in the console window:
mkdir git cd git git clone https://github.com/GuenterQuast/PhyPiDAQ
For your convenience, the script installlibs.sh installs all components needed for PhyPiDAQ. Simply execute
the script installlibs.sh once on the command line (without text after
cd ~/git/PhyPiDAQ # change to installation directory git pull # eventually update to latest version of PhyPiDAQ ./installlibs.sh
The installation is now done and PhyPiDAQ is ready to be used.
The last part of the inatallation procedure is also valid to update an exiting verion of
To test the installaion without connected hardware or on a system other than the Raspberry Pi, PhyPiDAQ may be started in demo-mode:
cd ~/git/PhyPiDAQ # change to installation directory ./run_phypi.py # execute run_phypi.py with configuration PhyPiDemo.daq
PhyPiDAQ is meant to be an educational tool. Confronting students with the full contents of this package is therefore not appropriate. Instead, it is recommended to create a working directory and copy examples from there to the student's working directory. This is achieved via the following commands:
# create PhyPi working directory and make examples and config files available cd ~/git/PhyPiDAQ ./install_user.sh # provide icon to graphical user interface cp ~/git/PhyPiDAQ/phypi.desktop ~/Desktop
You might also consider moving the PhyPiDAQ package to system space, e.g. /usr/local:
sudo mv ~/git/PhyPiDAQ /usr/local/
Please note that the paths in the example above must be adjusted in this case, e.g. ´~/git/` -> /usr/local/. The paths in ~/Desktop/phypi.desktop must also be changed appropriately. This is most easily achieved by right-clicking the icon and use the dialog "Properties".
Dependencies on external packages
The PhyPiDAQ package relies on code from other packages providing the drivers for the supported devices and libraries for data visualisation:
- the Adafruit Pyhon MCP3008 library
- the Adafruit Python ADX1x15 library
- the Adafruit Python MAX31855 library
- the w1thermsensor library by Timo Furrer
- components from the picoDAQ project
- the python bindings of the pico-python project by Colin O'Flynn
- the low-level drivers contained in the Pico Technology Software Development Kit
For convenience, installation files for external packages and for modules of this package in pip wheel format are provided in sub-directory ./installlibs.
The visualization modules depend on matplotlib.pyplot, Tkinter and pyQt5, which must also be installed.
For completeness, the steps performed by the script
installlibs.sh are documented here:
# script installlibs.sh sudo apt-get update sudo apt-get upgrade sudo apt-get install python3-scipy sudo apt-get install python3-matplotlib sudo apt-get install python3-pyqt5 sudo apt-get install libatlas-base-dev # needed by latest version of numpy sudo pip3 install pyyaml # pyhton drivers for sensors and devices sudo pip3 install installlibs/whl/*.whl # install drivers for PicoScope 2000 and 2000B sudo dpkg -i installlibs/picoscopelibs/*.deb sudo usermod -a -G tty pi # USB access for user pi
The drivers for PicoScope oscilloscopes may also be installed from the repository of the vendor, which is included as follows:
- Open file /etc/apt/sources.list by
sudo nano /etc/apt/sources.list.
- Use arrow keys to navigate to the next free line and add entry
deb http://labs.picotech.com/raspbian/ picoscope mainto /etc/apt/sources.list.
- Save file /etc/apt/sources.list by
Ctrl + Oand
- Close /etc/apt/sources.list by
Ctrl + X.
Now the drivers for drivers for the various PicoScope devices can be included end eventually updated with apt-get:
wget -O - http://labs.picotech.com/debian/dists/picoscope/Release.gpg.key | sudo apt-key add - sudo apt-get update sudo apt-get install libps2000 sudo apt-get install libps2000a # allow access of user pi to usb port sudo usermod -a -G tty pi
Overview of files contained in PhyPiDAQ
run data acquisition and display modules as specified in configuration files (default
PhyPiConf.daqand .yaml files ins subdirectory config/)
graphical user interface to edit configuration files and start the script
initialisation for package phypidaq
version info for package phypidaq
class for handling of analog-to-digital converter ADS1115
class analog-to-digital converter MCP3008
class for current and voltage sensor INA219
class for handling of digital thermometer DS18B20
class for the digital temperature and pressure sensors BMP180/280 or BME280
class for the digital accelerometer MMA8451
class for reading rates from GPIO pins
class for MAX31855 thermocouple-to-digital converter
class for MAX31865 resistance-to-digital converter
class for PicoScope USB oscilloscopes
class for VL53L1X distance sensor
phypidaq/TCS34725Configclass for TCS34725 RGB color sensor
phypidaq/AS7262Configclass for AS7262 six channel color sensor
phypidaq/AS7265xConfigclass for AS7265x 18 channel spectral sensor
class for gamma ray detektor GDK101, FTLAB
class to generate simulated data (for test, debugging or exercises)
class to replay data from file
interface and background-process handling data visualisation
class for display of data histories and xy diagrams
general display module for data as bar graphs, history plots and xy-graphs
store data in CSV format
class containing the implementation of script
runPhyPiUI.pyclass implementing the graphical user interface
phypiUIas base class
base class for
runPhyPyUI, generated from
designer-qt5, describes the graphical user interface
main configuration file, depends on device configurations in sub-directory config/
config/ADS1115Config.yaml16 bit ADC
config/MCP3008Config.yaml10 bit ADC
config/MCP3208Config.yaml12 bit ADC
config/INA219Config.yamlcurrent and voltage sensore
config/DS18B20Config.yamldigital temperature sensor
config/BMP280Config.yamltemperature and pressure sensor
- ``config/BMP180Config.yaml` temperature and pressure sensor
config/GPIOCount.yamlfrequency measruement via GPIO pin
config/MAX31855Config.yamlconverter for thermocouple
config/MAX31865Config.yamlconverter for PT-100
config/AS7262Config.yaml6 channel color sensor
config/AS7265xConfig.yaml18 channel spectral sensor
config/PSConfig.yamlPicoScope usb oscilloscope
very minimalist example to read one channel from an analog-to-ditigal converter
very minimalist example to read one channel from an analog-to-ditigal converter and display data as a history graph
read two channels from an analog-to-ditigal converter and display data as a history graph
read data from INA219 current and voltage sensor
examples/read_18B20.pys simple example to read the temperature sensor DS18B20
examples/readBMPx80.pysimple example to read the digital temperature and pressure sensor BMP180/280
examples/readMMA8541.pysimple example to read the digital accelerometer MMA8451
run an oscilloscope display, configuration as specified in .yaml file (default is
example to control GPIO pins: generate square signal on output pin from variable voltage on input pin
generate a random signal following Poisson statistics on a GPIO pin
generate a fixed frequency signal on a GPIO pin
example to set voltage on MCP4725 ditital-to-analog converter
Configuration files for run_phypi.py
display current and eventually voltage read from INA219 sensor
uses BMB180 or BMP280 sensors to display temperature and air pressure
uses MMA8451 to display x-, y- and z-acceleration
measure noise with a microphone connected to PicoScope USB oscilloscope; displays the rms of 200 samples taken over a time periods of 20 ms. Can also be used with geophone SM-24
examples/RGBsensor.daqRGB color sensor
examples/ColorSpectrum.daqsix channel color sensor
examples/AS7265x.daq18 channel spectral sensor
measurement of gamma-ray dose with GDK101
examples/ToyData.daqgeneration and display of simulated data
data from file (for demo mode)
read data from named linux pipe (run_phypi.py with option DAQfifo: <pipe name>)
German only: Introductory course to measuring with the Raspberry Pi
German only: setting up the Raspberry Pi for this project
recommended components for this project
building instructions for a force sensor