Jump to content

As of July 17, 2015, the LabJack forums here at forums.labjack.com are shut down. New registrations, topics, and replies are disabled. All forums are in a read-only state for archive purposes.

Please visit our current forums at labjack.com/forums to view and make new posts. To post on the current forums, use your labjack.com login account. Your old LabJack forums login credentials have been retired. There are no longer separate logins for labjack.com and LabJack forums.


Photo

What is the code to send a PWM signal to an input


  • Please log in to reply
12 replies to this topic

#1 6thSense

6thSense
  • Members
  • 8 posts

Posted 17 July 2013 - 10:08 AM

Hello,

 

I'm a novice when it comes to LabJack and electronics in general. What would the code look like to send a PWM signal from say one of the FIO channels (say FIO4) to an input channel like AIN2. I connected a wire between those two. Now I just need a code to send the voltage.

 

Any help would be appreciated.

 

Thanks.



#2 LabJack Support

LabJack Support
  • Admin
  • 8677 posts

Posted 17 July 2013 - 10:55 AM

What are you using for software?

 

Why do you want to send a PWM signal to an analog input?



#3 6thSense

6thSense
  • Members
  • 8 posts

Posted 17 July 2013 - 11:09 AM

Using Java...I will be needing to send a PWM signal to control a pump..but before that, I'm just trying to see if the signal is being sent/output like I want it.



#4 LabJack Support

LabJack Support
  • Admin
  • 8677 posts

Posted 17 July 2013 - 12:23 PM

First use the test panel in LJControlPanel to set up the timers to output PWM:

 

http://labjack.com/s...s-guide/2.9.1.1

http://labjack.com/s...s-guide/2.9.1.2

 

The best way to look at the PWM is with an oscilloscope, but you might be able to close out LJControlPanel and use LJStreamUD to see the PWM signal on an analog input.



#5 6thSense

6thSense
  • Members
  • 8 posts

Posted 17 July 2013 - 01:23 PM

How can I set up the timers to output PWM using code?

 

Also, how could I see how much voltage it's outputting without going to the control panel? I mean, again, using code.



#6 LabJack Support

LabJack Support
  • Admin
  • 8677 posts

Posted 17 July 2013 - 03:07 PM

We provide Java examples here:

 

http://labjack.com/s...d/examples/java

 

The U3_eFunctions and U3_TimerCounters examples will demonstrate setting up timers and counters. In the User's Guide this section provides timer/counter documentation using the UD driver and some sample pseudocode which shows how to setup a PWM8 output specifically:

 

http://labjack.com/s...ers-guide/4.3.6

 

Note that if you downloaded the Java examples previously, redownload it. I fixed the U3 timer/counter example just now.



#7 6thSense

6thSense
  • Members
  • 8 posts

Posted 17 July 2013 - 03:59 PM

I just downloaded it and tried the timer/counter code...how would I go about reading the voltage put out by the PWM signal from the FIO4 channel?



#8 LabJack Support

LabJack Support
  • Admin
  • 8677 posts

Posted 18 July 2013 - 11:41 AM

After configuring your timer/counter on FIO4 you can connect it to an analog input (AIN0/FIO0 for example) with a wire and read the voltage on it. You will want to use streaming to read the AIN at faster rates. Take a look at the U3_SimpleStream example for streaming analog inputs readings. For reading analog inputs using command/response mode take a look at the U3_Simple example.



#9 6thSense

6thSense
  • Members
  • 8 posts

Posted 19 July 2013 - 10:22 AM

Hi again,

 

So I tried what you said..

 

I basically copied the entire TimerCounter example (which is supposed to output PWM signal) and just added to the end of it some code to take a reading of AIN2, the input channel to which the PWM line is connected. Here is a notepad file with all that code: http://www.2shared.c...lemodified.html (sorry for using a third party upload site...I tried attaching it to this post but it kept giving me an error). I would really appreciate it very very much if you could take a look at it or run it and see what's wrong. It is giving the same reading of 3.2  Volts no matter what the duty is set to. I believe we really narrowed down the problem to the code...because I went to the control panel and the input channel is clearly responding to the PWM signal changes there...but still no luck with the code. So it's the code that has a problem. Please check out the attached file and let me know what I need to change. I would greatly appreciate it.

 

Thanks.

 

 



#10 LabJack Support

LabJack Support
  • Admin
  • 8677 posts

Posted 19 July 2013 - 11:39 AM

You'll want to move this chunk of code (the calls before your AIN read) to the end of the function or remove it:

// Reset all pin assignments to factory default condition.
intErrorcode = LJUD.ePut (intHandle[0], LJUD.LJ_ioPIN_CONFIGURATION_RESET, 0, 0, 0);
ErrorHandler(intErrorcode, 0, new Exception());

// The PWM output sets FIO4 to output, so we do a read here to set
// it to input.
intErrorcode = LJUD.eGet (intHandle[0], LJUD.LJ_ioGET_DIGITAL_BIT, 4, dblValue, 0);
ErrorHandler(intErrorcode, 0, new Exception());

This code is turning off the timer/counters and setting the timer line to a digital input.



#11 LabJack Support

LabJack Support
  • Admin
  • 8677 posts

Posted 19 July 2013 - 11:50 AM

Also, we are currently looking into forum problems right now.



#12 6thSense

6thSense
  • Members
  • 8 posts

Posted 19 July 2013 - 11:41 PM

Hi,

 

Thanks for the reply. It seems to be working...but the readings I'm getting from the input, though relatively in the right ballpark depending on the bits assigned, seem to be very very inconsistent. For example, the following could be a typical reading pattern as I run it multiple times: 1.8, 2.1, 1.5, 1.5, 1.3, 1.8, 2.1 etc.

 

If the spectrum was between 1 and 100, you wouldn't have to worry too much about that. But considering it's between 1 to 5, those kind of inconsistencies are a bit troubling. Why do you think this is happening? Any ideas on how to fix it? Thank you very much for being responsive again.



#13 6thSense

6thSense
  • Members
  • 8 posts

Posted 22 July 2013 - 08:50 AM

Hi, nevermind my last question. I now understand why.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users