## Introduction: A Basic Course on Data Acquisition.

I sometimes give seminars on Motorcycle Dynamics and an important part of that is the subject of data acquisition (DAQ). In racing, the motorcycles are nowadays equipped with sophisticated DAQ systems which measure a range of parameters from temperatures, pressures, suspension displacement and many more. However, it has been my experience that very few operators understand the fundamental aspects of DAQ such as the sampling rate, filtering and required resolution of the sampling hardware and software.

Students in other fields are using DAQ on various science and engineering projects also, often unaware of potential pitfalls and sources of error. This Instructable (which is entered in the “Classroom Science contest”), gives some lessons in the basic aspects. The target audience is technicians and science or engineering students. These lessons will look at measuring the basic physical quantities of acceleration, velocity and displacement. In the rest of this course they will be referred to as A, V and S respectively.

We will look at the sensors that we can use and see how some can be easily made in a school lab or workshop and I will use my own shock absorber dynamometer as a source of repetitive movement cycling for demonstration measurments . Any other mechanism that gives repeated cyclic motion will serve equally well. A simple crank or cam mechanism is easy to make for those wishing to copy the measurements. In my own workshop I mostly use a Labjack for acquiring the data and communicating it to a PC for analysis, but in the interests of accessibility for students I prepared these lessons using the ubiquitous Arduino. The Arduino sketch code is included.

Normally on the shock dyno only displacement and velocity are needed and I do not need to measure acceleration. Many similar machines do not measure velocity directly either, preferring instead to calculate that from the measured displacement. We will look at where it is appropriate or not to use calculated data channels. These lessons are not about a shock dyno or any other specific application, it was just a convenient way for me to use its motion to demonstrate some aspects of DAQ.

The following lesson plan is not divided by an estimate of time for each lesson but by particular topics. In order to limit the number of illustrations in each step for clarity, some lessons will be spread over multiple steps in the Instructable. Calculus is deliberately not used because it is not necessary and would exclude some students.

## Step 1: Lesson Plan and Resources

1. Numeric relationships between the three parameters of acceleration, velocity and displacement.
2. Fundamentals of sampling, such as rate, resolution, noise, aliasing and filtering.
3. Sensors and their connection to DAQ hardware. DIY sensors.
4. DAQ hardware. Acquisition and recording on mobile platforms and Acquisition in a lab or workshop environment.
5. Analysis of data and what we can do or not do with calculated channels.
6. Summary and review.
7. The Aduino sketch and PC pseudo code.
8. The testing setup.
9. Some DIY sensor making.

Resources used.

• Motion source. I used a shock dyno but there are many options limited only by imagination.
• Instrument amplifier chip AD623. The data sheet can be found here https://www.analog.com/media/en/technical-documen... There are other equally good options and fully assembled boxed instruments.
• Accelerometer GY-521/MCU 6050 on breakout board.
• LVT ( Linear Velocity Transducer) I made my own and will show how easy that is.
• Displacement sensor. I used what is known as a resistive string potentiometer but there are many options.
• Arduino Uno. Other flavours of Arduino, Pi or other micro will serve equally well. Dedicated DAQ hardware is recommended if available.
• PC or laptop. I used Windows7 but a Linux or mac box would work with appropriate analysis software
• Analysis software. I wrote a simple programme using Delphi (Pascal for Windows) specifically for this Instructable.

## Step 2: Lesson 1. Numeric Relationships Between A, V and S

Although many sensors are analog in nature, the output is usually sampled at discrete time intervals and passed through Analog to Digital (A to D) converters. That is the case with the sensors used in this course.

The illustration shows some X data graphed against time divided up into equally spaced time intervals. The data value at each point from 0 to maximum is identified with a subscript such as X[n + i]. These points in time are where the DAC system samples the data and either stores it or passes it to the analysis software.

We know that for a constant acceleration we can express the A, V and S relationship as follows:

V = Vo + A * dt and S = So + V * dt

In other words when an object is subject to constant acceleration, the current velocity V is equal to its initial velocity Vo plus the added velocity due to the acceleration A over the time period dt.

