Having trouble reading pulses with my pi

Thread Starter

zirconx

Joined Mar 10, 2010
171
Hi, I'm trying to get my pi to read pulses to a GPIO pin. This is in preparation for reading from a hall effect flow sensor in my water line. Right now I'm just trying to get software + hardware reading pulses. I have a signal generator set to output square waves. The thing is no matter what my output frequency (generally I'm attempting 5-20hz) I get strange readings (example output below). I also tried just connecting a wire from ground or from 3.3v to the pin (and various assortments of pull down/pull up settings in my code) and I get odd output from that also. If I just touch the wires together, I might get a count of 120 pulses in a second, for example. Adding the debounce code (glitchFilter) did not help. Any ideas what might be happening?

Here is my code

And the output while the signal generator is connected. I can adjust the output frequency and the counts don't really change. But if I disconnect the signal generator, then my counts go to zero. I have an oscilloscope connected in the circuit too, and everything looks good there.

Javascript:
const Gpio = require('pigpio').Gpio;

var counter = 0

//const led = new Gpio(17, {mode: Gpio.OUTPUT});
const button = new Gpio(6, {
  mode: Gpio.INPUT,
  pullUpDown: Gpio.PUD_UP,
  edge: Gpio.RISING_EDGE
});

// debounce - shouldn't be necessary? It didn't help
//button.glitchFilter(10000);

button.on('interrupt', (level) => {
  //led.digitalWrite(level);
  counter++
});

setInterval(function() {
    //read the count of pulses
    console.log(counter);
    counter = 0

}, 1000);
Example output when the signal generator is connected:
356
440
366
459
401
387
365
396
414
399
376
411
365
391
400
458
422
456
416
441
431
426
369
397
423
424
416
411
407
400
388
393
438
418
454
371
425
383
 

KeithWalker

Joined Jul 10, 2017
3,063
If the PI requires a 3V logic signal, the square wave should be switching between 0V and 3V.
How is the signal generator output coupled to the GPIO pin?
If it is AC coupled, the 3V p-p signal will be switching between plus 1.5V and minus 1.5V which will probably give very marginal results.
 

Thread Starter

zirconx

Joined Mar 10, 2010
171
On my scope the square wave changes between about 700mv and 3.1v.

The signal generator output is connected directly to the GPIO pin. I also have a 500R resistor from the signal generator output to ground to reduce the amplitude down to 3.1v. I have connected the grounds between the two devices. The signal generator is a simple XR2206 kit.

But I get the same odd results when I touch the wires together. I would expect some bounce, but not 120 pulses worth of bounce.

I can post screenshots from the scope if that would be helpful. But shouldn't I get better results from touching the wires together?
 

KeithWalker

Joined Jul 10, 2017
3,063
The data sheet for the Raspberry PI is very vague about input logic levels.
The input gate detects whether the input voltage level is low or high by comparing it to a threshold voltage. Normally the voltage threshold is about 1.8V, but it isn't guaranteed; it can be anywhere between the maximum input low and minimum input high, that is, between about 0.8 and 2.0V.
That means that your "low" is probably on the borderline which is why you are getting odd results.
To provide noise immunity and prevent chatter on transitions, you can configure the input gate to act as a Schmitt trigger, with input hysteresis. With hysteresis there are different threshold voltages for rising and falling transitions.
On an open circuit input pin the noise pickup characteristics are not defined and are very unpredictable so always use a pull up or pull down resistor or an active push-pull drive.
 

Thread Starter

zirconx

Joined Mar 10, 2010
171
Ok, forget the pulse generator for a moment. I connected a 5k resistor from the input pin to 3.3v. I put my scope on the input pin. I see a clean 3.3v. I manually ground the pin. My scope shows 0v. Now I unhook the ground. The scope shows a clean jump back to 3.3v. You can see in my code I'm watching for RISING_EDGE, so this should register a count. But I get a count of 15 printed to the screen. Any idea what might cause that?
Thanks.
 

KeithWalker

Joined Jul 10, 2017
3,063
Ok, forget the pulse generator for a moment. I connected a 5k resistor from the input pin to 3.3v. I put my scope on the input pin. I see a clean 3.3v. I manually ground the pin. My scope shows 0v. Now I unhook the ground. The scope shows a clean jump back to 3.3v. You can see in my code I'm watching for RISING_EDGE, so this should register a count. But I get a count of 15 printed to the screen. Any idea what might cause that?
Thanks.
What are you using to unhook the ground from the input? Can you guarantee that it is a single clean break?
If you are using a switch or simply manually disconnecting a wire, the wiping action will cause a number of very fast breaks and makes before it is completely open. The PI is probably fast enough to catch them all. That is why they built in the Schmitt trigger mode.
You may even be able to see them if you use the scope in positive trigger mode and increase the timebase speed.
 

Thread Starter

zirconx

Joined Mar 10, 2010
171
What are you using to unhook the ground from the input? Can you guarantee that it is a single clean break?
If you are using a switch or simply manually disconnecting a wire, the wiping action will cause a number of very fast breaks and makes before it is completely open. The PI is probably fast enough to catch them all. That is why they built in the Schmitt trigger mode.
You may even be able to see them if you use the scope in positive trigger mode and increase the timebase speed.
Yes you could be right about that. I am doing my best to do a clean break, and I find it hard to believe that could cause 15 pulses, but perhaps it is.

I decided to try a different gpio library, this on/off one: https://github.com/fivdi/onoff

My manual break causes 3-5 pulses, that is closer to what I might expect.

Using the function generator, the pulses-per-second output from this code below matches the frequency displayed on my oscilloscope almost exactly. :) Up until about 3.8khz, after that the readings are no longer accurate. I doubt my sensor will send that many pulses, so I think this will work.

JavaScript:
const Gpio = require('onoff').Gpio;
const button = new Gpio(6, 'in', 'rising');

var counter = 0

button.watch((err, value) => {
  if (err) {
    throw err;
  }

  counter++
});

process.on('SIGINT', _ => {
  button.unexport();
});

setInterval(function() {
    //read the count of pulses
    console.log(counter);
    counter = 0
}, 1000);
 
Top