LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Controlling a relay using time

Hi,

 

I did aVI to control an electrical relay (attached) and now I want to run it every x seconds. I have tried timed loops, while loops, and I keep getting the same problem: the program works fine one time and then the relay stays open but the loop continues running.

 

Thank you

0 Kudos
Message 1 of 9
(1,540 Views)

Hi Wavicle,

 

I tried running your code and it seems like the loop would run for whatever time (s) you set. Once the time has elapsed, then it would stop. What do you mean by the loop continues running?

Also, try to give more details.  Is it a continuous cycle? or do you want the program to stop when the relay opens? How long do you expect the relay to be ON and how long do you want it OFF? If you can give a little more detail, that would be great. 

0 Kudos
Message 2 of 9
(1,534 Views)

Never (well, hardly ever) use a Frame Sequence!  [Almost the only exception would be for FPGA coding].  What is it that you want to repeat?  Whatever it is, do the following:

  • Isolate the code that you want to repeat.  If it is a Relay, you probably want to Close It (one command).  But if you want to repeatedly close it, you will also need to open it.
  • Surround the code that is to be repeated with a While Loop.
  • Put a Wait (ms) (or a Wait until next multiple) function with the desired Loop Time inside the While Loop.
  • Don't forget to bring error In and Error Out in and out of the While Loop, and use the Error line for the code inside the While Loop.

Relays, of course, provide slightly more tricky timing, as there are two times to consider, both mentioned above.  One is the repetition time of the loop ("Do this every 5 seconds"), and the other is the fraction of the time (Duty cycle?) that the Relay is Closed.  In the early days of LabVIEW, there was no "easy" way to have a Delay occur at a particular "point in time" relative to other code that was serialized by sitting on the Error line other than using a Frame Sequence around the Delay and running the Error Line through it.  Now there are options, including the Stall VIM and Time Express VIs.

 

Bob Schor

Message 3 of 9
(1,522 Views)

@Wavicle wrote:

I did aVI to control an electrical relay (attached) and now I want to run it every x seconds. I have tried timed loops, while loops, and I keep getting the same problem: the program works fine one time and then the relay stays open but the loop continues running.

 


When attaching code, please provide sufficient information. For example, what are the VISA command strings for "open relay" and "close relay"?

 

As a fist step, get rid of the instrument communication and work out the code logic just controlling an LED of the front panel.

 

Explain exactly what should happen and when:

 

  1. What is the relay state at the start of the program?
  2. What should the relay state be after the time target?
  3. What is the typical time target? (Your default value is 1s, is that reasonable?)
  4. If you want it to repeat, should it switch the relay after the elapsed time to the "other" state? Turn it on and off quickly until the next elapsed time?
  5. What should happen after that? (start over from the beginning? Start a shutdown sequence the stop the program? Something else?)

 

Also: Please NEVER maximize the front panel and diagram to the screen. You need to be able to program while looking at other windows (e.g. the help window!!!!). We don't want to stare ant 90% whitespace!). Also never use the "continuous run" button.

0 Kudos
Message 4 of 9
(1,507 Views)

@altenbach wrote:
As a fist step, get rid of the instrument communication and work out the code logic just controlling an LED of the front panel.

 


Here's how that could look like.

0 Kudos
Message 5 of 9
(1,496 Views)

Hi, 

 

What I want to do is to open the relay for x milliseconds then close it, wait x seconds and repeat the opening and closing process again, then wait, and so on. The relay state would be closed at the start and after the time target should be closed. the typical time target for the opening and closing sequence is milliseconds then the typical time target to wait until repeating are seconds.

 

The VISA command strings for "open relay" and "close relay" are A001 01A2 and A001 00A1.

 

When I tried to put everything inside a while loop the problem I had was that it opened and closed the relay, then waited the seconds I wanted, and then opened the relay and left it open while the loop was advancing.

 

Thank you

 

0 Kudos
Message 6 of 9
(1,458 Views)

@Wavicle wrote:

What I want to do is to open the relay for x milliseconds then close it, wait x seconds and repeat the opening and closing process again, then wait, and so on. The relay state would be closed at the start and after the time target should be closed. the typical time target for the opening and closing sequence is milliseconds then the typical time target to wait until repeating are seconds.

 


 

Well, maybe this can give you some ideas....

 

altenbach_0-1682004708532.png

 

 

Should be easy to add the instrument communications.

0 Kudos
Message 7 of 9
(1,435 Views)

Is this a mechanical or solid state relay and the make/model number? If mechanical- can it even open/close on the order of milliseconds?

 

-AK2DM

~~~~~~~~~~~~~~~~~~~~~~~~~~
"It’s the questions that drive us.”
~~~~~~~~~~~~~~~~~~~~~~~~~~
0 Kudos
Message 8 of 9
(1,432 Views)

@AnalogKid2DigitalMan wrote:

Is this a mechanical or solid state relay and the make/model number? If mechanical- can it even open/close on the order of milliseconds?


Good point. Well, it could be 900ms, who knows. 😄

 

Then there's all the communication overhead....

(Even in my code, you won't be able to see the LED flash if the on-time is significantly below 50ms, even if the LED is set to "synchronous display". We could use a chart instead.). 

0 Kudos
Message 9 of 9
(1,427 Views)