An acquisition system framework for mechanical measurements with Python, Raspberry-Pi and MEMS sensors

In this paper, we propose the development of a low-cost acquisition system using Raspberry-Pi 3 and MEMS accelerometers to measure vibrations in mechanical systems. The main objective is to assemble a signals acquisition system easy to handle, of low cost and good accuracy for teaching and industrial purposes. The central idea is that the signals are acquired and processed in the Raspberry-Pi. The Python language and numerical libraries (scipy, numpy and matplotlib) are used for implementation of acquisition and signal processing. This paper proposes the study of vibrations in time and frequency domain. The acquisition system proposed can be easy implemented and the results obtained had good precision (time and frequency domain) and agree with the literature.


Introduction
Open platform-based microcontrollers, which, in general, use the Arduino and Raspberry-Pi microcontroller as an acquisition system in conjunction with several MEMS sensors to measure vibrations in mechanical systems are now available to a wide range of students, professionals and young researchers, in the most diverse areas of knowledge.
Mechanical vibrations measurement is one of the most important types of measurement applied to engineering today, either for the study itself as modal analysis [1,2] or even the development of control of dynamical systems based on these studies [3], and we can mention the structural health monitoring of machine components [4], such as rotating elements [5], wind turbines [6], compressors, pumps, fans, among others. Linked to such a study is the use of accelerometers that are sensors of the most variety such as piezoelectric, piezoresistives and capacitives [7].
With the growing availability of low-cost, open source technology in a wide range of devices, associated with ease of use, such as the Arduino and Raspberry-Pi microcontrollers, the application of these devices has been widely used in teaching and scientific development [8]. This work is in line with these new technologies, including a modal study of vibrations using microelectromechanical system accelerometers (MEMS) and the development of high-level Python programming, all together in an affordable Raspberry-Pi based acquisition system. * Correspondence email address: marcusvaranis@ufgd.edu.br The Raspberry-Pi is a card-sized microcomputer, whose initial proposal was to bring programming and computer teaching to schools of developing countries. However, the Raspberry-Pi has brought together other open source technologies in which it has enabled applications in various areas of modern physics and engineering. A system based on MEMS accelerometers and Raspberry-Pi can be found in [9], it is a proposal for an urban seismic network in real time in Sicily (Italy). MEMS are already being used in applications such as fall detection systems [10,11], angle measurement [12] and robot control [13]. Another important part of the Raspberry-Pi development is the possibility of developing applications with cloud storage [14], internet of things (IoT) [15], face recognition [16].
This work also aims to expand the proposals initiated in [17] where the introduction of an easy to use, low cost and good precision signal acquisition system is presented, with didactic objectives for applications in teaching physics and engineering. The same mechanical system proposed in [18] is used here, to compare using Raspberry-Pi, where the performance of two accelerometers of the MEMS type, ADXL335 and MPU6050 with an acquisition system based on the Arduino microcontroller, is evaluated, where the results are obtained to evaluate the dynamic structure response Shear building type with 3 degrees of freedom, excited by a nonideal source. A long and in-depth study on the use of MEMS accelerometers for the analysis of mechanical vibrations can be seen in [19]. In-depth study of mechanical vibrations and modal analysis with teaching applications using Arduino and MEMS sensors can be seen in [20][21][22]. In this way, this study proposes the Even with the proper connection, the communication between both devices is yet not possible. Before, the I2C port must be enable inside the Raspberry-Pi configurations so the microprocessor will be able to initialize and use its I2C port. After this, the next step is to install a tool package to detect the accelerometers addresses that are connect to the I2C port. Besides that, in some cases, another configuration must be done inside the folders that define the I2C communication parameters, this is done so the transmission velocity limit is raised using Raspbian system terminal commands.
The last setup is about installing the needed modules for the Python software already incorporated to the OS. Then, the modules used during the project, such as SMBus, datetime and numpy can be installed.
After all the setup steps properly conducted, the Raspberry-Pi can detect the accelerometers and identify its addresses. And the program, written in Python language, will be able to use its modules to configure and collect digital data coming from the accelerometer, according to its respective limitations and factory internal functions further discussed.

