Sending a PWM from LabVIEW to Arduino

Hello Everybody!My first topic here…I hope you can understand my problem eheh :stuck_out_tongue:

I’m working on a project for college trying to use the Arduino Severino instead the NI-DAQ for communication with LabVIEW. In summary, I need to read a cooling fan speed, a LM-35 (temperature sensor) voltage and send 2 PWM in order to control temperature inside an acrylic box. I’ve already gotten the board acquiring the cooling fan RPM and the temperature from LM-35.

Here is the problem: when I implement 1 PWM (one because I’m still testing) it doesn’t work well, as you can see here:

I’ve read some threads in this forum talking about making a “Dimmer” using LabVIEW and Arduino. I believe this is what I need, but apparently they couldn’t make it too.

Here is the sketch I’m using as a test using two potentiometers as sensors.

#include <stdlib.h>

const int potPin = 0; // 1st pot
const int potP = 1; //2nd pot
const int ledPin = 10; // This led should change the brightness depending on the PWM value

int val;
int valb;
int     n;

char    buffer[4];//To get the incoming PWM value

void setup()
{
Serial.begin(9600);
pinMode(potPin, INPUT);
pinMode(potP, INPUT);
}

//Since the Serial.read()  can read only one string sent from labVIEW I use it to get three digit numbers

void loop()
{
  if ( Serial.available() >=3 )
{
    buffer[0] = Serial.read();
    buffer[1] = Serial.read();
    buffer[2] = Serial.read();
    buffer[3] = '\0';

    n = atoi(buffer); // array to int
    analogWrite (ledPin,n); //send the PWM value
}    

val = analogRead(potPin); // Read the 1st pot
valb = analogRead(potP); // Read the 2nd pot
Serial.print("F"); //header string used to identify 1st pot 
Serial.println (val);
delay(7); //if the delay is less than this LabVIEW go crazy and can't read the potentiometers
Serial.print("A");//header string used to identify 2nd pot
Serial.println (valb);
delay(7);
}

I’ve already tried to change (increase/decrease) the baudrate, but it didn’t work.

I’m quite sure this is possible to make, even reading 2 sensors in the same micro controller simultaneously

Any help is welcome!! I’d be so grateful ;D

Thanks in advance!!

If, instead of sending data from LabView, you send data from the serial monitor, is the result the same? Does the LED still seem to change brightness?

That's an interesting thing I noted but forgot to write here!! I sent the string via serial monitor and the LED didn't change its brightness. It was perfect, but I need to use LabVIEW :(

Here is my VI!! It might be helpful

It's based on Michael Nash's VI ... http://web.me.com/iklln6/automation/LabVIEW_2.html

but I need to use LabVIEW

What does labview tech support say? For the $$$$ you paid for labview, they should provide some technical evaluation. You might use a utility like portmon (free on the net) to see if there are differences between what is sent from labview compared to the serial monitor.

For the $$$$ you paid for labview, they should provide some technical evaluation

I didn’t pay anything, man. I’m using the evaluation software I downloaded from NI website. I use LabVIEW at college due to its Control Systems library. In this project we intend to make a feedforward control. That is the reason I NEED to use it. I’ve been actually trying to use Arduino in this project because I don’t wanna be stuck by using NI-DAQ. Anyways, Thanks a lot for your tip, it was really helpful.

Using portmon I could see that LabVIEW sends (and I don’t know why it happens) a “dot” after sending the PWM string. Due to the buffer didn’t have the same length of the value received from labview it was filled incorrectly. I just had to increase the length of the buffer array to get it working fine. Then, I have now

#include <stdlib.h>

const int ledPin = 10; // This led should change the brightness depending on the PWM value

int     n;
char    buffer[5];//To get the incoming PWM value

void setup()
{
Serial.begin(9600);
}

//Since the Serial.read()  can read only one string sent from labVIEW I use it to get three digit numbers and a "dot". For example "156."

void loop()
{
  if ( Serial.available() >=4 )
{
    buffer[0] = Serial.read();
    buffer[1] = Serial.read();
    buffer[2] = Serial.read();
    buffer[3] = Serial.read();
    buffer[4] = '\0';

    n = atoi(buffer); // array to int
    analogWrite (ledPin,n); //send the PWM value
}    
}

In the code above I took off all the part involving the potentiometers.

That’s it!! Thanks a lot for your help guys!! :smiley: