Tuesday, February 11, 2014

Wireless (XBee) and serial communication, data acquisition and Labview


With this experiment I want to monitor some analog sensors and display the values on a graphical interface on my PC. There are many stages involved and there is much to learn in all of them. 

Arduino

First of all I need to wire up and program the Arduino to read the sensors. In this case I'm using six potentiometers to simulate alanog sensors. I want to display these values on a small LCD attached to the board, so I will compare them with the values displayed on the PC. To do that I need to import the LCD library at the beginning of the sketch. 
The string sent from the Arduino to the PC must contain a fixed number of digits for every variable. As I explained in a previous post, the values from the sensors vary from 0 to 1023, which means they can use one to four digits. I programmed the Arduino to divide all of these values by 1023, multiply by 900 and add 100. The result is a number between 100 and 999. So the string is always 21 digits long and each 3 digits represent a value. The opposite operation has to be done by the software on the PC to display the original values. Then I send these values to the PC using the serial connection.


This is the sketch I wrote

#include <LiquidCrystal.h>

LiquidCrystal lcd(10, 8, 7, 6, 5, 4);

void setup() 
{
  Serial.begin(9600);
  lcd.begin(16,2);
}

void loop() {
    String dataString = "";

  for (int analogPin = 0; analogPin < 6; analogPin++) {
    float sensor = analogRead(analogPin);
   int value = sensor / 1024 * 900 + 100;
   dataString += String(value);
   }
  Serial.println(dataString);

  lcd.println(dataString);
  delay(15);
}

Wireless communication

I'm using XBee radio transmitters to send data using radio frequencies. To connect the Arduino to the XBee module you need an Arduino Wireless Shield, and to connect the other XBee module to the PC you need a XBee Explorer board (see picture below). Each module has to be configured using the Explorer board and X-CTU, a free software distributed by Digi International. To communicate, the two modules have to use the same ID (the name of the network), and same baud speed; one have to be a coordinator and the other can be router, end device or both. In this case I set the one attached to the PC as coordinator and the one attached to the Arduino as end device. They can be configured to create very complex mesh networks, but I don't need it now.
I don't need to modify the sketch on the Arduino because the serial data flows through the XBee module. The wireless shield has a switch: it must be on “USB” when using serial to upload a sketch on the microcontroller, and on “MICRO” when using the XBee to send data.
The XBee module I've chosen are: XBee PRO series 2. They use 50 mW power at 2.4 GHz frequency (legal in Australia). The range of this model is about 3 km outdoor. 
I bought all the components from http://www.australianrobotics.com.au/, and I think they have good items, reasonable prices and an excellent service (I talked to them because I thought the explorer board didn't work). Here are all the parts I bought, but you can chose different combinations:

X-CTU has a "terminal" window where we can see the data received on the PC, but only one software at a time have access to the serial connection (Arduino IDE, X-CTU or LabVIEW).


The following pictures show the data string as transmitted to serial (read by the Arduino monitor), and as transmitted wireless (read by the X-CTU terminal). The left one shows broken data because the Arduino was working at maximum speed. So I had to add a delay of at least 15 milliseconds in the sketch and the data was received properly as in the right picture.



LabVIEW

I was able to get a student copy of LabVIEW so I’ll try it. It’s used in SCADA (Supervisory Control And Data Acquisition), for example to monitor and control oil rigs in the middle of the ocean, from an office in the city centre. It’s also used in military and space applications. The programming language of LabVIEW is graphical; it’s like a flow diagram, but not that easy. Sparkfun sells a kit with an Arduino UNO board and an old version of LabVIEW (here). That’s not the way I got it but I think it’s a good deal. Getting started with LabVIEW is easy: you have to use two windows, one to create objects graphically, and the other to connect them together and make them work.
To learn the programming language of LabVIEW I found these tutorials very useful.
LabVIEW needs another software to be able to read serial data. It's called VISA Shared Components and it's not included in the basic software. I had it installed for a coincidence (it was together with my dive computer software).
I noticed that LabVIEW has a library to program the Arduino boards, so I installed it and I had a look. You can use it to control the Arduino, but the program is not deployed on the board. So, to work, the Arduino has to be connected to the PC and LabVIEW, which is not what I want now.
I created a basic interface where the values are displayed graphically.



To read the serial data you need the VISA function. I tried some examples from the National Instruments tutorials, but they were all slow, the data was updated once a second. So I tried this example and the data rate was very quick. The configuration of the serial port is as follow:


The following picture shows the main loop:


The pink line is the string read by the VISA function. This string is then divided in substrings and converted to numbers. The operator can set a maximum value, when the value from the sensor goes over that limit, an alarm lights up. The white icon is a group of functions I've put together in one to make it easier to repeat it for every value. This group of functions is shown in the following picture:


It converts the string to number, calculate the original value and the max value.


Conclusions

I’ve set up my Arduino Uno to send data through serial port. The Arduino reads analog inputs from potentiometers (they simulate real sensors, sending an analog voltage) and keeps sending a string with all the data together through the serial port. The XBees modules send this data wireless to the PC. Labview reads this string (VISA function) and takes every single data out of the string. The single data is then displayed graphically.





3 comments:

  1. sir thanq for your post..can you give me the vi file of your project. i m not able to find it on ni .com page. they have remove it :(

    ReplyDelete
  2. https://drive.google.com/open?id=0B-Ivl-qrWZ-DVk1CalRjUkMtWEU
    https://drive.google.com/open?id=0B-Ivl-qrWZ-Dc1NlWkJNUlU3a1U

    ReplyDelete
  3. Hi .. I really like your project. Will it be possible for you to pass me the code?

    ReplyDelete