Introduction: Arduino and LabVIEW

This instructable is a quick tutorial explaning how to connect your Arduino to LabVIEW thought USB.

You’ll 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.

You will need:

  • Arduino UNO;
  • LM35 (Or other sensor);
  • LabVIEW Software with NI-VISA Driver;

This tutorial will not explain how LM35 sensor works. See its datasheet.

What's LabVIEW?

LabVIEW (Laboratory Virtual Instrument Engineering Workbench) is a visual programming language developed by National Instruments. It's very useful for data acquisition (purpose of this tutorial), instrument control, industry automations.

Arduino?

Arduino is an open-source computer hardware used to develop interactive objects, taking inputs from a
variety of switches or sensors, and controlling a variety of lights, motors, and other physical outputs.
Arduino projects can be stand-alone, or they can communicate with software running on your computer (e.g. Flash, Processing, MaxMSP.) The boards can be assembled by hand or purchased preassembled; the open-source IDE can be downloaded for free. (from Arduino).

Step 1: LabVIEW: VI's

LabVIEW.

1ª Create a new VI in File > New VI.

2ª Go to block diagram window. At Data Communication > Protocols > Serial select some VI.

3ª 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 there’s data available.

Step 2: LabVIEW: Structures and Joining the Dots

4ª It’s time to “Join the dots”

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

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

4.3 The first two Case are for writing a string and the last one for reading. (Pict. 3)

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

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. To activate the Reading case, it has to check if the bytes at serial port are greater than 0 using a Programming > Comparison > Grater than 0 VI. (Pict. 4)

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. 4);

4.8 Create a command at VISA Write > Write Buffer. That string will be written to USB Port. This tutorial uses TO for Turn On and TF for Turn Off . (Pict. 5)

If you want to send commands from keyboard, you can use just one VISA Write and create a control for
input string. This tutorial uses buttons.

5ª Go to Front Panel window to create an user interface.

5.1 To show the data received (temperature), It’d be nice to have one Numerics > Thermometer and a Graph Indicators > Chart. (Pict. 6)

5.2 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. 7)

6ª Return to Diagram Block window to set the Buttons, Graph, Thermometer and Num inds.
To show the data received from the USB Port, it needs to be converted from string to number:

  • Programming > String > String/Number Convertions > Fract/Exp String to Number: converts string to number. This VI must be added inside the read case structure to make sure that all data received will be converted.
  • The String input of this VI is connected to read buffer of VISA Read. (Pict. 8)

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

8ª 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.10)

9ª Due to the fast process, add some delay in the while loop 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. 10)

VISA Configure Serial Port is inside the while loop in the 10th picture.

Step 3: LabVIEW: Optionals

  • You can check if the USB Port is available, or even check if there’s a kind of error with the COM Port.
    This (Programming > Dialog and User Interface > Find First Error) VI return “true” if there’s an error, then if the COM port didn’t return an error, it’s available, otherwise, an error was thrown (Pict. 1, 3)
  • You can create an error handler (Programming > Dialog and User Interface > Simple Error Handler).
    It’ll indicate an error, where it is and a description of the error. (Pict. 2,3).
  • 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. 4)

Step 4: Arduino Code

1ª Define all variables and pins you’ll use. In this case:

- 1 Led (Arduino’s LED);

- 1 LM35.

char command;
String string;
#define led 13
#define lm A1

2ª Void Setup.

void setup()
  {
    Serial.begin(9600);
    pinMode(led, OUTPUT);
  }

3ª Void Loop.

void loop()
 {
    if (Serial.available() > 0) 
    {string = "";}
    
    while(Serial.available() > 0)
    {
      command = ((byte)Serial.read());
      if(command == ':')
      {
        break;
      }
      
      else
      {
        string += command;
      }
      
      delay(1);
    }
    
    if(string == "TO")
    {
      TempOn();
    }
    
    if(string =="TF")
    {
      TempOff();
    }
 }

4ª In the code there are two functions, TempOn(); and TempOff();. They control the sensor.

4.1: TempOn(); is a function that starts the measure:

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

void TempOn()   
{  
          int x = analogRead(lm);
          float temp = (5.0*x*100.0)/1024.0;
          Serial.println(temp);
          digitalWrite(led, HIGH);
          delay(500);
    }

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

void TempOff()
    {
      digitalWrite(led, LOW);
      delay(500);
    }

Code Explanation:

The void loop is the most importante part of the code. It'll check if there’s something at the Serial Port, that’s why Serial.available(); is used to check bytes.

If it’s available, a string of those bytes must be created, then Serial.read(); do the trick and the code line

string =+ string + command; 

stores the full command.

That’s it folks.

Step 5: Download

LabVIEW 2012 VI;

Arduino Code;

If there's some errors, please correct.

Thanks.

Comments

author
silth made it! (author)2015-09-28

Thanks for the tutorial!

Captura de pantalla 2015-09-28 a las 11.43.50.pngCaptura de pantalla 2015-09-28 a las 11.43.46.png
author
AhmedF114 (author)silth2017-02-21

smith, i have done the code, but there is problem with in arduino UNO. the code is right could you discuss with me what happen. i wrote code and upload it on arduino what next. is there mistake in hardware.

author
Deyson (author)silth2015-10-12

