( Code )
-->

I was looking for a way to plot real-time Arduino data and after a bit of search I decided to couple it with LabVIEW; but among all the documentation and references I found on the internet about the topic none of them suited exactly my needs.

The documents from NI provides useful information to directly access read/write Arduino pins both analog and digital, but no further details are given to transfer variables back and forth.

If you don’t have Arduino IDE or LabVIEW installed, please follow the instructions from NI page on how to setup the environment.

METHOD 1: SEQUENCE OF CHARS
Common practice when it comes to transfer data over a serial line is to send them as sequence of ASCII characters. The following example show how to send a float variable from Arduino as fixed 4 characters, and read it in LabVIEW using the “Send Receive” utility block.

The trick here is to send a dummy flush command to Arduino, which otherwise respond with a “sync” signal that mess up with actual data.

METHOD 2: BYTE ARRAY
The approach described before is not that flexible as it assumes a fixed number of bytes is exchanged. Moreover data were sent encoded in ASCII and not as C data type implementation.

A more versatile approch would be send data to LabVIEW as sequence of bytes (binary). This would allow to send variable length data and reconstruct them inside LabVIEW, and require proper serialization functions in the firmware.

The following example show how to read a single precision float (4 bytes). A fifth byte is prepended and used as header for the transmitted data. LabVIEW wait for 5 bytes on the serial line, read the first of them and if it’s not equal to 0×69 flush the serial receive buffer and wait for the next packet. If the header is recognized the remaining 4 bytes are swapped to convert Arduino’s little-endian to LabVIEW big-endian format, then casted to a single precision float.

METHOD 3: A SIMPLE PROTOCOL

WORK IN PROGRESS

-->

9 Comments

  1. Nilo

    Hi Stefano: Please how did you modified the arduino LIFA sketch to send MCU memory variable to LABVIEW. Please show us an example.

    • Stefano

      Hi Nilo, you can find the sketches zipped in the downloads available at the top of the page. Please note that no major changes has been done to the LIFA code, only the “Serial.write” in the main loop have been added for these examples to work. Any further suggestion will be appreciated.
      Cheers,
      Ste

  2. Adam___B

    Hello.

    I managed to get your VI to work with my program. I’m sending my variable (data from one axis of my accelerometer) as a string via serial.println. It only works with your second VI (LIFA_test_2), when I convert buffer data straight from Send Receive block to number and then plot it. Otherwise I get 0 all the time. Thing is, when it plots as I said before, it is very fluent with values from -200 to -1 and 10 to 200 (integers only), but for values 0 to 9 it gets significantly slower. What would you recommend to look at in order to improve it? I set “Bytes to read” to 4 (best results so far).

    I uploaded my VI in case you wanted to look at it

    Would very much appreciate an advice, cheers!

    The file:

    http://www.sendspace.com/file/sbarfo

    • Stefano

      Hi Adam,
      first thing to pay attention to is how many bytes we send and receive (you can read previous statement as “LIFA need to know how many bytes has to wait for!” ). We have two way to send data from Arduino: 1) Serial.print(ln) or 2) Serial.write the second one being more low level than the first one.

      With 1) data will be sent as human-readable ASCII text (and as per the following links with sort of default truncation) while with 2) the data are sent as bytes.
      For example to send following Arduino float numbers :
      1.23 1.23456 -12.34
      Serial.Print “1.23″ (4bytes) “1.23″ “-12.34″(6bytes)
      Serial.Println “1.23\r\n” (6bytes) “1.23\r\n”() “-12.34\r\n”(8bytes)
      Serial.Write 1.23 (4bytes) 1.23456(4bytes) -12.34(4bytes)

      The only function that guarantee a constant number of bytes, 4 in case of single precision float, is “write” and for me this is the way to go!

      The LIFA_test_2 show exactly how to send one float (4bytes) over the serial line + a simple header (1byte) : total 5 bytes, always!!!

      Of course that example can be modified to send multiple data / different types at the same time…

      http://www.arduino.cc/en/Serial/Print
      http://www.arduino.cc/en/Serial/Println
      http://www.arduino.cc/en/Serial/Write

  3. Adam___B

    Hello Stefano!

    It works indeed :) I can now send one variable form Arduino to Labview without any lag.

    You said that the code can may be modified to send multiple data (variables) at the same time. How can it be done? Is it about adding cases in Labview with different headers and respectively serial.write commands with their names and variable names in Arduino code?

    Cheers,
    Adam

  4. Stormie

    Hi,

    I’m fairly new to programming with labview, how would I go about it so I could reverse the process and send a variable from LV to the arduino at the same time?

  5. Hi dude,
    Great approach! But I go another approach. I use LIFA firmware and modified as follow:
    case 0×08: // Read Analog Pin
    readAn(command[2]);
    // retVal = analogRead(command[2]);
    // Serial.print( (retVal >> 8), BYTE);
    // Serial.print( (retVal & 0xFF), BYTE);

    Regards,
    AungKL

  6. luis

    hi,
    i’m trying to do this, but always receive “SYNC” , could you help me, i’m using labview 2010.

  7. luis

    hi,
    i’m trying to do this, but always receive “SYNC” , could you help me please?, i’m using labview 2010.

Leave a Reply

Recent