Similarly the position of an object subject to a constant velocity over the time interval is its initial position + the displacement due to the velocity over the time interval.

From the illustration above we can see that if the sampling interval is small then the parameter value over that time interval is close to being constant. If the time interval is a bit larger then taking an average value between two samples gives us a value which we can substitute for a constant value, provided that the interval is still short enough for the data to approximate a straight line. We get the following relationships using the subscript notation and expressions above, where “dt” is the time interval between samples:

V[n] = V[n-1] + (A[n-1] + A[n])/2 * dt

Which means that the velocity at sample “n” is equal to the velocity at the previous sample plus the average acceleration during those two sample periods multiplied by the time interval. With similar meaning we can express the position or displacement as:

S[n] = S[n-1] + (V[n-1] + V[n])/2 * dt

So using these relationships we can calculate the velocity over time if we only measure acceleration, and we can calculate displacement if we have velocity or acceleration data. This is known as numeric integration, which is the discrete version of integration in calculus, the difference being that in calculus the time intervals are infinitesimally small. We will see that this process of numeric integration helps smooth out noise in the calculated parameters.

We can also calculate in the opposite direction, by a process of numerical differentiation. That is, if we measure only displacement we can calculate velocity and acceleration, or if we only measure velocity we can calculate acceleration by numeric differentiation but also displacement using numeric integration.

V[n-½] = (S[n] – S[n-1])/dt and

A[n- ½ ] = (V[n] – V[n-1])/dt

The [n- ½] needs explanation. When we do the numerical integration we calculate the value at each discrete sample. On the other hand when we do the numeric differentiation the value we get is midway between two samples. So if we simply assumed that the value was at the nth sample then the acceleration would be out of step or phase referenced to the sampled data by (+ ½ * dt), or half a sampling period. In many cases this would be of little importance but depending on what calculations are done down the line it might make a huge difference.

It is often inconvenient to think or calculate in terms of ½ samples so we can simply correct the phase difference by averaging the [n - ½] and [n + ½] values as per:

V[n] = (V[n + ½] – V[n - ½])/2

This will put the data back in phase and is a trivial exercise inside the analysis software.

In contrast to the data smoothing that we get with numeric integration, numeric differentiation increases the noise in the calculated parameter. This limits the usefulness of the technique.

## Step 3: Lesson 2. the Basics of Sampling.

Sampling is the process of taking measurements of physical data at discrete and defined time intervals. The illustration in the first lesson shows what this means. If we are measuring multiple parameters we would ideally like to measure all at exactly the same time or at least so close to the same time that the phase error is only a small proportion of the sample period. Sometimes when we need greater phase precision we have to adjust for the delay between the sampling of each parameter.

As our sampled data is just a collection of data points we could (some say “should”) plot the data as a collection of points but it is more usual to plot the data by drawing lines between successive sampled data points to get a visual representation of the original analog data.

Sampling rate.

This is an extremely important parameter, if we want get accurate data there are many factors to consider.

If we wish to sample some data with variations at principally around 50 Hz. We might think that we could sample at a little over that frequency, there would certainly be no need to sample at 1 MHz or 1 MS/s (S/s is “Samples per second” and is a better term than Hz for DAC use). However, there is a theorem know as the Nyquist theorem which states that you need to sample at least double the rate of the highest frequency of interest in the data that you want to sample. That sounds easy, in our case of 50 Hz data being of interest we could sample at 100 S/s.

There is a problem with that due to any higher frequencies in the data. This maybe due to noise or even real data outside of our range of interest..

Sampling noise

There will always be some noise in our sampled data and there are many sources of this such as noise on the physical object being measured, noise created at joins in the sensor wiring, to random electron movements deep inside a sensor (some applications require sensors to be held at cyrogenic temperatures to reduce such effects). There are also airbourne electromagnetic fields from electric supply wiring, motors running, radio and TV transmissions etc. All of these can often be reduced to small levels by shielding cables, sensors, attention to earthing etc. but regardless of how clean our analog data is there is one source of noise that is inherent in the A to D conversion.

