**3. Sampling and coding**

## **3.1 Sample and hold**

The S&H system is the circuit part that performs the sampling of the signal (sampling phase). Sampling, in signal theory, is a technique that consists in converting a continuous signal in time into a discrete signal, evaluating its amplitude at regular time intervals. Therefore, considering that the physical quantity attributed to the physical phenomenon varies continuously over time without any interruption, it is necessary to decide with which time interval to interrogate the sensor in order to have meaningful data for our measurement.

From the definition of the sampling interval (Tc) for the scan we derive the sampling rate:

$$f\_c = \frac{1}{T\_c} \tag{1}$$

**83**

signal we need:

*LabView and Connections with Third-Party Hardware DOI: http://dx.doi.org/10.5772/intechopen.96056*

medium sized car, is 180 km/h.

travels at maximum for 10 cm (**response time**).

preserving its main characteristic, its frequency.

**3.2 Sampling theorem (or Nyquist-Shannon theorem)**

the stop of the car before the impact.

should be double of the fmax.

one of the vision sensor must be less 2 ms. These involves

2.How high has to be the sampling rate if we would like to create automatic braking for anti-collision car system? Assume that max velocity, for small/

:3,6

Let us assume that the control system reacts in such a time that the car still

So, if we make some calculations, the time between one reading and the next

The proposed cases are at the antipodes: while in the first one we do not have any criticality, in the second one there is a big responsibility due to the need to manage

In the real world, according to the mathematician J. Fourier, an analogue signal can be represented by linear combination of sinusoidal functions (called

The first harmonic, called fundamental, has the same frequency as the input signal, while the following harmonics will have a frequency multiple of the fundamental. The transition from the analogue to the digital domain, therefore discrete, leads us to acquire one of these harmonics, of course the first one, therefore a suitable sampling frequency will be the key to a good acquisition of the analogue signal,

In order to have a correct sampling (without loss of information) we must to choose a correct frequency of sample rate. Supposing that the frequency signal (first harmonic) is *fmax* then Nyquist-Shannon Theorem says that the minimum sampling frequency *fs*, that preserve the frequency information of a original signal,

If, in addition to the frequency, we would like to storage also the shape of the

180 / *km h*⇒5 / 0 *m s* (2)

*cf Hz* > 500 (3)

*<sup>s</sup> f f* ≥ 2 max (4)

Question:

*S&H circuit and example.*

**Figure 5.**

Answer:

**harmonics**).

The effect of the circuit in **Figure 5** is to store the analog value taken at a given time (sample phase) and keep it constant for as long as it takes the converter to perform the conversion (hold phase).

But how fast should the sampling rate be? Clearly it depends on the phenomenon we are observing. See two examples below:

1.We want to monitor the temperature of a room to stabilize it at a value of Tset ± error. Considering the inertia of the room and the radiators it makes sense to acquire the temperature every second i.e. fc = 1 Hz.

*LabView and Connections with Third-Party Hardware DOI: http://dx.doi.org/10.5772/intechopen.96056*

**Figure 5.** *S&H circuit and example.*

*LabVIEW - A Flexible Environment for Modeling and Daily Laboratory Use*

The multiplexer (commonly called MUX) is a selector of data lines (analog or digital) able to select different input signals: once selected the channel, the corresponding signal is collected and sent on the output line. There are some particularly performing and expensive devices that do not use the MUX but they have a

The S&H system is the circuit part that performs the sampling of the signal (sampling phase). Sampling, in signal theory, is a technique that consists in converting a continuous signal in time into a discrete signal, evaluating its amplitude at regular time intervals. Therefore, considering that the physical quantity attributed to the physical phenomenon varies continuously over time without any interruption, it is necessary to decide with which time interval to interrogate the sensor in

From the definition of the sampling interval (Tc) for the scan we derive the

*c*

1.We want to monitor the temperature of a room to stabilize it at a value of Tset ± error. Considering the inertia of the room and the radiators it makes

sense to acquire the temperature every second i.e. fc = 1 Hz.

*c*

The effect of the circuit in **Figure 5** is to store the analog value taken at a given time (sample phase) and keep it constant for as long as it takes the converter to

But how fast should the sampling rate be? Clearly it depends on the phenomenon

*<sup>f</sup> T*<sup>=</sup> <sup>1</sup> (1)

complete acquisition chain for each input.

order to have meaningful data for our measurement.

perform the conversion (hold phase).

we are observing. See two examples below:

**3. Sampling and coding**

*Example of a 4-input multiplexer.*

**3.1 Sample and hold**

**Figure 4.**

sampling rate:

**82**

Question:

2.How high has to be the sampling rate if we would like to create automatic braking for anti-collision car system? Assume that max velocity, for small/ medium sized car, is 180 km/h.