MEMS sensors
In this work, the accelerometers ADXL345, MPU6050 and MMA7455 are used. The ADXL345 is a small, lowpower, three-axis accelerometer, with high resolution measurement (13 bits) at 16g (gravity), it has digital output from Analog Devices and selectable measurement ranges in gravities, 2 g, 4 g, 8 g or 16 g, which determine the range of motion, and a fixed sensitivity of 4 mg/LSB. Measuring only 3mm × 5mm × 1mm and with low energy consumption, which is between 40 A to 145 A, and by default the connection via the digital interface I2C and SPI [23]. Its data rate at the output is between 6.25Hz and 3200Hz with a bandwidth between 3.125Hz and 1600Hz. The minimum operating temperature of −40C and the maximum of +85C.
The MPU6050 is the first integrated 6-axis Motion-Tracking device that combines a 3-axis gyroscope, a 3-axis accelerometer, and a Digital Motion Processor, all on a 4mm × 4mm × 0.9mm plate. The userprogrammable accelerometer features scales of 2g, 4g, 8g and 16g, its 16-bit resolution, with a consumption of 500uA in normal operations, and the I2C communication interface. The operating temperature is between −40C to 85C, with bandwidth ranges from 5Hz to 260Hz, and acquisition rate from 10Hz to 520Hz [24].
The MMA7455 is an I2C digital output sensor, low power, capacitive and with signal conditioning and low pass filter. It has a resolution for 3 acceleration ranges, 2g, 4g, 8g. It has a resolution of 10 bits, a consumption of 400uA, operates between the voltage of 2.4V and 3.6V, and works in the temperature range of −40C to 85C. Their bandwidths are 62.5Hz and 125Hz, with their data collection rates of 125Hz and 250Hz [25].

Mechanical system
The acquisition system scheme ( Figure 2) that was used is validated from a test bench, already used in [18], Figure 3. As shown in [18] the natural frequencies of the structure are 4.09 Hz, 12.54 Hz and 19.32 Hz. So the experimental procedures and the structure are the same applied previously in a vibration analysis using MEMS and Arduino.

