Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 6 of 6

Thread: Pulses counting in intervals with teensy 3.2

  1. #1
    Junior Member
    Join Date
    May 2020
    Posts
    3

    Pulses counting in intervals with teensy 3.2

    Hello everyone!
    I’m trying to use the teensy 3.2 with PaulStoffregen’s library (https://github.com/PaulStoffregen/FreqCount) to count the number of partial discharges.

    Code:
    #include <FreqCount.h>
    #include <LiquidCrystal.h>
    const int rs = 12, en = 11, d4 = 9, d5 = 8, d6 = 7, d7 = 6;
    LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
    
    unsigned long count = 0;
    unsigned long prior_count=0;
    
    void setup() {
      FreqCount.begin(100);
      display.setBrightness(0x0F);
      lcd.begin(16, 2);
      lcd.print("PULSES:");
    }
    
    void loop() {
      if (FreqCount.available()) {
        count = count + FreqCount.read();
        if (count != prior_count) {
          Serial.println(count);
          
          lcd.setCursor(0, 1);
          lcd.print(count);
          
          prior_count = count;
        }
      }
    }
    Example signal (frequency not higher than 300 kHz):

    Click image for larger version. 

Name:	example.png 
Views:	10 
Size:	7.4 KB 
ID:	20114

    The problem is, that the amplitude of the signal is very low (about 100-200 mV), so the Teensy doesn’t count it.

    I want the teensy to count the signals in some kind of ‘intervals/periods’ (not longer than 0,02 s) in which counted will be only peaks with an amplitude more than the trigger (set on 100 mV for example). And if in this period the teensy find 1 or more peaks higher than a trigger level, it should mark is as a ‘positive’ and add one to the counted pulses.

    It is possible to do it with this library?

  2. #2
    Senior Member+ Theremingenieur's Avatar
    Join Date
    Feb 2014
    Location
    Colmar, France
    Posts
    2,583
    That’s not the purpose of the freqcount library which expects clean logic levels. To count your signal, you’d have to add some external circuitry, for example around a LM331 comparator where you could add a multi turn potentiometer to set a trigger level.

  3. #3
    Senior Member
    Join Date
    Jan 2020
    Location
    Toscana
    Posts
    109
    With this signal you get a large spectrum of pulse widths. The question is, what kind of information do you expect and want to get out of this.

    I'd program that "by hand":
    digitize the signal into a buffer
    scan through the data for edges
    check if the edge is tall enough to count as useful
    use this data (counting, timing, etc)

    This way you get no artefacts from filtering, etc.
    The built-in ADC has about 630k/sec sample rate, so that should barely work.
    Last edited by flashburn; 05-15-2020 at 09:07 PM.

  4. #4
    Junior Member
    Join Date
    May 2020
    Posts
    3
    Quote Originally Posted by Theremingenieur View Post
    That’s not the purpose of the freqcount library which expects clean logic levels. To count your signal, you’d have to add some external circuitry, for example around a LM331 comparator where you could add a multi turn potentiometer to set a trigger level.
    Ok, I thought that this library counts the rising slopes, I'll get rid of this. So you thing that this comparator will be better option, than simply using some kind of analogRead instruction? The next question, I should use it in a voltage-to-frequency circuit? Will it make square signal based on how high it went above the trigger?

    Quote Originally Posted by flashburn View Post
    With this signal you get a large spectrum of pulse widths. The question is, what kind of information do you expect and want to get out of this.
    The thing is that I only have to recognize changes in the signal, I don't care about the width or duration. As you can see on this example there is a noise (10809 - 11400 sample) and then the discharge begins. I'd like the teensy only to recognize, that the signal went higher than for example 200 mV in this interval, so the counter add one to the number of counts.

    Quote Originally Posted by flashburn View Post
    I'd program that "by hand":
    digitize the signal into a buffer
    scan through the data for edges
    check if the edge is tall enough to count as useful
    use this data (counting, timing, etc)
    About which kind of a buffer have you thought? Register? Wouldn't it take too much time? Do you have any example codes with that sort of programs?
    As you wrote it, it looks pretty easy but I have no idea how to start it.

  5. #5
    Junior Member
    Join Date
    May 2020
    Posts
    3
    Maybe, I'll try to summarize the current work status.

    I wrote a PM to Theremingenieur, because when I was doing research on LM331 (F/V converter) which he advised me to use, I realized that he made a mistake. That was right, he thought about the LM311 (comparator).
    Finally, I used it to compare this two signals, but now there is a problem, because comparator is adding some noise when it's changing the state of the output. The counter (teensy with FreqCount library) counts it as hundreds of pulses.

    Click image for larger version. 

Name:	IMG_1402.jpg 
Views:	7 
Size:	127.2 KB 
ID:	20554

    I tried to use some pull-down capacitors on the output (100 pF+100 uF parallel), but they completely slowed down the device, it became completely unresponsive.

    In PM Theremingenieur recommended me also to add some hysteresis to the circuit, which as I understand as adding a resistor between output and non-inverting input, like in this picture? Is there any typical values of resistor like this?

    Click image for larger version. 

Name:	1.JPG 
Views:	6 
Size:	29.5 KB 
ID:	20555
    (https://www.analog.com/en/analog-dia...ysteresis.html)

  6. #6
    Senior Member+ Theremingenieur's Avatar
    Join Date
    Feb 2014
    Location
    Colmar, France
    Posts
    2,583
    All you need to calculate the values for the resistors R1 and R2 are given in the schematic from analog.com. To use these formulae, you have to analyze the input signal first and determine two voltage levels. A lower one which is low enough to identify for sure a true low input level, and a higher one which the input signal must reach to drive the output high. Then, only input peaks which are above VIH will drive the output high and it will remain high until the input signal goes below VIL. As long as the signal will be between VIL and VIH, the comparator will not change its output state. After you found these two values, you set first Vref to (VIH + VIL) / 2. Afterwards you use the given formulae and transform them to solve for R1 and R2, since you have already Vref, VIH, and VIL.
    There aren’t “typical values”, these depend from the input signal and the required “cleaning” of the latter and are thus different for each individual application.

Posting Permissions

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