From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

Real-Time Measurement and Control

cancel
Showing results for 
Search instead for 
Did you mean: 

PWM control by arduino : incomprehensible PWM variation when interruption

Hi everyone !

I work on a project where I have to control  DC motor using LabView, an Arduino nano, both connected by bluetooth.

For now, I can control the speed of the motor.

I can get values from an light blocking sensor to calculate the speed of rotation of the system with an encoder.

I can't send the right value to LabView but it's another problem and another topic ( https://forums.ni.com/t5/LabVIEW/LV-Arduino-bluetooth-communication-String-integer-convertion/m-p/38...: )

Here is a little diagram to explain my project. Don't pay attention to the string to int convertionBlocs2.png

But I have an issue with the PWM. When an interruption starts, the PWM changes until the end of the interruption:

Here is a video to show you what happens

https://youtu.be/RcOGuSw1gDw

If you have any idea about it, please tell me. I may not be clear, feel free to ask questions.

 

Thanks for reading Smiley Happy

 

Here is the code I use, and the VI attached

 volatile byte counter;
 int rpm;
 long timeold;
 int interruptPin = 0, pinPWM = 5;
 int duration;
 String command = "128";
 
 void setup()
 {
   pinMode ( interruptPin, INPUT );
   pinMode ( pinPWM, OUTPUT );
  
   counter = 0;
   rpm = 0;
   timeold = millis();
   attachInterrupt ( interruptPin, interruptCounter, FALLING );
   Serial.begin ( 9600 );
   Serial.setTimeout( 85 ); //to avoid waiting 1000ms between each change of the pwm value on labview
   Serial.println ( "let's go" );
 }

 void loop()
 {
    if( counter >= 1 ){
      detachInterrupt ( interruptPin ); 
      /******************* TREATMENT OF THE ROTATION SPEED *******************/
      rpm = 60000 / ( millis() - timeold );
      Serial.write( rpm );  //this is supposed to be rpm but maybe for another post 😉
      /******************* GETTING READY FOR ANOTHER ROUND ******************/
      timeold = millis();
      counter = 0;
      attachInterrupt ( interruptPin, interruptCounter, FALLING );
    }
    else{
      Serial.write( "no" ); //Not important, just because labview is waiting to read something
    }
    /*******************  *** ACTUALISING THE PWM **********************/
    command = Serial.readString(); 
    analogWrite ( 5, command.toInt() );
 }

 void interruptCounter()
 {
   ++counter;
   delay(1);
 }

0 Kudos
Message 1 of 3
(2,245 Views)

Hello nitneuk,

 

After looking at your code in LabVIEW I don't see any problem with it. I think your problem is more an Arduino-sensor one. The most appropriate action may be to ask this question in an Arduino-focused forum? (https://forum.arduino.cc/ for example)

 

If someone in this forum is competent in this subject (Arduino), feel free to reply!

 

Regards

Corentin

Application Engineer - National Instruments - France
0 Kudos
Message 2 of 3
(2,203 Views)

Hi Corentin.

 

Okay, thanks you very much Smiley Happy

0 Kudos
Message 3 of 3
(2,200 Views)