Arduino Serial Write and Read using LabVIEW

and read data between Arduino board and a computer using labVIEW

How to serial write and read data between Arduino board and a computer using labVIEW

I will show how to communicate with an Arduino board using LabVIEW.

I have made a simple example to demonstrate how to communicate via serial port between an Arduino board and a computer using NI LabVIEW software.
Download ZIP Arduino sketch and LabVIEW project.

Intro
You can set in LabVIEW software a number N and communicate it via serial to the Arduino board. The board read the number N and elaborate a random number between 0 and N. Finally, the board communicate the random number n to the computer via serial and the LabVIEW software plot the data.

Hardware
I have used an Arduino YUN board, but you can use the Arduino you like !

Arduino Sketch
In the Arduino sketch firstly you set the serial port baud rate, check for a serial communication and then generate a random number to write via serial.


/**********************************************************************
 Arduino serial write and read with LabVIEW
 How to read a number from serial, do something in Arduino, 
 and write the result via Serial for LabVIEW
 author Physics Light
 date 09 July 2014
 license Creative Commons 4.0 share alike 
************************************************************************/
long randNumber;
long readNumber = 1;
void setup() {
 // init serial port baud rate
 Serial.begin(9600);
 // generate different seed number if pin 0 is unconnected
 randomSeed(analogRead(0));
}
void loop() {
 // check serial 
 if ( Serial.available() ){
 // cast the string read in an integer 
 readNumber = Serial.parseInt();
 }
 // generate a random number from 0 to the number read 
 randNumber = random(readNumber);
 //write the result via Serial for LabVIEW
 Serial.println (randNumber);
 delay(100);
}

LabVIEW project
In the LabVIEW project you have to auto detect the Arduino board connected to your computer. Note that you must set the right Arduino board name. You must also use the same serial port baud rate that you have set in the Arduino sketch.

If everything works, you can run the VI and set the number N into write string box, then you can read and plot the random number n sent via serial from the Arduino board

Download ZIP Arduino Serial Write and Read using LabVIEW.
I have moved the software on github repository. You can view all the resources at this link on github.

Advertisements

22 comments

  1. By far essentially the most succinct and also up-to-date information I found about this topic matter. Certain pleased that I stumbled upon your write-up by opportunity. I will likely be signing up on your rss feed so as I’ll receive the newest posts. Enjoy everything right here.

    1. Thank you, I really appreciate your comment ! I am going to add rss feed asap

  2. BUG fixed
    Serial.println (randNumber) add new line to data stream in Arduino code

  3. Howard Matis · · Reply

    Thanks for this code. I modified the code, so every time there is a write to the Arduino. In this case the first time that LabVIEW runs, it finds that there is no serial data. Help, I need to issue a write and immediately read it back. From what I see, I would need to issue the command twice.

    1. Hi Howard, you are welcome !
      I might be wrong but I don’t understand well how you have modified the code.

      It seems that you want to read the data with LabVIEW “only ” when you wrote to Arduino before. Am I right ?

      In this case, I think you have to check if there are data at the serial port before you read in the labVIEW software. Let me know if it works

  4. I’m trying to run this code as it is (I replaced the auto-detect instance with a string constant “ASRL4::INSTR” for my Arduino on COM4). I uploaded the Arduino program and then ran the LabView program, and it seems to have trouble using the same COM for both devices. I keep getting a Configure Serial Port error:
    VISA: (Hex 0xBFFF0072) The resource is valid, but VISA cannot currently access it.
    Can you please offer any advice on getting it to properly set up the necessary serial connections?

    1. Hi Duncan, maybe the arduino IDE serial monitor is still working while you try to launching the program ? Otherwise I will try to fix the issue

  5. Jang Taeho · · Reply

    Hello!
    I am trying to serieal communicate between the arduino DUE and labview for control DC motor.

    Now, i can control DC motor using PWM made from labview.

    but when i receive encoder signal from arduino DUE for feedback control, signal is not stable. It jumps frequently.

    Could you give me the help, please?
    or check my code please?

    thank you!

    1. Hi Jang,

      I have not a great experience with encoder signal from Arduino, but I can take a look to your code. Please post your sketch

  6. Extremely interesting piece of writing

  7. My code is this,

    How read that chinese characters read in serial monitor in labview and convert into numbers?

    String foot = “L”;
    String data;

    void setup()
    {
    Serial.begin(57600);
    pinMode(A0,INPUT);
    pinMode(A1,INPUT);
    pinMode(A2,INPUT);
    pinMode(A3,INPUT);
    pinMode(A4,INPUT);
    pinMode(A5,INPUT);
    }

    void loop()
    {
    data = foot
    + “,”
    + String(analogRead(A0))
    + “,”
    + String(analogRead(A1))
    + “,”
    + String(analogRead(A2))
    + “,”
    + String(analogRead(A3))
    + “,”
    + String(analogRead(A4))
    + “,”
    + String(analogRead(A5));
    Serial.println(data);
    delay(20);
    }

  8. Can you explain to me why the input 50 can be converted to 36?

    1. It could be strange, try to explain:
      N = 50 is the number you set in write string box, send by PC to Arduino
      n = 36 is a random number 0 < n < N = 50 in read string box, send by Arduino to PC

  9. The labview program can’t detect my Arduino Uno despite it being recognized by the Arduino IDE. What might the problem be? I’ve specified the correct name in labview.

    1. You should check the name of Arduino as specified by your PC.
      Open windows Device Manager > Ports (COM & LPT) > The name of the board
      – Specify in the LabVIEW project in the field istr name the exact string The name of the board

      Also
      – close the serial monitor of Arduino IDE before launching the labVIEW program

      cheers

  10. Ryan Smith · · Reply

    Thank you so much, I successfully used your method and adapted it to my encoder reading project. Sorry to ask an off-topic question, but how did you get that nice white background for your labview front panel? Looks nice. Also what style are you using for your Labview controls / chart? They are very clean looking and I don’t see them in the normal palette options. Thanks again!!

  11. Thank you very much! This post helped me solve my problem in less than an hour, and Iearnt more about LabView. Thank you again.

    1. you are very welcome ! thank you for the feedback

  12. Thanks very much

  13. Hello, I am doing my final year project i want to view the real-time data continuously running in arduino in the labview can u reply as fast as u can. The sensor values are digital displaying in serial monitor.

    1. you can use this software with little modification:
      – set the Arduino name
      – delete the write button and the case structure in the LABVIEW code panel
      This should work for your application

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: