In my previous article, I have made the Bridge Monitoring System using Wireless Sensor Network project, which allows me to display all the related measurements from Arduino on my PC. Therefore, in this article, I am going to explain in details on how to display the Arduino measurements using Laboratory Virtual Instrument Engineering Workbench (LabVIEW). Two analog inputs of Arduino will be measured and displayed on a graph in LabVIEW.

A brief introduction to LabVIEW, according to Wiki, LabVIEW is a system design platform and development environment using graphical programming made by National Instruments. In other words, it can be used for various purposes such as creating graphical user interface (GUI), programming and simulation, hardware interface and data acquisition (DAQ).

Even though there are many platforms or tools available that can be used to create GUI for Arduino, my personal preference is using LabVIEW, mainly is due to the my previous experience with LabVIEW. The graphical programming is also an advantage as it eliminates the need to learn a new programming language. Creating user interface is also an easy task as LabVIEW was designed to enable engineers without heavy programming background to easily configure and display measurements from DAQ devices.

To get started, I will explain what is actually going on in Arduino. In this project, I am using an Arduino Nano to acquire signals and send the data to PC. As mentioned earlier, two analog input channels (A0 & A1) will be used to measure input signals. To ensure an accurate measurement is performed at fixed sample rate, the Arduino is configured to wait the predefined interval before taking a measurement and send to PC serially. The concept used is similar to the BlinkWithoutDelay example in Arduino. The benefit of using this method is that there is a while loop that always checks if it has crossed the desired interval. If it is reached, it will take the measurement, else it will skip and you can make it to work on other task.

Here is the code for Arduino, it is also available at my github.

 


/*==========================================
 * Arduino Labview Voltage Display
 * Created by zxlee (iamzxlee.wordpress.com)
 *
 * Demonstrate how to display Arduino analog
 * inputs, A0 and A1 to Labview using Serial
 * communication
============================================*/

#define input0 A0
#define input1 A1

unsigned long previousCount = 0;

/**==========================================
 * Constants for interval between samples
 * 50000 microseconds = 50 milliseconds
 * equivalent to 20 samples per second
 *
 * Sample rate is controlled using the
 * method similar to example BlinkWithoutDelay
============================================*/

const unsigned long interval = 50000;

void setup()
{
 Serial.begin(115200);
 delay(1000);
}

void loop()
{
 unsigned long currentCount = micros();

//Calculates the time difference since last serial data sent
 unsigned long countDiff = currentCount - previousCount;

 if(countDiff >= interval)
 {
 //Create a buffer to store string
 char buffer[50];

//Format the data into string
 sprintf(buffer, "$%d,%d,%u\r\n", analogRead(input0), analogRead(input1), countDiff);

 //Write string into COM port
 Serial.write(buffer);

//Update new previous count
 previousCount = currentCount;
 }

}

In this code, Arduino will continuously check the microseconds since the beginning of program using micros() and compare to a previous checkpoint if it is greater than interval defined. This time interval is the sample period, Ts desired in microseconds. The figure below is used to describe the concept of sampling (you can skip this part as it is not required to interface Arduino and LabVIEW, but I feel that it is interesting to introduce the concept of  fixed sampling on Arduino here).

Sample
How sampling works

Sample period, Ts is the time interval between each successive measurement. Taking the reciprocal of sample period is the sample rate or sample frequency. Sample rate, fs is a measure of how many samples is taken in 1 second. From the diagram at t = 0s, the first sample is taken. Then it will wait for a time given by the sample period before taking the second measurement at t = Ts. Similarly, after the second sample, the third sample will occur at t = 2Ts. For a measurement, it is desired to have a fixed sample rate so that we can know what is really happening at the exact time and also able to reconstruct the actual signal.

Back to the code, I have configured the Arduino to measure at the sample rate of 20 samples per second (S/s). With that it is also important to note that this sample rate is only able to reconstruct any signals at half of the sample frequency given by Nyquist Shannon sampling theorem. In simpler words, if my sample rate is 20S/s, I am only able to measure signals up to 10Hz. Beyond that, aliasing will occur where higher frequency signal appear as lower frequency content.

string format
String format sent by Arduino serially

Once the measurement on analog input A0 & A1 is made, this measurement together with the actual time interval is formatted into a string stored in the buffer[] variable. This buffer has a header, ‘$’ and terminates with ‘\r\n’ which is used to move the cursor to the beginning of new line. Within this string, the information is stored in the position in the <>. Finally, this buffer is written serially to the COM port using Serial.write() and we are ready to move on to LabVIEW.

For this project, I am using LabVIEW 2015 to develop the VI. VI or virtual instrument is the file format used to store the program routine developed. In order to interface with Arduino through serial communication, you also need to install the NI-VISA driver. VISA allows you to communicate with instrumentation busses such as USB, Serial or Ethernet.

To give you a big picture of what it looks like for the GUI design, the front panel looks like this.

front panel default
Front panel

Breakdown of the elements in front panel:

  • COM Port – used to select the COM port of devices that you would like to open
  • Loop iterations – indicate the nth loop in the Labview
  • Loop period (ms) – how long it takes to execute each loop in Labview
  • Bytes remaining – how many bytes of data left in the PC serial buffer
  • New Serial Data – the latest string read from COM port, configured to terminate at ‘\r\n’
  • Arduino sample rate – the actual time interval sent from Arduino is used to measure the actual sample rate
  • Serial monitor – a display mimic the functionality of Serial Monitor from Arduino IDE
  • Error Out – display any error and it’s error message
  • Input waveform – a chart to reconstruct the waveform based on the samples measured by Arduino
  • Stop – used to halt the VI

And here is the block diagram of the VI. A block diagram is where you ‘draw’ your program using blocks and connect them using wires. This looks a bit complicated or scary, don’t worry, I am just adding a bunch of comments (blocks in yellow with text) to describe each section.

Block Diagram snippet
Labview VI block diagram

The program start from left to right according the flow of wires. Starting at the left, the COM port is configured with baud rate 115200, 8 bit mode with no parity. The serial communication has a timeout of 10s. And since I had defined the string format, the serial interface is set to read until every termination character and dump for further process.

serial configure
Serial port configuration

Once the serial port is configured, the main loop will read 1000 bytes from the COM port. However, since it was configured to read anything until the term char, it will always return only the string specified. Then a property node is used to check how many bytes remaining in the serial buffer. Ideally, this should be 0 or at least some bounded value. If this value is growing, sooner or later, overflow will occur and some data will be lost. This will happen if the serial device is sending faster than it is read from the PC buffer.

serial read
Serial read

The data read from the serial buffer is passed to a ‘Scan from string’ block. This block will convert the string input based on the format specified and return the individual values from the string. In this case, you can see that two signed 16 bit integer (I16) and an unsigned 32 bit integer (U32) are read. The two I16 corresponds to the two reading from analog inputs and U32 is the time interval taken by Arduino loop.

scan from string
Scan from string

After the two variables are read from the string, they are combined into a cluster to display on the graph. It’s that simple.

cluster to waveform
Combine data into a cluster and display on a waveform chart

To show how the system works, I am using a myDAQ to generate signals to the Arduino analog inputs. I will cover more on myDAQ in the future article. For now, myDAQ will act as a function generator using the analog output feature. Arduino A0 is fed with a sine wave with 2V peak to peak at 0.5Hz with 2.5V offset and A1 is fed with a sine wave with 1V peak to peak at 1Hz with 2.5V offset. The overall system diagram is as below.

Block diagram
System block diagram

Once the VI is executed, this is how the front panel looks like. You can see that there are two sine wave from the graph indicate by two different colours. Besides that, the graph is also set to display actual samples points. In digital domain, the actual samples are just the individual points. The ‘analog’ waveform seen is actually an interpolation and reconstruction by the software to visualise the actual signal measured.

display front panel
Front panel after VI is executed

All the Arduino codes and Labview files are available at https://github.com/zxlee/arduino-labview-voltage-display. Basically this is how you can create and visualise your Arduino data easily using LabVIEW. In my opinion, both Arduino and LabVIEW are very powerful tools which you can consider in your next projects. I hope this has covers much of the details needed for you to create your own. If you have any questions, feel free to drop me a comment below.

If you find this article is useful, give a thumbs up so I know what my readers actually interested and share this article with your friends.

Advertisements