Consider the Arduino Uno, the on board A to D converters are 10 bit. That means a maximum resolution of 1 in 1023, around 0.1%. Imagine that we sent a signal with a value of 523, that will not create a problem because it will be converted into a digital version of 523. Now imagine that the next two data samples are a little different at 523.49999 and 523.51111, these values get rounded by the A to D process to 523 and 524 yet the real values only varied by 0.00002. So we get a variation of “1” for an input difference of “0.00002”. This means that on our digital data will have a random noise of +/- 1. For the Arduino this is +/- 1 in a full scale of 1023. Using a 12 bit A to D that will be +/- 1 in 4095 and 16 bit hardware it will be +/- 1 in 64535. The above photo show the zoomed peak of the displacement data, it can clearly be seen how the values jitter about due to this effect.

This is called sampling noise, it is inevitable and we can see that we can reduce it proportionally by using better resolution in the A to D conversion process. In many cases 10 bit conversion is perfectly adequate but if we need to create a calculated data channel by numeric differentiation then the noise will be amplified in the calculated parameter. We will see this in the demonstration data in a later lesson.

Aliasing.

We are all familiar with aliasing but perhaps unaware of the reasons behind it. We are witnessing aliasing when we see wagon wheels appearing to rotate backwards in Western movies. Movie making is a sampled data system, the camera takes frames at fixed intervals and what we see is only the situation at the time of the frame capture. Imagine that we label the spokes from 1 to n with n being the total number of spokes. Say that spoke 1 is at the top for the first frame then if the wheel does a little less than one revolution by the time that the next frame is taken then it will appear that the wheel has moved backwards by a little, as filming continues the wheel will continue to look like it is running backwards unless the wagon speed varies and the relationship between spoke position and frame rate changes.

Unfortunately it can get more complicated, the wheel might do multiple revolutions in between frames. Imagine that it does 6 complete revolutions plus a partial one. Then it will look identical to the case of only the partial revolution. The wheel will be in the same position...So we do not see the increased speed of 6 and bit revolutions compared to only a bit of a revolution even though there is a near 7:1 actual wheel speed difference. The answer to this is to use a very high frame rate as we see with slo-mo videos.

It gets even more complicated. If Spoke 2 is just before where spoke 1 was at the last frame then our eyes will not know if that spoke is 1, 2 or spoke 6. In other words we are generating harmonics of the wheel speed which we cannot separate from the actual wheel speed. If we painted spoke 1 with white or yellow paint and the other spokes black or the background colour. Then we will see only spoke 1. We have filtered out the other spokes.

Exactly the same happens when sampling physical data. Say that we are sampling a cyclic parameter if our sample rate is too slow, around ½ of the data frequency, we take a sample at a point in the data cycle then the next sample will be taken from two cycles further on. We miss sampling anything in the second cycle. In fact the Nyquist theorem demands that we take at least 2 samples during a cycle of the highest frequency in order to reconstruct what the data looks like.

The 2 samples per data cycle is hardly ever enough though, noise and real data at frequencies higher that those of interest will be reflected back into the data of interest just as the images of multiple spokes on wagon wheels can get mistaken for spoke 1. There are two approaches to improve this.

The first is akin to painting out the other spokes, that is we filter unwanted higher frequencies before the A to D conversion takes place, this will have to be a hardware filter, known as an Anti-alias filter. The damage will be done if we try to do it with software after the A to D conversion. However this technique is only applicable to sensors with an analog output which is accessible before feeding into the A to D converter. Many sensors now do the A to D on the chip and send the data over one of the many serial protocols such as SPI, IC2, CAN etc. We cannot hack the internal connections, but some sensors have filters built in.

The second is to sample at a high rate then some of the data reflected back will land on frequencies above our range of interest leaving a smaller amount to reflect back into the required data. Higher sampling rates require better and more expensive hardware, this is a two pronged fork. Faster hardware and sampling rates will produce a greater volume of data to be stored and transmitted

So we can see that sampling rate is controlled by many factors, and we have only considered some, and ends up being a compromise.

## Step 4: Lesson 3. Sensor Hardware.

