LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to indicate two different sensor values from Arduino serial in lab view?

Solved!
Go to solution
Hello everyone,
As I am reading data from Arduino serial port in lab view and I want two values to be indicate separately but not able to achieve. Need a help to separate the data coming from the VISA read vi so that I can display temp and ultrasonic values separately.
Please find the code I used in Arduino attached below.
Thank you.
 
 
#include <DHT.h>
#include <DHT_U.h>

#include <HCSR04.h>

int ultra_tri = 3;
int ultra_eco = 4;
int DHTPIN = 6;
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);

HCSR04 hc(ultra_tri, ultra_eco); //initialisation class HCSR04 (trig pin , echo pin)

void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
 dht.begin();
}

void loop() {
  // put your main code here, to run repeatedly:
 Serial.println(hc.dist()); // return curent distance in serial
    delay(60);
// temperature read
  float t = dht.readTemperature();
  Serial.println(t);
}
0 Kudos
Message 1 of 12
(1,711 Views)

Take a look at my answer to this question here.

 

Also watch this video: VIWeek 2020/Proper way to communicate over serial

========================
=== Engineer Ambiguously ===
========================
0 Kudos
Message 2 of 12
(1,681 Views)

Hi CM_N,

 


@CM_N wrote:
Need a help to separate the data coming from the VISA read vi so that I can display temp and ultrasonic values separately.

void loop() {
  // put your main code here, to run repeatedly:
 Serial.println(hc.dist()); // return curent distance in serial
    delay(60);
// temperature read
  float t = dht.readTemperature();
  Serial.println(t);

I see two options:

  1. Send the data using only one PrintLN command like PrintLN(hc.dist + "," + t). Then you can parse one string in LabVIEW to get two values.
  2. Send each value separately, but indicate which value you send. Like PrintLN("dist=" + hc.dist) and PrintLN("temp=" + t). Then you know from the received string in LabVIEW which value you are processing…

Why is there a wait between both acquisitions?

 

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 3 of 12
(1,655 Views)

Hi GredW,

 

Thank you for your reply.

 

I tried the first option and I am using 'scan from string' function to separate data but the data read is not coming in the fix order it is jumping in between output one to three. Is there any way I read data in fix order? I can't use indicator like ("ultra" + hc.dist()). I am reading data in (hc.dist,tempvalue) this format which I want to process to indicate different output. 

Delay mention in the code is for sensor to process the data. 

 

Again thanks a lot for your help and support.

0 Kudos
Message 4 of 12
(1,620 Views)

@CM_N wrote:

Hi GredW,

 

Thank you for your reply.

 

I tried the first option and I am using 'scan from string' function to separate data but the data read is not coming in the fix order it is jumping in between output one to three. Is there any way I read data in fix order? I can't use indicator like ("ultra" + hc.dist()). I am reading data in (hc.dist,tempvalue) this format which I want to process to indicate different output. 

Delay mention in the code is for sensor to process the data. 

 

Again thanks a lot for your help and support.


Post all of your code, LabVIEW and Arduino.  My guess is you are not properly using the termination character.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
0 Kudos
Message 5 of 12
(1,614 Views)

Hi,

 

I appreciate for help me out in this. I have attached the files below please have a look. Also, as you can see in the code it is separating the only then serial is read in a particular sequency but it randomised when the program is stopped and start again meaning sometime I read temperature value first or " , " string and if you keep restarting the program then sometime it read data as intended (ultra , temp).   

 

The Arduino code is mention below.

 

#include <DHT.h>
#include <DHT_U.h>

#include <HCSR04.h>

int ultra_tri = 3;
int ultra_eco = 4;
int DHTPIN = 6;
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);

HCSR04 hc(ultra_tri, ultra_eco); //initialisation class HCSR04 (trig pin , echo pin)

void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
 dht.begin();

}

void loop() {
  // put your main code here, to run repeatedly:
 //Serial.print("Ultrasonic ");
 Serial.print(hc.dist()); // return curent distance in serial
   Serial.print(" , ");
    delay(60);
  // Read temperature as Celsius (the default)
  float t = dht.readTemperature();
  Serial.println(t);
}
0 Kudos
Message 6 of 12
(1,601 Views)

