**4. Arduino UNO rev. 3**

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

values are compatible with the dynamics of the DAQ;

given by the word with all ones 111....11 i.e. Vmax.

ranges, some data is shown in **Figure 8**.

Q/2 and is defined as quantization error.

2.Choose an appropriate sampling rate;

3.Choose an appropriate resolution;

technology will be presented [3].

the following points must be satisfied:

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

A different number of resolution bits clearly produces different quantization

Clearly the measurement of Q is affected by error and corresponds precisely to

Recapitulate, in order to perform a correct measurement through a DAQ system,

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

1.Prefer a sensor with a linear response and that the maximum and minimum

**86**

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

**Figure 7.**

*Quantization and coding.*

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 Tx/Rx serial communication.

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 models of it: with wifi o ethernet, compact or large model, wearable, etc.

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

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 code to transform Arduino from Master to Slave.

The new role of Arduino will be to be used in LabView environment as a real data acquisition system (**Figure 11**).

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

**Figure 10.** *Arduino-stand alone mode.*

**Figure 11.** *Control hierarchy with LabView-Arduino.*

#### **4.1 From master to slave**

Among of programmer "sketch" is the name that Arduino's programmer uses for a program. It's of code written in like C, compiled and, then, uploaded on the board. After it is possible to run on an Arduino board the code. There are two distinct functions available in Arduino sketch: *setup()* and *loop().*

The *setup()* is called once time only at beginning when the sketch goes in run. It's a correct place to make setup tasks like setting pin modes or initializing libraries.

The *loop()* function is a infinite loop and is heart of most sketches. You need to include your algorithm and functions in it.

Normally in the setup() section there is the sequence of instructions to configure all the Arduino peripherals and features that will be used in the project such as: Analog input, PWM, i2c. In loop(), instead, is written all the control algorithm that will be characterized by an infinite loop.

In this paragraph we propose the development of a code from a different perspective, Arduino will be used as a DAQ system. So inside the setup() there will be a pre-cycle in which the Arduino waits for the USB connection to LabView and waits for the ASCII character sequence to configure the Arduino ports as desired.

The ASCII code, we call op-code from now, to send for configuration are printable characters, so you can always test the Arduino code from any serial terminal or using the serial monitor of the IDE.

For example, to configure the Analog Input channel zero (A0) just send the code "a". Arduino will remain in the setup() section until the master sends the character "z" on the serial which will end the setup cycle to execute the code in the loop().

The code proposes a scenario in which analog inputs A0÷A5, DIO pin2 and pin4 and a PWM channel on pn3 are configurable. Clearly it is possible to extend the "offer" by adding other input or output lines. The complete management of a sensor through Arduino libraries could also be included.

Regarding the sampling time Ts it is possible to define through the ASCII codes A,B,C,D a time delay equal respectively to 100 msec, 10 msec, 1 msec, 500 μsec. If it is omitted the acquisition time is 1000 msec.

**89**

**Figure 12.**

*Code- SerialEvent() and blinking().*

The code developed in the loop() section collects data from the previously configured input line ports, maps them to the following format

#A0#A1#A2#A3#A4#A5\$D0\$D1 and sends the message continuously to the USB

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

Among of programmer "sketch" is the name that Arduino's programmer uses for a program. It's of code written in like C, compiled and, then, uploaded on the board. After it is possible to run on an Arduino board the code. There are two

The *setup()* is called once time only at beginning when the sketch goes in run. It's a correct place to make setup tasks like setting pin modes or initializing libraries. The *loop()* function is a infinite loop and is heart of most sketches. You need to

Normally in the setup() section there is the sequence of instructions to configure

all the Arduino peripherals and features that will be used in the project such as: Analog input, PWM, i2c. In loop(), instead, is written all the control algorithm that

for the ASCII character sequence to configure the Arduino ports as desired.

In this paragraph we propose the development of a code from a different perspective, Arduino will be used as a DAQ system. So inside the setup() there will be a pre-cycle in which the Arduino waits for the USB connection to LabView and waits

The ASCII code, we call op-code from now, to send for configuration are printable characters, so you can always test the Arduino code from any serial terminal or

For example, to configure the Analog Input channel zero (A0) just send the code "a". Arduino will remain in the setup() section until the master sends the character "z" on the serial which will end the setup cycle to execute the code in the loop(). The code proposes a scenario in which analog inputs A0÷A5, DIO pin2 and pin4 and a PWM channel on pn3 are configurable. Clearly it is possible to extend the "offer" by adding other input or output lines. The complete management of a sensor

