Forum Rule: Always post complete source code & details to reproduce any issue!

Thread: Pulses counting in intervals with teensy 3.2

1. 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()) {
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):

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. 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. 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.

4. Originally Posted by Theremingenieur
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?

Originally Posted by Deleted User
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.

Originally Posted by Deleted User
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. 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.

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?

(https://www.analog.com/en/analog-dia...ysteresis.html)

6. 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.

7. The solution of my problems was adding a peak detector circuit before comparator and everything started working very well. Then, the hysteresis became unnecessary.

Now, I want to improve my device by adding a SD card to register the number of pulses. I also want to add RTC, to write out the time and corresponding pulses (kind of register). Is it possible to connect FreqCount library and SD library in one program? I'm afraid, that external interrupts caused by pulses can lead to problems with writing on SD card, what do you think?

8. Hello! Does anybody find any solution to my problem to connect FreqCount with SD card?

9. Originally Posted by brogue
Is it possible to connect FreqCount library and SD library in one program?"
yes.
Originally Posted by brogue
I'm afraid, that external interrupts caused by pulses can lead to problems with writing on SD card, what do you think?
Should work. Its has a clock line.
Originally Posted by brogue
Hello! Does anybody find any solution to my problem to connect FreqCount with SD card?

Have you actually tried to solve the problem yourself?
What exactly is not working?
Where is the sourcecode?

10. Originally Posted by brogue
Hello! Does anybody find any solution to my problem to connect FreqCount with SD card?
Also note that current TeensyDuino 1.54 Beta is changing the SD code to a more efficient alternate version.

Would suggest working with that version to see functionality.

Current Beta is #5 - but refresh to #6 may be coming ... though not sure if that has any changes to the new SD code that is using the SdFat library.

11. Originally Posted by Frank B
yes.
Doubt. In Hardware Requirements (https://www.pjrc.com/teensy/td_libs_FreqCount.html) there is an information, that the library requires special pin, Teensy 3.2 - Pin 13.

Originally Posted by Frank B
Should work. Its has a clock line.
Have you actually tried to solve the problem yourself?
What exactly is not working?
On the other hand, I have to use the <SD.h> including the <SPI.h> library (some kind of bus, as i read), which is using the following pinout:
CS to pin 14,
SCK to pin 13,
MOSI to the pin 11,
MISO to pin 12.
So the thing is, that I have a some kind of the collision between this libraries.

Originally Posted by Frank B
Where is the sourcecode?
The sourcecode doesn't exist. Firstly, I have to manage with the hardware problems.

12. Look at the card that came with your Teensy 3.2. It shows where the pins are located. You will also notice some pins are listed in grey text, they are alternate pin assignments. It appears that all the SPI pins have at least one alternate, maybe more. There are commands to make the alternate pins active. I don't have them in front of me but you could do a web search for something like 'teensy alternate spi pins' and examine the information at the links.

These are examples of source code in the arduino environment for the bits and pieces to do what you want. You have to mix them together into one program to get your desired results. We can't do that for you, but we can help guide you to figure out what went wrong or provide clues.

Typical programming for me is the Arduino environment on one screen and an internet browser in the other to find reference material.

13. Thanks for you reply. I found the information about alternative pins for the SPI library. As you can see at https://www.pjrc.com/store/teensy32_card7a_rev3.png. The alternative pin for SCK is the pin 14. Firstly, I decided to go for some basic example code, directly from arduino forum.
Code:
```#include <SPI.h>
#include <SD.h>
File myFile;
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);

SPI.setSCK(14); // SCK on pin 14
SPI.begin();

while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.print("Initializing SD card...");
if (!SD.begin(10)) {
Serial.println("initialization failed!");
while (1);
}
Serial.println("initialization done.");
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
myFile = SD.open("test.txt", FILE_WRITE);
// if the file opened okay, write to it:
if (myFile) {
Serial.print("Writing to test.txt...");
myFile.println("This is a test file :)");
myFile.println("testing 1, 2, 3.");
for (int i = 0; i < 20; i++) {
myFile.println(i);
}
// close the file:
myFile.close();
Serial.println("done.");
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
}
void loop() {
// nothing happens after setup
}```
As you can see, I decided to define the alternative SCK pin as 14 and ...it simply doesn't work. I tried it many times without any solution. And, on the other hand, if I delete line "SPI.setSCK(14)" and move the SCK cable to pin 13, everything works perfect. I think, I might have a problem with my Teensy...? What do you think? I have run out of ideas...

14. I don't know but just a thought, does SPI.setSCK need to go after SPI.begin?

15. @brogue I'm initializing my SPI the same sequence as you are. I don't know how fast the SD card you are using is. Have you tried to slow down the SPI bus for the SD Card by using either

p3=(!SD.begin(sdCs), SPI_HALF_SPEED);

OR

p3 = (!card.init(SPI_HALF_SPEED, sdCs));

(So many different versions of changing code so try one then the other.)

Posting Permissions

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