Position/displacement

I will tend to use the word displacement for both position and displacement. In the context of this document they are more or less the same.

There are many methods of measuring displacement depending on need, budget and availability. These include laser, ultrasonic, capacitive, optical encoders, resistive etc. Here we will only consider resistive solutions. These are just a potentiometer packaged in different forms. Race motorcycles use a linear packaging as shown in the main photo, but for the demos here we use a string pot which is basically a multi-turn circular potentiometer operated by a cord over a wheel. The string pot is useful in a workshop or lab because it is quite flexible in its mounting requirements. A string pot is shown in the next photo fitted to the shock dyno. The scaling or calibration factor is easy to determine because the movement stroke was known accurately to be 50 mm peak to peak. So in the analysis software it is a simple matter to calculate a scaling factor.

## Step 5: Lesson 3. Continued

Velocity

Again there are several methods for measuring this, but we will only consider an LVT (Linear Velocity Transducer). This comprises of two coils, either counter wound or counter connected and a magnetic rod. It works on the principle that when you move a magnetic field in a coil a voltage will be induced which is proportional to the velocity. These require no external power source. They are available commercially or they can be made very easily. The demos here were made with a DIY unit, shown in the first photo. At least some commercial models have a large number of turns on the coil and produce outputs of several volts without amplification, but they also have large inductance. I wanted a low inductance device and limited the number of turns, which has the effect of reducing the output to milli-volts not volts. The mV output requires an instrument amplifier to bring the output up to a level suitable for the Arduino. Unlike the string pot I had no direct way of getting a calibration factor but by numerical intergration of the velocity to get displacement, the calculated displacement could be compared to the accurately known displacement and scaled accordingly. This also agreed closely with the velocity calculated from the acceleration.

Here is an example of a commercial item https://transtekinc.com/product-categories/linear...

Acceleration

We will use a DY521/ MCU6050 breakout board. Which was further mounted on a support board with cable attached as shown above.There are many sources of these on the internet at very low cost. They are quite incredible devices, on a 4 mm square chip there are 3 accelerometers, 3 gyros, a temperature sensor and an SD card socket. Internally there is software for calculating a variety of spatial data. It seems almost wasteful to only use a single axis. These chips can be used in +/- 2, 4 and 8 g mode. The default 2 g was used here. At 2 g the output is 32767 but when orientated vertically then gravity uses up -1 g so leaving a useful range of -1 g to +3 g. +/- 1 g is quite adequate for the current task. Unlike the string pot and LVT which output analog signals the MCU 6050 uses an IC2 interface. The Arduino has IC2 functions built in so it is just a matter of coding a sketch to read the data values. The sketch is shown in a later lesson.

The scaling factor of 32767 = 2 g proved quite accurate.

## Step 6: Lesson 4. DAC Hardware

There is a huge range available for this data collection application, some are fixed rack mounted laboratory instruments with built in filters and other advanced features. These need a budget of \$1000s. At the other end of the scale are the Pi.s and Arduinos. The LabJack which I normally use is a dumb device, that is, its job is to collect data under the command of some PC software. The LabJack does not need any user software to be loaded. I mounted a LabJack in a box and brought most connections out to a D25 connector, this allows me to quickly switch the LabJack between jobs because there is no need to download different software for each purpose. Shown in the second photo.

The Arduino has smarts but that requires that it must be loaded with software (a sketch) to collect and transmit data to software on to a PC. A different sketch is needed for each different application.

Evaluating the performance parameters of mobile devices like cars, motorcycles etc. demands that the DAQ system needs to be small and mobile and able to withstand physical and electrical noise and shock. That is a tough environment and high class equipment is necessary. A typical unit is shown in the first photo.

For this current course I used an Arduino in combination with a breadboard to make a purely prototype system. Connection details are shown in photos above.

## Step 7: Lesson 5. Results and Data Analysis.

