1

[![I uploaded piscope image and same time output image also for better understanding of issues][1]][1]I am using sample code of Pypi from "http://abyz.co.uk/rpi/pigpio/python.html" to detect frequency from function generator but frequency not coming constant .For verifying my input frequency i checked it on CRO where i got frequency is constant i.e 4.985khz but on raspberry its showing 4.805khz and 5.000khz.

here is my code

import time
import pigpio # http://abyz.co.uk/rpi/pigpio/python.html

class reader:
   
   def __init__(self, pi, gpio, weighting=0.0):
      
      self.pi = pi
      self.gpio = gpio

      if weighting < 0.0:
         weighting = 0.0
      elif weighting > 0.99:
         weighting = 0.99

      self._new = 1.0 - weighting # Weighting for new reading.
      self._old = weighting       # Weighting for old reading.

      self._high_tick = None
      self._period = None
      self._high = None

      pi.set_mode(gpio, pigpio.INPUT)

      self._cb = pi.callback(gpio, pigpio.EITHER_EDGE, self._cbf)

   def _cbf(self, gpio, level, tick):

      if level == 1:

         if self._high_tick is not None:
            t = pigpio.tickDiff(self._high_tick, tick)

            if self._period is not None:
               self._period = (self._old * self._period) + (self._new * t)
            else:
               self._period = t

         self._high_tick = tick

      elif level == 0:

         if self._high_tick is not None:
            t = pigpio.tickDiff(self._high_tick, tick)

            if self._high is not None:
               self._high = (self._old * self._high) + (self._new * t)
            else:
               self._high = t

   def frequency(self):
      """
      Returns the PWM frequency.
      """
      if self._period is not None:
         return 1000000.0 / self._period
      else:
         return 0.0

   
   def cancel(self):
      
      self._cb.cancel()

if __name__ == "__main__":

   import time
   import pigpio
   import read_PWM

   PWM_GPIO = 4
   RUN_TIME = 60.0
   SAMPLE_TIME = 2.0

   pi = pigpio.pi()

   p = read_PWM.reader(pi, PWM_GPIO)

   start = time.time()

   while (time.time() - start) < RUN_TIME:

      time.sleep(SAMPLE_TIME)

      f = p.frequency()
      
     
      print("f={:.1f}".format(f)

   p.cancel() 


  [1]: https://i.stack.imgur.com/2a1UP.jpg

1 Answer 1

1

As the site says these are code examples. They are there as an aid to learning to use pigpio.

The following examples show various ways pigpio may be used to communicate with sensors via the GPIO.

Although many are complete programs they are intended to be a starting point in producing your own code, not an end point.

If you are confident the signal is as stable as you think perhaps correct any faults you find in the code?

7
  • ,as i am quite new to this ,i am asking weather i need to average output value or anything else .So that i can get a constant value?
    – ram
    Sep 28, 2020 at 7:38
  • Look at the signal with piscope. Is it as stable as you expect?
    – joan
    Sep 28, 2020 at 7:51
  • ,in piscope frequency is vary like 4.960 -4.980..But frequency return in program is 4.89 ,4.89,4.89 ,5.00,5.00....I tested with low frequency i.e 945.2 hz and its varying on scope is 944.8 to 945.8 but frequency return in program is 943hz and 947
    – ram
    Sep 28, 2020 at 8:30
  • If the piscope data seems steady then you should be able to smooth the data in the example code. They are both working from the same data source.
    – joan
    Sep 28, 2020 at 10:19
  • ,i am uploaded both piscope and ouput value image of my problem. please guide me
    – ram
    Sep 28, 2020 at 11:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.