On the Arduino side:

Why is the delay between the measurements?  I would expect a delay to be after both measurements.

 

On the LabVIEW side:

1. Remove the wait.  VISA Read will limit the loop rate by waiting for the data to come in.

2. Replace VISA Open with VISA Configure Serial Port.  This way we make sure the settings are correct.  It looks like the defaults will work for your situation, so you only need to wire up the VISA resource.

3. Your string format for the Scan From String should be "%f , %f" (without the quotes).  This will tell the function to look for a floating point number, followed by a space, a comma, and another space, then then another floating point number.

4. What you are telling the VISA Read to read may not be enough bytes to read.  I would increase that to something more like 100 just to make sure the termination character is read.

5. You really should stop your loop if there is an error.

 

With all that said, here is how I would clean up your VI.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
Download All
0 Kudos
Message 7 of 12
(1,572 Views)

Hi Crossrulz, 

 

Thanks for you help. I tried the code but scan from string is giving me the error mentioned below I guess this may be because  I have a " , " in my string. 

Possible reason(s):

LabVIEW: (Hex 0x1) An input parameter is invalid. For example if the input is a path, the path might contain a character not allowed by the OS such as ? or @. 

 

One more guidance I would appreciate is that I if I want to use switch case in such a way the if Boolean is true it will read temperature and ultrasonic data and if false it will read humidity data. However, when I am trying to  that in lab view the "scan from string" in function from both cases are reading data for output 1. I have mentioned the code below and attached the vi file too. 

 

Thanks again for your help.

 

 

#include <DHT.h>
#include <DHT_U.h>

#include <HCSR04.h>

int switchdata = 1;
//int control = 0;

int ultra_tri = 3;
int ultra_eco = 4;
int DHTPIN = 6;
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);

HCSR04 hc(ultra_tri, ultra_eco); //initialisation class HCSR04 (trig pin , echo pin)

void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
 dht.begin();

}

void loop() {
 
 if (switchdata == 1){
 Serial.print(hc.dist()); // return curent distance in serial
   Serial.print(" , ");
  // Read temperature as Celsius (the default)
  float t = dht.readTemperature();
  Serial.println(t);
 }else if (switchdata == 2){
 (Serial.println(dht.readHumidity()));
 }
  if (Serial.available()>0){
    int control = Serial.read();
    if ( control == '0'){
      switchdata = 2;
    }else if (control == '1'){
      switchdata = 1;
   }
  }

 

0 Kudos
Message 8 of 12
(1,557 Views)
Solution
Accepted by topic author CM_N

Hello Everyone, 

 

Since I have figured out way to separate the data coming out of a serial port. I thought it would be good idea to post the solution so that if someone is struggling can review it and work a way around it. It might not be the effective way and I am sure, there are many more different ways to deal with it but this method works too. 

 

So, In the labview code I am toggling Boolean manually as there is push button on the device which toggle the Serial print reading and sensors and didn't want to write any command to the Arduino and just want to read serial coming from it and lab view. Hence, I have but a Boolean such that if the push button is pressed on device, user have to manually turn Boolean true or false accordingly in labview to get appropriate reading. 

 

Hope this helps.

0 Kudos
Message 9 of 12
(1,518 Views)

Hi CM,

 


@CM_N wrote:

So, In the labview code I am toggling Boolean manually as there is push button on the device which toggle the Serial print reading and sensors and didn't want to write any command to the Arduino and just want to read serial coming from it and lab view. Hence, I have but a Boolean such that if the push button is pressed on device, user have to manually turn Boolean true or false accordingly in labview to get appropriate reading. 


So the solution is to have your user to press two buttons instead of just one - and curse you if they forgot to press the 2nd button on your UI…

 

Wouldn't it be much easier (better, more safe, user-friendly, …) to implement a slightly different message format on your Arduino sketch and parse that message with your LabVIEW tool automatically???

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 10 of 12
(1,512 Views)