Python programming
The program made in python language must attend the objective of creating an acquisition instrument and provide data storage so the user is able to access, transport and handle the numerical data collected, being free to use it in others software or platforms.
As the python language is vast, there are several useful modules that can manage all the work required by the data acquisition system. Thus, eliminating the need to use more software and others programming languages. Thereby, creating a light program with low computational cost and easy utilization. According to the simple and powerful Raspberry-Pi hardware along the accelerometers.
The most important module is the SMBus, whose utilization aims at controlling the accelerometers by functions which inputs are the accelerometer I2C address, the functions registers and the hexadecimals numbers that define the way the device perform its internal functions which parameters are provided by each accelerometer user guide. All the three show a variety of configurations. Emphasizing that the same module will be used for all the devices, this way there is a possibility of adapting an already completed program so it can work with certain device that offers a I2C digital connection port.
Then, using the module functions, it is possible to send and collect the device data with a proper Python program.
Each function (register) from an accelerometer have its own specific address determined by its user guide in the register map. Not all the functions are configurable, some have only the ability to be readable so can uniquely send information, other functions can only be writable so can just receive configuration parameters and some have both abilities. It is necessary to read all the information from the manuals, so all functions utilities and modifications are known for appropriate usage. The functions inputs parameters are performed by hexadecimal values. In each user guide configurable register there is a way of modifying internal functions. For that reason, when programming, care must be taken to interpret how to activate and disable what is interesting as output. Finally, the adjusted value for the function configuration must be converted from its binary or decimal base to a hexadecimal base that is introduced in a function as input. Then, the registers are used in the program so the measurement is configured according to the user preferences.
Even though the three accelerometers have different internal functions, the digital communication and the modules utilized are the same. Besides that, the logical sequence conducted by the three is the same.
The discrepancy between the accelerometers is in the configuration of measurements parameters and the data outputted conversion. For each accelerometer, its scales define how the digital data are going to be transformed into decimal base numbers that represent a value of acceleration in a known unity of measure. The more relevant differences are going to be explained next for each accelerometer.
As mentioned before, each accelerometer has different registers, which hexadecimals values, obtained from the user manual, are used in SMBus module functions to change the internal configurations. So, the main differences are the values that represent the register and the parameters that the registers can change. In the ADXL345, the most relevant registers, in hexadecimal base are: 0 × 2D, which can change the measurement mode of data; the register 0 × 2C, that alter the bandwidth; and the register 0 × 31, responsible for determining the measuring range. Furthermore, the registers, 0 × 32 to 0 × 37, are the output for accelerations data for each axis. For each one of the three axes, there are two outputs, an LSB (Least Significant Bit) and an MSB (Most Significant Bit) output, both having 8 bits each, that need to be converted, as further explained. For the MPU6050, the most significant registers are: 0 × A1, capable of setting the bandwidth; the 0 × 1C register, which is responsible for the scale adjusting for the measures; and for configuring the measuring mode, the register 0 × 6B. The registers 0 × 59 to 0 × 64 are the acceleration output for each axis, also in LSB and MSB, each one having 16 bits of data.
Worth mentioning that this accelerometer also has a gyroscope which can measure angular velocity in three axes. However, neither gyroscope measurements neither internal thermometer are being used in this project, but both can be easily added by implementing the rights registers in the program. Beyond that, this one diverges from the others in his manual, because the configurations are made from a decimal entry to the proper hexadecimal utilized in the program. Different from the others that the configuration of each of the 8-user defined binary bits, which are too switched to hexadecimal values.
Differently from the others, this accelerometer has scale and measure mode configuration in the same register defined by the hexadecimal 0 × 16. Using the 0 × 18 register, the bandwidth is chosen, in this case, between 62.5 and 125 Hz. The data output is done through the registers 0 × 00 to 0 × 05, which output values have the size of 10 bits and each axis present data in LSB and MSB. In the same way as the ADXL345, the configuration parameters must be determined from a binary base and then converted to the hexadecimal base, the correct input in the functions from the SMBus module.

Data acquisition
Having all the register and the proper configurations parameters, the write functions inside the SMBus module are used to send and determine which scale, bandwidth and measure mode will be assigned. After that, a loop made with the output acceleration data registers is used to gather the data. This loop was based on another used in a project that collected data with Arduino and MEMS accelerometers [17].
To deal with time, the datetime class from the datetime module was chosen due to its accuracy and easy handle of operations in the microsecond resolution. Thus, the loop compares the times between the difference of start from the previous acquisition and the current time so when the period from the user determined frequency is reached a new acquisition is started and the process of comparison initiates again. This loop keeps gathering data until the user determined duration is reached, stopping the measurement.
When using datetime module in time comparison operations, attention should be taken in the fact that only objects of the same kind from these modules can be compared. For that reason, frequency and measure duration inserted by the user, which are "string" class objects, must be converted to datetime class objects, so both can be compared.
In each data gathering, output functions for the three axes are used together with hexadecimal registers and the IC accelerometers registers. Is now that the read functions from the SMBus module are applied with the output data registers for each axis in LSB and MSB and all data from the record is stored in a list, in which each line is a samples and the six columns are the values obtained from the registers. Beyond that, the time when each sample was gathered is stored in another list so it can be displayed with the acceleration to the user. Much importance was given to use low processing memory from the Raspberry-Pi. Therefore, real time data is no shown to the user as this operation would waste memory. Thus, all data is only stored by the list method "append", which add elements to the acceleration and time lists each time data is collect in the loop so computational cost during measurement is reduced. This way, lower is the chance of a gathering take more time than the period required by the frequency inserted by the user.