:) great

author
AhmedF114 (author)2017-02-21

could you please justify the hardware connection

author
tmoir (author)2015-02-19

Can you provide the Labview Vis please, it is hard to follow your diagram

author
Deyson (author)tmoir2015-02-20

Thanks for commenting. I'm going to fix the tutorial diagram.

https://www.instructables.com/id/Arduino-and-LabVIE...

If you want for a different version, just ask.

author
AhmedF114 (author)Deyson2017-02-21

VI file for 2010 please

author
Bilal Mubdir (author)Deyson2016-02-10

Yes I want it in 2010 version if you don't mind

author
IanM180 (author)2016-04-28

hey i have a question. What is ASRL6::INST?

author
BillyA23 (author)IanM1802017-01-18

Thats a NI Visa Device

author
maverick_head (author)2016-11-01

Hi,

do you have a VI for an BMP180 Pressure Sensor thats used to create a Hight-Diagram instead of Temperature?

Thanks

author
izzetshn (author)2016-10-19

Hey,

Can anybody explain the code little bit more?
---------------------------------------------------------------------------------------------
if (Serial.available() > 0)

{string = "";}

while(Serial.available() > 0)

{

command = ((byte)Serial.read());

if(command == ':')

{

break;

}

else

{

string += command;

}

delay(1);

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

whats happening here?.

Thanks.

author
MoleiW made it! (author)2016-02-23

Hi,

There is a small err in your vi file. For the write cases, you put the string as "LT" and "DT", which should be replaced by "TO" and "TF". The tutorial is right, just the attached VI.

Thank you, the tutorial is very nice and helpful.

Capture.JPG
author
andromeda2020 (author)2016-02-12

Hi, can I get the VI file? It is a little hard to follow your instructions thanks .

author

ooops silly me the download link is there at the end of the tutorial

author
JangTaeho (author)2015-07-28

Hello!

I am trying to communicate between ArduinoDUE and Labview for DC motor feedback control.

Now, i can control DC motor using PWM signal given to DUE from Labview.

But, when i receive the encoder signal from DUE through VISA function,

the encoder signal is not stable. It jumps frequently.

I wnoder if you can help me.

thank you.

/////////////////////////////////////////////////////////////Arduino code/////////////////////////////////////

int R_in1 = 22;//Motor driver pin1
int R_in2 = 24;//Motor driver pin2
int R_PWM = 2;//PWM out pin
long readNumber = 0;
float A;
float In1, In2, pwm, newtime;

float newposition1 = 0;

unsigned int Aold1 = 0;
unsigned int Bnew1 = 0;

unsigned int Aold2 = 0;
unsigned int Bnew2 = 0;

volatile long encoder1Pos = 0;
volatile long encoder2Pos = 0;

int encoder1PinA = 52;//Encoder A pin
int encoder1PinB = 53;//Encoder B pin

void setup() {

pinMode(R_in1, OUTPUT);
pinMode(R_in2, OUTPUT);

Serial.begin(115200);
Serial.flush();

pinMode(encoder1PinA, INPUT);
digitalWrite(encoder1PinA, HIGH); // turn on pullup resistor

pinMode(encoder1PinB, INPUT);
digitalWrite(encoder1PinB, HIGH); // turn on pullup resistor

attachInterrupt(encoder1PinA, d1EncoderA, CHANGE);
attachInterrupt(encoder1PinB, d1EncoderB, CHANGE);
}

void loop() {
if ( Serial.available()) {
// cast the string read in an integer
readNumber = Serial.parseInt();
}
newposition1 = ((encoder1Pos/19)/52)*2*PI; //Encoder signal
digitalWrite(R_in1, LOW);//Motor rotation direction control
digitalWrite(R_in2, HIGH);//Motor rotation direction control
analogWrite(R_PWM, readNumber);//PWM write
Serial.println(newposition1);
//delay(5); //50 ms loop interval
}

////////////////////////////////////////////////////////////////////////
//Encoder Read///////////////////////////////////////////////
void d1EncoderA()
{
Bnew1^Aold1 ? encoder1Pos++ : encoder1Pos--;
Aold1 = digitalRead(encoder1PinA);
}

void d1EncoderB()
{
Bnew1 = digitalRead(encoder1PinB);
Bnew1^Aold1 ? encoder1Pos++ : encoder1Pos--;
}

///////////////////////////////////////////////////////////////end/////////////////////////////////////////////////////

Image.JPG
author
Bilal Mubdir (author)JangTaeho2016-02-10

Dear friend, may I have a copy from your .vi file??

author
Bilal Mubdir (author)2016-02-10

Hello friend,

Would you please recompile the .vi file in 2010 version? because I have Labview 2010.

Thank you in advance

author
KiokoS (author)2015-12-12

Thank you I used it in Ultrasonic Sensor and t worked well with me. Is it possible to send data to the x-axis as well? As I want to graph distance gotten from the ultrasonic sensor versus angle gotten from servo motor.

Thank you in advance

author
OğuzhanC (author)2015-10-26

Thank you for your clear and accurate instructions

author
tomatoskins (author)2015-02-18

Oh how I miss LabVIEW! Great memories from high school!

author
Deyson (author)tomatoskins2015-02-18

:)