Vous êtes sur la page 1sur 11

Arduino and LabVIEW

This tutorial explans how to connect your Arduino to LabVIEW thought USB.
Youll learn how to send a string and receive data available at USB port.
First of all, C programming skills and LabVIEW diagram block knowledge will help.

LabVIEW
Lets start with labview.
1 Create a new VI in File > New VI.
2 Go to block diagram window. Now we have to select some VI.
3 Serial is a protocol communication. At Data Communication > Protocols > Serial you
pick:

- VISA Configure Serial Port: sets up the serial port.

- VISA Write (2x): writes the data to the device connected.

- VISA Read: read data available at serial port from the device connected.

- VISA Close: closes the connection established.

- Bytes at Serial Port: checks if theres or not data available.

4 Its time to Join the dots


4.1 Create a while loop in Programming > Structures > While Loop. (Pict. 01);

Pict. 1

4.2 Create three cases structures inside of while loop in Programming > Structures > Case
Structure. (Pict. 02);

Pict. 2

4.3 The first two Cases are for writing a string and the last one for reading. (Pict. 03);

Pict. 3

4.4 The VISA Configure Serial Port and VISA Close go outside the while loop. (Pict. 04);

Pict. 4

You can put VISA Configure Serial Port inside the while loop. The example above,
you gotta choose the COM Port first before running the vi, but if you put inside the
loop you can choose the COM Port anytime.

4.5 Visa Bytes at Port goes inside While Loop, We gotta check the bytes at serial port using a
Programming > Comparison > Grater than 0. (Pict. 05);

Pict. 5

4.6 If the bytes at port are greater than 0, the True Case Structure will be activated and the
VISA Read will return the bytes read.
4.7 VISA Configure Serial Port should let the user choose which serial port to use, and set up
the baud rate, so Visa resource name and Baud Rate must be a control. The default baud
rate is 9600. (Pict. 06);

Pict. 6

4.8 You gotta create a command string to wirte to the device, lets use TO for Turn On (LM35)
and TF for Turn Off (LM35). In Create Constant. (Pict. 07).
If you want to send commands from keyboard, you can use just one VISA Write and
create a control for input string. In this tutorial well use buttons.

Pict. 7

5 Go to Front Painel window, lets create an interface for user.


5.1 Well measure temperature using LM35, Itd be nice to have one Numerics >
Thermometer, and a Graph Indicators > Chart. (Pict. 08);

Pict. 8

5.2 At this point you gotta add the buttons (Buttons > OK Button) to turn on/turn off the
sensor.Change the Mechanical Action to Switch Until Released and add num inds (Num Inds
> Num Indicator) to see the temperature.
You can also add some captions like USB Control and LM35; (Pict. 09)

Pict. 9

6 Return to Diagram Block window to set the Buttons, Graph, Thermometer and Num inds.
To show the data received from the usb port, we need to convert string to number:

- Programming > String > String/Number Convertions > Fract/Exp String to


Number: convert string to number. This VI must be added inside the read case
structure
to
make
sure
the
all
data
received
will
be
converted.
The String input is connected to read buffer of VISA Read. (Pict. 10)

Pict. 10

7 Now connect the Gaph, Thermometer and Num ind to the Number output of Fract/Exp
String to Number. (Pict. 11);

Pict. 11

8 All you have to do now is connect the turn on button to the first case structures, the turn off
button to the second case and stop button to While Loop Condition. (Pict.12)
9 Due to the fast process, you need to add some delay in the while loop. Add a delay
Programming > Timing > Wait (ms) to wait 600 ms, to make sure the all data has been
received, once the arduino will refresh the data every 500ms (See Arduino Code). (Pict. 12)
Picture with VISA Configure Serial Port inside the while loop.

Pict. 12

Optional:

- You can create a sytem to check if the usb port is available, or even to check if
theres a kind of error with the COM Port.
This (Programming > Dialog and User Interface > Find First Error) VI return true if
theres an error, then if the COM port didnt return an error, its available, otherwise, an error
was thrown (Pict. 13)

Pict. 13

- You can create an error handler (Programming > Dialog and User Interface >
Simple Error Handler). Itll indicate an error, where it is and a description of the
error. (Pict. 14).

Pict. 14

- You can also reset the VI eveytime it runs, just use the VI Server Reference (Programming
> Application Control> VI Server Reference), and Invoke Node (Programming >
Application Control> Invoke Node). (Pict. 15)

Arduino Code
Now were gonna create an arduino code (C Programming Skills).
1 First you gotta define all variables and pins youll use, in this case:
- 1 led (Arduinos LED);
- 1 LM35.

2 Void Setup.

3 And the Void Loop.

4 You can see in the picture that there are two function, TempOn(); and TemOff();. They
control the sensor.
4.1: TempOn(); is a function that starts the measure:
Im not going to explain how LM35 works, but you can check the datasheet right here;

You can use other sensor or even just turn on/turn off leds, all you have to do is change the
functions;

4.2: TempOff(); is a function that stops the measure:

Code Explanation:
The Void loop is the most importante part of the code. Itll check if theres something at the
Serial port, thats why Serial.available(); is used to check bytes.
If its available, a string of those bytes must be created, then Serial.read(); do the trick and the
code line string =+ string + command; store the full command.
Thats it folks.

Vous aimerez peut-être aussi