Data conversion
After the acquisition, all the acceleration is inside a large list and inside each column there is a list containing both LSB and MSB data for each axis. The next step is to apply a function defined inside the program to make the proper conversion for the accelerometer out-put data. This is done for the three axis and for all the values inside the lines which are the samples recorded in each loop and stored in the large acceleration list.
Each accelerometer has a different conversion function because theirs outputs have different sizes. While the ADXL345 and MMA7455 data in 10 bits, the MPU6050 sends it in 16 bits. Thus, each acceleration output in LSB must be combined with the MSB out-put and then multiplied by a scale factor so the output values are turned in to decimal values in the standard gravity acceleration unit.
The conversion function uses python logic operators to manipulate and combine binary data. Depending on the output value a function is used so is necessary one for the 8 bits output and another for the 16 bits. Then, the acceleration list that holds the collected data from the loop is converted into a list with only three columns, one for each axis.
After combined, the values must be multiplied by the scale factors, which transforms data to the gravity acceleration units. The factors change for each scale and output measured so they must be obtained inside the accelerometers devices documentation.
A code implementation allows the user to choose the measurement scale, so the adequate factor is adjusted for the last conversion to the proper magnitude. After that, the values can be easily transformed to the international acceleration unity system (m/s 2 ) by a simple multiplication. Thus, the final values are ready to be stored utilized outside the measurement device program.

Data storage
An easy and well known way to extract and save this already converted data is by creating a cvs ("Comma Separated Value") format file, in which can be read by the most common and free spreadsheet editors, already incorporated in the Raspberry-Pi operational system.
For this purpose, a csv module must be utilized and the data inside the list be converted to string class when writing the new file. The file name takes the actual date and time when the creation is being done to make easier to handle sequential measurements. Beyond that, a loop is used so all lines and columns from the list are placed inside the new archive which is closed and saved when the loop reaches its end.

Signal processing
In the acquisition and experimental signals for the analysis of mechanical vibrations, through accelerometers, it is ideal to analyze not only their representation in the time domain, but also their response in the frequency domain. For the response in the frequency domain, the Fast Fourier Transform (FFT) algorithm was used. In this work, it was used the fftpack module from the scipy library.
Another important implementation was the development of time-frequency analysis through Short time Fourier Transform (STFT), in order to quantify the change in the frequency of a non-stationary signal over time. In this work, it was used the module signal.stft from the scipy signal processing library. All graphics generated by the acquisition system are made with the Matplolib module from the same library.
In order to produce the FFT and STFT it is necessary a time step, that is the sample rate of the measurement. During the data gathering loop, the code compares the previous time that data was collected with real time provided by the Raspberry Pi to assure that the right period of time determined by the user is followed. However, the time collected from the microcomputer comes with its own part of error and commands used in the code take some time to be processed by the computer. Therefore, sample rate is asynchronous and each measurement has different errors in time period that depends of computational processing. As time sample is not uniform, the value used for FFT and STFT is a mean, provided by the division of total gathering operation time and total samples. It is a very complex task to obtain synchronous rates using such a simple wiring connection and depending on computers, even more time accuracy must be pursued in future research.