A later lesson will give details of the Arduino sketch and PC software. The following looks at numerous aspects of the measured data. A single measuring run of about 10 seconds long was performed but only the first 3 seconds are plotted as an aid to clarity with the 3 times expanded time scale. No hardware filters were used for simplicity. We will look at all the raw data and all the combinations of calculated channels using numeric integration and differentiation. The software has some moving average filtering and results are shown with and without that filtering.

Raw data.

This is shown individually for the three parameters of A, V and S. The V and S plots show an inset zoomed in to show more detail of sampling and other noise.

It is clear that the displacement gives the cleanest signals and the noise appears to be mostly sampling noise. The velocity shows more noise and there appears to be a lot that is not sampling noise. The velocity sensor is more sensitive to external mechanical noise because only very small movements are needed to produce significant velocities and hence noise. There was machinery running in another part of the building. The acceleration data is full of noise and looks to be totally useless. An accelerometer is very sensitive to mechanical input, if it is sitting on a table just moving some papers nearby will show up. We will see if anything useful can be extracted.

## Step 8: Lesson 5 Continued

Displacements.

We have 3 displacement traces, one from the string pot, in red, another from numeric integration of the raw velocity, in blue, and the last is a two stage numeric integration of the raw acceleration, in green. We can see that the direct and the integrated velocity are a close match. The twice integrated acceleration has lost most of it noise but is not such a good match. There is a logical reason for this. Both the string pot and LVT are two ended devices, they measure relative motion between the fixed and moving ends of the shock absorber mounting yokes, so if the dyno moves in response to the running motor and out of balance forces on the crank, then that movement is common to both ends of the sensors and so will be cancelled out. This is known as common mode noise. On the other hand the accelerometer has a single attachment and will see any movement of the whole machine and fixed end and add that to the acceleration measured off the moving yoke. This will increase both random noise and reduce the accuracy of the data. This type of error is enforced as we do successive numeric integrations as is the case here. This problem can be solved or reduced by putting a second accelerometer on the fixed yoke and deducting that reading from the first. Using a single accelerometer was a good way of showing that you do not always get the pure data that you want. Despite that you will be surprised to see what was hiding amongst the very noisy data. Considerable thought, care and validating is always necessary if you want to have valid results. This cannot be stressed to much.

## Step 9: Lesson 5 Continued

Velocity

We have 3 Velocity traces, one from the string pot numerically differentiated , in red, another from the raw velocity, in blue, and the last is a numeric integration of the raw acceleration, in green.

It can be seen that numerical differentiation of the raw displacement data gives a very noisy velocity trace, even though the raw displacement data was fairly clean. The second case shows what happens when the raw displacement data is filtered. We get what might be an acceptable result, greater filtering would smooth the results more but would increase the chances of hiding some real data.

## Step 10: Lesson 5 Concluded

Acceleration

We can see that with differentiating raw displacement we get a signal which drowns out the other data. The second illustration shows just the velocity and acceleration without the displacement derived data. The acceleration trace is raw data and the blue velocity derived acceleration is from numerically differentiating raw velocity. The third picture shows all the accelerations from the contributing parameters filtered in software. Although the raw displacement data is the smoothest of all, even when filtered the double dose of differentiation creates a lot of noise.

## Step 11: Lesson 6. Summary and Conclusions.

We have seen how we can measure the three physical quantities of acceleration, velocity and displacement using simple sensors. It was shown that from just a single one of these we can use numerical methods to calculate the time history of the other two parameters either by numerical integration or differentiation as appropriate. Calculating displacement from either velocity or acceleration gives data which is smoother that the direct measurement. The random noise gets filtered in the integration process.

On the other hand it was noticed that getting acceleration by numeric differentiation of velocity and particularly the double stage differentiation of displacement leads to increased noise to the point that the data would be useless unless the original raw data was filtered. The problem with excessive filtering is that you lose fine detail in the real data and not noise.