Answer:

$$180\,\mathrm{km}/\,\stackrel{:3,6}{\mathrm{h}\Longrightarrow} 50\,\mathrm{m}/\,\mathrm{s}\tag{2}$$

Let us assume that the control system reacts in such a time that the car still travels at maximum for 10 cm (**response time**).

So, if we make some calculations, the time between one reading and the next one of the vision sensor must be less 2 ms. These involves

$$f\_c > \\$00Hz \tag{3}$$

The proposed cases are at the antipodes: while in the first one we do not have any criticality, in the second one there is a big responsibility due to the need to manage the stop of the car before the impact.

In the real world, according to the mathematician J. Fourier, an analogue signal can be represented by linear combination of sinusoidal functions (called **harmonics**).

The first harmonic, called fundamental, has the same frequency as the input signal, while the following harmonics will have a frequency multiple of the fundamental.

The transition from the analogue to the digital domain, therefore discrete, leads us to acquire one of these harmonics, of course the first one, therefore a suitable sampling frequency will be the key to a good acquisition of the analogue signal, preserving its main characteristic, its frequency.

#### **3.2 Sampling theorem (or Nyquist-Shannon theorem)**

In order to have a correct sampling (without loss of information) we must to choose a correct frequency of sample rate. Supposing that the frequency signal (first harmonic) is *fmax* then Nyquist-Shannon Theorem says that the minimum sampling frequency *fs*, that preserve the frequency information of a original signal, should be double of the fmax.

$$f\_s \ge \mathcal{2}f\_{\text{max}} \tag{4}$$

If, in addition to the frequency, we would like to storage also the shape of the signal we need:

$$f\_s \ge \mathfrak{F}f\_{\text{max}} \tag{5}$$