Regarding the sampling time Ts it is possible to define through the ASCII codes A,B,C,D a time delay equal respectively to 100 msec, 10 msec, 1 msec, 500 μsec. If it

distinct functions available in Arduino sketch: *setup()* and *loop().*

**88**

**4.1 From master to slave**

*Control hierarchy with LabView-Arduino.*

**Figure 11.**

include your algorithm and functions in it.

will be characterized by an infinite loop.

using the serial monitor of the IDE.

through Arduino libraries could also be included.

is omitted the acquisition time is 1000 msec.

#### **Figure 12.** *Code- SerialEvent() and blinking().*

The code developed in the loop() section collects data from the previously configured input line ports, maps them to the following format #A0#A1#A2#A3#A4#A5\$D0\$D1 and sends the message continuously to the USB port. The message will contain as many strings as there are lines configured. In the syntax #Ai (i = 0…5) the value of Ai corresponds to the decimal decoding of the combination of the 10 bits, so there will be 2n combinations. At value 0 will correspond 0 (zero) Volt and at value 1023 will correspond 5 Volt.

WE suggest to the reader to test own system velocity before to set 500 μsec of sample rate. Usually, for my experience, it is very rare to follow with a LabView (not real time) Loop code that velocity. In case the system is not fast enough, one way of not losing data could be the following: change the Arduino's code to collect the msg (measured value) in a vector of 100 elements and send it to LabView each 50 msec. You can choose different size of vector but you have avoid to saturate the Arduino memory.

The op-code (operation code) we have written does not belong to any standard communication protocol. We have invented a sequence of simple ASCIII strings to be sent over serial. So the Master will have at his disposal a set of instructions, which can be extended by the reader, to change the status of a digital output: D0\_ON\n, D0\_OFF\n, D1\_ON\n, D1\_OFF\n.

In order to avoid a slowdown loop() for sensors reading, due at continuous polling on the receipt of messages from the Master, an event-driven solution has been considered.

The reception on the serial line of a request from the Master is triggered by the event generated by the chip that manages the USB communication. When a byte arrives on RX an event is generated and triggered by a software procedure. When this occurs the Master message will be read (**Figure 12**).

In the end we can send a message to set a Analog output by pin3 in PWM mode.

The **Pulse Width Modulation** [4], or PWM, is a powerful technique to control analogic circuits (applied to a load) using a digital signal. It is a type of digital modulation, in particular we speech of pulse width modulation which allows to obtain a variable average voltage depending on the ratio between the duration of the high pulse and the entire period (duty cycle).

In electronics it is used to change the voltage, and therefore the power, on a generic load. For example, to change the speed of a direct current electric motor, to vary the brightness of light bulbs, especially LEDs. A useful duty cycle of 0% indicates a pulse of zero duration, in practice no signal (Vout = 0 volts), while a value of 100% indicates that the pulse ends when the next one begins (Vout = Vcc). To use

**91**

**Figure 14.**

*CODE-variable declaration.*

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

**4.2 Arduino code**

the comments.

has been set at 2 \* 106

between Arduino and the Master.

this technique with Arduino is very simple, with the analogWrite (PIN, VALUE) function it is possible to modulate the work cycle. The PIN corresponds at PWM pins and VALUE is scale from 0 to 255. For example analogWrite (pin, 255) corresponds to a 100% duty cycle and analogWrite (191) is a 75% duty cycle (**Figure 13**).

In the following boxes (**Figure 15**) we'll show some code that you can use to

In **Figure 14** it is possible to understand the functionally of declarations reading

bit/s in order to have the maximum communication speed

In **Figure 15** is possible to verify the **setup()** code, inside it there are the comments to understand it. The code in **Figure 15** has been conceived to be very static and the expansion is very simple for the novice programmer. The serial speed

The code written in the "WHILE LOOP" could be redesigned to treat the Arduino channels dynamically. We want to say that configuration strings (like **pinMode(4, OUTPUT)**) can be sent directly from the MASTER unit.

create a communication Master–Slave from LabView and Arduino [5].

**Figure 13.** *PWM example.*

this technique with Arduino is very simple, with the analogWrite (PIN, VALUE) function it is possible to modulate the work cycle. The PIN corresponds at PWM pins and VALUE is scale from 0 to 255. For example analogWrite (pin, 255) corresponds to a 100% duty cycle and analogWrite (191) is a 75% duty cycle (**Figure 13**).