We considered and used 3 different types of sensor connection viz: a potentiometer for displacement which fed directly into the Arduino A to D converter, but needed a voltage supply of 5 V from the Arduino. The LVT needed no power supply but did need an instrument amplifier and the accelerometer chip communicated with the Arduino via an IC2 connection. Low output sensors like my DIY LVT need the additional hardware complication of an instrument amplifier. Sensors with a serial connection like the IC2, CAN etc. need some extra complication in the software. Analog sensors like the string pot give an analog output at a level which is natively compatible with the DAQ system are are generally the easiest to use. Ease of use should not be the sole consideration when selecting a connection system. There are many factors involved and reference to the sensor manufacturer's data sheets is very important.

For anyone who wants to further their knowledge of DAQ and digital signal processing in general I would highly recommend the book shown in the above photo. There is no ISBN number but it can be found on the internet. It is a bit mathematical but that is the nature of the subject.

## Step 12: Lesson 7. Arduino Sketch and PC Pseudo Code.

The Aduino sketch feel free to modify it to your own needs. Note that I used the Y axis of the accelerometer which is the second one after X. so there is a throw away read of X to reach Y.

-----------------------------------------------------------

#include "Wire.h" // This library allows you to communicate with I2C devices.

const int MPU_ADDR = 0x68; // I2C address of the MPU-6050. If AD0 pin is set to HIGH, the I2C address will be 0x69.

int16_t acceleration;

unsigned long myTime ;

int dT; // Time interval between data records

void setup() {

Serial.begin(115200);

Wire.begin();

Wire.beginTransmission(MPU_ADDR); // Begins a transmission to the I2C slave (GY-521 chip)

Wire.write(0x6B); // PWR_MGMT_1 register

Wire.write(0); // set to zero (wakes up the MPU-6050 / GY-521)

Wire.endTransmission(true);

myTime = 0;

}

void loop() {

Wire.write(0x3B); // starting with register 0x3B

Wire.endTransmission(false); // the parameter indicates that the Arduino will send a restart. As a result, the connection is kept active.

Wire.requestFrom(MPU_ADDR, 2*2, true); // request a total of 2*2=4 registers

dT = micros()-myTime ;

myTime = micros();

// serial.print data

Serial.print("S "); //Signals start of record to PC software

Serial.print(dT);

Serial.print(" "); Serial.print(acceleration);

Serial.println(" E"); //Signals end of record to PC software

}

-------------------------------------------

Pseudo code of PC software.

Create arrays for

Data records from Arduino.

Time steps

Raw acceleration, velocity and displacement

Backups for Raw acceleration, velocity and displacement // To be able to undo filtering.

6 x arrays for calculated channels

Open appropriate comm port.

Get manually set baud rate to match Arduino sketch.

Establish serial connection with Arduino.

Wait for data from Arduino.

Search incoming data for “S” which signals start of a record.

Continue reading data until “E” which signals end of record.

Write each record to an array, stripping the S & E.

Loop

Separate individual parameters in each record into separate arrays for dT, acceleration, velocity and displacement.

Prepare the calculated channels and fill other arrays.

Until end of data

Plot raw data.

Wait for and act on plot selection or filter selection interrupts.

Replot

## Step 13: Lesson 8. Testing Setup.

The 3 sensors were added to the shock dyno. The main photo shows the accelerometer board fitted to the moving yoke. The second shows the mounting details of the string pot and LVT. The third photo shows the crank mechanism for driving the bottom yoke. The stroke of that is adjustable from 20 mm to 100 mm in 10 mm steps.

## Step 14: Lesson 9. Some DIY Sensor Making.

This course is about data collection and analysis not about sensor making but for those interested I show an idea for a simple DIY string pot and the making of the LVT.

A simple string pot could be made from a normal rotary potentiometer, by fitting a short drum to drive it. If the perifery of the drum was grooved some appropriate cord, maybe fishing line, could be wound around it. A tension spring could be fitted to one end of the cord leaving the other for connecting to the object being measured. A simple calculation would determine the minimum drum diameter needed for a given stroke, considering the angular limits of the pot. The first two photos show the idea. I have not actually tried this and I am certain that it would not be as good as commercially sourced linear or string pot for serious work but it should be fine for educational purposes.

The other photos show some aspects of the LVT without much explanation except to say that a rod magnet is connected to a moving shaft which is fitted inside a double coil. Participated in the
Classroom Science Contest