The sampling rate is normally expressed in Sample Rate and the unit of measure is number of samples per second [#S/s].

To understand better how the theorem works in the **Figure 6** we report a sequence of acquiring with several sampling rate. The software used is developed for university student's lectures [2].

**Figure 6** shows how the sampling frequency acts. We start with a 440 Hz source signal (resonance frequency of a conventional tuning fork) which is visible in the first waveform graph of the sequence. In the following sequences the following sampling frequencies were used: 440 Hz, 600 Hz, 880 Hz and 2200 Hz.

In the first and second cases the fs is not adequate, in fact we have an undersampling. In the third case we have a result that preserves the frequency of the input signal. Finally, in the last case, we have reconstructed quite faithfully the profile of the original signal.

#### **3.3 Coding**

The last sequence in the DAQ chain (**Figure 2**) consists of the operations performed by the A/D converter: quantization and encoding. First we need to introduce the concept of signal dynamics. The dynamics of the signal indicates the maximum excursion of the signal and, therefore, also the maximum and minimum values it can reach, the range of Vin (also defined as the Full Scale value):

$$\mathbf{V}\_{\rm FS} = \mathbf{V}\_{\rm max} - \mathbf{V}\_{\rm min} \tag{6}$$

#### (we have assumed a voltage signal)

The input signal, being continuous in time, can by definition take on an infinity of values. As well as the sampler has discretized the signal in time (X axis) we now need another circuit which discretizes the values of the physical quantity which represents the information (Y axis). So the technique is to approximate the value acquired in the sampling phase to a discrete value. The number of discrete values available for these approximations is given by a very simple calculation. If we choose *n* bit to make a digital conversion then the number of discrete value is 2n .

At this point we have to define the unit of quantization that we call quantum or quantization step, that is the smallest approximation interval that we use to compare the sampled signal to discretize it.

$$Q\left[V\right] = \frac{V\_{\text{FS}}}{\text{2}^{\text{u}}} \tag{7}$$

**85**

**Figure 6.**

*Signal sampled with different fs.*

*LabView and Connections with Third-Party Hardware DOI: http://dx.doi.org/10.5772/intechopen.96056*

Q is called quantization step. It is possible to assert, at this point, that a higher bit number and a smaller VFS interval implies the greater number of intervals available. This means that the size of the interval will tend to be an extremely small value with increasingly accurate measure.

The simplest coding (commonly used for unipolar signals, i.e. always positive ones), natural binary code (straight binary), consists in making each quantization interval correspond to a progressive binary number, starting from 0 (corresponding to the lowest level) up to 2n -1.

*LabView and Connections with Third-Party Hardware DOI: http://dx.doi.org/10.5772/intechopen.96056*

*LabVIEW - A Flexible Environment for Modeling and Daily Laboratory Use*

is number of samples per second [#S/s].

compare the sampled signal to discretize it.

value with increasingly accurate measure.


to the lowest level) up to 2n

for university student's lectures [2].

the original signal.

**3.3 Coding**

The sampling rate is normally expressed in Sample Rate and the unit of measure

**Figure 6** shows how the sampling frequency acts. We start with a 440 Hz source signal (resonance frequency of a conventional tuning fork) which is visible in the first waveform graph of the sequence. In the following sequences the following

In the first and second cases the fs is not adequate, in fact we have an undersampling. In the third case we have a result that preserves the frequency of the input signal. Finally, in the last case, we have reconstructed quite faithfully the profile of

The last sequence in the DAQ chain (**Figure 2**) consists of the operations performed by the A/D converter: quantization and encoding. First we need to introduce the concept of signal dynamics. The dynamics of the signal indicates the maximum excursion of the signal and, therefore, also the maximum and minimum

(we have assumed a voltage signal)

The input signal, being continuous in time, can by definition take on an infinity of values. As well as the sampler has discretized the signal in time (X axis) we now need another circuit which discretizes the values of the physical quantity which represents the information (Y axis). So the technique is to approximate the value acquired in the sampling phase to a discrete value. The number of discrete values available for these approximations is given by a very simple calculation. If we choose *n* bit to make a digital conversion then the number of discrete value is 2n

At this point we have to define the unit of quantization that we call quantum or quantization step, that is the smallest approximation interval that we use to

> [ ] *FS n*

Q is called quantization step. It is possible to assert, at this point, that a higher

The simplest coding (commonly used for unipolar signals, i.e. always positive ones), natural binary code (straight binary), consists in making each quantization interval correspond to a progressive binary number, starting from 0 (corresponding

bit number and a smaller VFS interval implies the greater number of intervals available. This means that the size of the interval will tend to be an extremely small

values it can reach, the range of Vin (also defined as the Full Scale value):

To understand better how the theorem works in the **Figure 6** we report a sequence of acquiring with several sampling rate. The software used is developed

sampling frequencies were used: 440 Hz, 600 Hz, 880 Hz and 2200 Hz.

*<sup>s</sup> f f* ≥ 5 max (5)

*VV V FS* = − max min (6)

*<sup>V</sup> Q V* <sup>=</sup> <sup>2</sup> (7)

.

**84**

**Figure 6.** *Signal sampled with different fs.*

In **Figure 7** we show what we have said, on the X-axis we put the intervals between Vmax and Vmin and beside them the bit combinations. The first level consists of all bit to zero, so the word 000...00 corresponds to Vmin while the last level is given by the word with all ones 111....11 i.e. Vmax.

A different number of resolution bits clearly produces different quantization ranges, some data is shown in **Figure 8**.

Clearly the measurement of Q is affected by error and corresponds precisely to Q/2 and is defined as quantization error.

Recapitulate, in order to perform a correct measurement through a DAQ system, the following points must be satisfied:


The premises made so far are useful to better understand the code written for the Master unit and the slave unit. In this chapter we propose an cheap and open source prototyping board for which we will write some code to transform it into a DAQ. The proposed board is Arduino UNO rev.3. In the next paragraph, the Arduino technology will be presented [3].

#### **Figure 7.**

*Quantization and coding.*


**87**

**Figure 10.**

*Arduino-stand alone mode.*

**Figure 9.** *Arduino UNO rev.3.*

*LabView and Connections with Third-Party Hardware DOI: http://dx.doi.org/10.5772/intechopen.96056*

code to transform Arduino from Master to Slave.

data acquisition system (**Figure 11**).

Arduino Uno (**Figure 9**) is a microcontroller board (Italian open source project) based on the ATmega328P (resolution @10 bit; input range 0÷5 V). It has 14 digital input/output pins (of which 6 can be used as PWM outputs), 6 analog inputs, a 16 MHz like internal clock (sample rate = ~10 kS/s), a USB high speed connection, a power jack 9 Volt input, an ICSP header, reset button and several states LED like

It contains all interfaces needed to support the microcontroller and its functionality; You can use prototype board with your Uno without worrying about doing something wrong, worst case you can replace chip with a new one and start over again. The Uno board is the first USB Arduino boards, today are available several

Wiring is an open-source programming framework for microcontrollers C/C++

The developer, under conditions of classical use, writes code for Arduino in order to have a "machine" that works in Stand Alone, in **Figure 10** is shown his working scheme, the code runs on Arduino, through the code reads the sensors and produces actions on the physical world. In the next paragraph will be discussed the

The new role of Arduino will be to be used in LabView environment as a real

models of it: with wifi o ethernet, compact or large model, wearable, etc.

**4. Arduino UNO rev. 3**

Tx/Rx serial communication.

based.

**Figure 8.** *Resolution example.*