Experimental Procedure
For the resonance condition, using an unbalanced motor, whose vibration frequency is defined by the voltage supplied which is described by the Figure 4 graph, receiving power from a controllable voltage power source. The data were collected using the measuring instruments for the exciting motor in the frequencies of 4.2Hz, 13.12Hz and 20.4Hz, corresponding respectively to a voltage of 3V, 6V and 9V. All measurements were made within a given interval of 10 seconds, enough time to observe the vibratory nature of the structure in resonance. In order to demonstrate the application of signals with nonstationary characteristics through the STFT, a second experiment conditioned the structure with a motor that started from zero up to the frequency of 20.4 Hz with its corresponding 9V of voltage in the power source of the motor. Then, the same 10 seconds interval applied in this procedure for all accelerometers. The location of the accelerometer sensor is presented in Figure 5.
In the experiment, all three accelerometers must be positioned in the right place, the placement is in the  lower part on the side of the highest plate that made the structure as shown in Figure 6. The Raspberry-Pi was connected to the accelerometer and the necessary display controller and its power source Figure 6.
Each type of measurement must be evaluated prior to determine the parameters chosen to suite the limits Using the MMA7455, the instrument settings for the measurements excited by 4.2 Hz and 13.12 Hz were 62.5 Hz for a bandwidth and 125 Hz for a sampling frequency. For the 20.4 Hz excitation, a bandwidth and sampling frequency respectively set to 125 Hz and 250 Hz.
For the ADXL345 configuration, the 4.2 Hz and 20.4 Hz frequency excitation measurements were made with 50 Hz bandwidth and 100 Hz sample collection frequency. Then for 13.12 Hz excitation, 100 Hz bandwidth and 200 Hz sampling frequency were selected for the acceleration acquisition.
The experimental procedure followed all the instruction to be as close as the procedures of vibration measuring conducted in the previous works using the same mechanical structure. Thus, more details in how to correctly repeat the experiment can be found in [18], which results will be compared to the obtained in this work.

Results
As the results show in Figures 7, 8 and 9 for the measure of the mechanical system in a resonance condition,   the FFT was capable of provide a peak that approaches the frequency expected from the excitation of the unbalanced motor in the three different vibration frequencies (4.2 Hz, 12.99 Hz, 20.89 Hz) provided by the motor using the ADXL345. The other accelerometers presented similar results of acceleration and the same peak for the frequencies in this condition. This peak is observed and comparable to previous works and matches with the results obtained for the measuring instrument from this work in the same experimental procedure. All the comparisons are in evidence in Table 1.
For the starting motor experiment, the results at Figures 10, 11 and 12, the STFT shows that the vibration frequency increases over the 10 seconds interval. It is possible to notice, in all the analyzes made with STFT, the characterization of the 3 natural frequencies of the structure, during the excitation through the motor start, non-stationary regime.

Final Remarks
This article presents an expansion of previous works started in [17,18], which proposed to measure mechanical vibrations using the Arduino microcontroller and low-cost sensors for educational purposes. The results presented in this work show good precision of the acquisition system in the domain of time and frequency, when compared to other instruments and analytical analyzes for the same mechanical structure in two conditions of vibration. Its low cost and small size have proven to be an easy-to-use resource in modal analysis and mechanical vibration applications. One of the great advantages of the proposed system is the fact that the acquisition, processing, and storage of the signals takes place in the Raspberry-Pi itself, in an automated way without the need for external software to export the registered data. With respect to signal processing, the system can perform analysis via FFT and time-frequency analysis using STFT, which allows the analysis of non-stationary signals. The Python language was also shown to be suitable for project implementation due to the ease of communication with the sensors and the construction of data acquisition and signal processing mechanisms using the mentioned libraries. The sensors used showed results in accordance with the literature, as shown in Table 1. Due to the low cost, ease of customization and presented results, the presented framework is very useful for applications in undergraduate and graduate courses graduation in physics and engineering, in addition to presenting the possibility of being used in a wide range of applications, from teaching and research to even industrial. For accelerometers used, there is a maximum data rate, a maximum sample frequency, that the connections can receive and sending between the two devices that make up the instrument. This is because the digital I2C communication between the accelerometers and the Raspberry-Pi is simple but limited in the number of bytes that can be transferred per second. Therefore, that one used for the measuring instrument is suitable only in the case of acceleration measurements whose sampling rate is below the frequency specified for each accelerometer.