Sampling a PWM signal


Closed Thread
Results 1 to 10 of 10

Hybrid View

  1. #1

    Question Sampling a PWM signal

    Hi,

    I'm trying to measure 2 PWM signals with a frequency of about 100 Hz.
    They are output from a dual-axis accelerometer.

    I have an idea how to go about it, but I'd be interested to here from you guys (and gals).

    Has anybody done this before?

    How did you do it?

    Do you have any sample code?

    Anyway, as always, all feedback is appreciated.

    Thanks.
    ---> picnaut

  2. #2
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    Take your PWM, thru a Diode (1N4148 for example) so as not to affect the PWM downstream, thru a Resistor (say 10K) to charge a Capacitor (eg 1uF) connected down to 0v. The Capacitor will charge proportionately to the PWM value. 100% PWM will charge the Cap to 100%. 50% PWM will charge the Cap to 50% etc. Connect a PICs ADC across the Cap to sample the charge level and so determine the PWM. Use a Resistor PD if your PWM is greater than 5v. Compensate in software for the voltage drop across the Diode. No fancy software required...just read the ADC... Assuming you had 5v PWM to start with, 0.6v drop across the Diode, then 4.4v will be 100% PWM, 2.2v will be 50% PWM etc etc proportionately. Simple.

  3. #3


    Did you find this post helpful? Yes | No

    Default

    Hi Melanie,

    Thanks, that's a cool approach.
    I actually got it working with software in 662 bytes.
    It was a good exercise.
    I'll try your method too.

    Thanks.
    ---> picnaut

  4. #4


    Did you find this post helpful? Yes | No

    Default

    Hi Melanie,

    Quote Originally Posted by Melanie
    Take your PWM, thru a Diode (1N4148 for example) so as not to affect the PWM downstream.
    If you use a diode, the capacitor has no where to discharge.
    It seems to work just fine when it's directly connected though.

    What are your thoughts on this?
    ---> picnaut

  5. #5
    Warrier's Avatar
    Warrier Guest


    Did you find this post helpful? Yes | No

    Default

    Well Ms. Newman, you need to discharge the cap to get the voltage to follow the PWM duty cycle - else the cap charges up to 4.4volts even at the lowest PRF and will stay there! The diode will make sure of that. So you need a discharge path thru another resistor (say, 10 times Rin or higher depending on the ripple one can tolerate) connected parallel to the 1uF, don't you think?

    -warrier

  6. #6
    Warrier's Avatar
    Warrier Guest


    Did you find this post helpful? Yes | No

    Default

    Hi picnaut:

    you can obviously try to device a software exor the two PWM signal if a relative measure is what you need and then use internal timer to integrate the resultant pulse. Or, you can output exored pulse and use Ms. Newman's method of external analog integration and measure the magnitude...

    you can also use a pulse of known duration to charge a cap and use the PWM to discharge it and measure the average voltage on the cap. The resultant value on the cap will of course be the inverse of the PWM duty cycle in this case.

    -warrier

  7. #7
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    The easy way, is to keep this diode there and add a resistor from the cathode to the ground to discharge the capacitor.

    Usually what i use, some higher resistor value, lower capacitor value, connect this to a OP-AMP input, OP-AMP output to the PIC. THat's it. There's no big chance to interfer to the PWM signal and the PWM circuit driver charge and discharge the Cap via the same resistor. BUT in some case, using some kind of OPTO-Isolator can also do the job.

    OR skipping the kinda 'frequency to voltage conversion' and measure the duty but... depending of you.

    This might be useful... well some parts

    EDIT: Warrier point you to some good idea too. I can't really comment them but those really make sense.
    Last edited by mister_e; - 7th May 2005 at 00:36.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  8. #8
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    Thats true. I'm usually sampling values much greater than 5v so my PD Resistors discharge the Cap... if it's 5v or less then of course you wouldn't have it.

  9. #9
    Stump's Avatar
    Stump Guest


    Did you find this post helpful? Yes | No

    Default

    If the freq is known and constant, you can use the pulse in command
    to measure the pulse width. With very little math you can repackage the result however you like. If the chip you are using has a capture module,
    it can run in the background with very high resolution. (I am use this
    for a 16 bit A/D converter)
    R

  10. #10
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,648


    Did you find this post helpful? Yes | No

    Wink

    Mhhh ...

    Let's see it closer : what's the precision of your accelerometer ??? 1% ...2% ?
    so, 8 bits are sufficient...
    @100 Hz ... a Pulsin 1, ... immediatly followed by a RCtime 0, ... will perfectly do the job. With no ref to the freq.

    just take 1 of 2 or 3 pulses to let the PIC calculate or display.

    use of the 8 bits timer in free running mode could also be seen ... as reading the timer "in flight" won't give too false a measure.

    Alain

Similar Threads

  1. PWM signal
    By Battalinoglu in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 25th January 2010, 07:07
  2. PIC16F877A pwm use for IR transmission
    By mcbeasleyjr in forum General
    Replies: 0
    Last Post: - 11th July 2009, 18:51
  3. Variable PWM PIC18F2431
    By trr1985 in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 12th June 2009, 06:03
  4. PIC16F684 + LCD to use the 256bytes of EEPROM - HELP
    By xnihilo in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 7th March 2008, 14:19
  5. Tidying Up PWM Routine
    By Tissy in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 21st February 2005, 00:26

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts