4

I am meassuring data pulses from my electrical meter, giving me one pulse per Watt used. I have 4 electrical meters connected to my Raspberry Pi model B. I have connected them directly like this (no other components used):

3.3VDC
GPIO 0, pin 11
GPIO 1, pin 12
GPIO 2, pin 13
GPIO 3, pin 15

I have this Python code (picked from a larger code):

# Use board layout pin scheme
GPIO.setmode(GPIO.BOARD)

# Setup pins
GPIO.setup(11, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(12, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(13, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(15, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

# Create interrupt for each pulse
GPIO.add_event_detect(11,GPIO.FALLING, callback=eventHandler, bouncetime=350)
GPIO.add_event_detect(12,GPIO.FALLING, callback=eventHandler, bouncetime=350)
GPIO.add_event_detect(13,GPIO.FALLING, callback=eventHandler, bouncetime=350)
GPIO.add_event_detect(15,GPIO.FALLING, callback=eventHandler, bouncetime=350)

...

# Threaded callback handler
def eventHandler (pin):

  global gpio0_totalPulses
  global gpio1_totalPulses
  global gpio2_totalPulses
  global gpio3_totalPulses
  global logger

  # GPIO 0
  if(pin == 11):
    gpio0_totalPulses = gpio0_totalPulses + 1
    logger.info("GPIO 0: Pulse number ["+ str(gpio0_totalPulses) +"] detected")

  # GPIO 1
  if(pin == 12):
    gpio1_totalPulses = gpio1_totalPulses + 1
    logger.info("GPIO 1: Pulse ["+ str(gpio1_totalPulses) +"] detected")

  ...

I am using one of the four electrical meters as a base reference, to check if I can trust the data but unfortanly it seems I cannot as I think I am getting bouncing counts. I am having a fixed power consumption on aprox 6W which should result in aprox 6 counts per hour but from time to time I am getting multiple hits within a 1-3 seconds period!? This is happening randomly but I really don't think this is the actual power usage though I have not checked if the electrical meter is actually giving the same number of blinks (too long to test).

My suspicion is that these extra-counts are ghosts/bouncing values and though I am using the built-in pull-down resistor I don't think this is enough? My setup is very "improvised" with a lot 1-wire connections and stuff you wouldn't see in a real product (hey, this is what RasPi is all about), so maybe my wires are acting as an antenna or the inputs are influencing each other?

I have read this raspberry-gpio-python link which suggests to use a 0.1u capacitor and maybe even using both the built-in pull-down AND a capacitor.

I have a couple of questions to this:

1) Should I use a 0.1u capacitor and how should it be wired?

2) Should I use my own 10K ohm resistor instead of the built-in?

# UPDATE 1 #

This is a database extraction of the data gathered over night:

Data values from my database

There is clearly something wrong as it should be pretty fixed at aprox 5-7 in the value column.

My physical setup is like this (maybe hard to see):

My Raspberry Pi model B setup

There are a few terminal strips, 1-wires and the length of the pulse cables are aprox 1-2 meters (not twisted pair).

The electrical meters I am using are these from Kamstrup (382L), Data pulse module for electricity meters

# UPDATE 2 #

I have now tried unplugging the electrical meter from its power source and still it gives a few pulses per hour!? For sure there is some interference but I cannot seem to figure out how to avoid this?

# UPDATE 3, 2014-12-10 21:30 #

I don't understand how this is possible (this is from my logfile):

20:32:06.373451 GPIO [1] pulse [425] detected (1.969 seconds since last = aprox 1828.7W)
20:32:06.378121 GPIO [1] pulse [426] detected (0.005 seconds since last = aprox 748908.6W)

I am recieving two pulses within 5ms - this should be impossible due to the 350ms bouncetime I have setup in the callback!?

I have now shifted from using the 3.3V to using the GND and I have shifted the pull-down to be a pull-up and trigger on RISING edge. This has certainly helped on the ghosting but it has not completely removed it and I am still receiving ghost-inputs on GPIO 3 (pin 15) even though the electrical meter is not even plugged in to its power souce (and thereby it should NEVER give out a pulse)! :-/

# UPDATE 4, 2014-12-12 14:55 #

Today I have been watching my electrical meter and seen with my own eyes that there in fact are "ghost inputs" - meaning it registered a pulse but it didn't blink on the meter. I saw these pulses (two of them ghosts):

14:10:49.586792 GPIO [3] pulse [23] detected; real pulse
14:21:38.909866 GPIO [3] pulse [24] detected; real pulse
14:30:46.887632 GPIO [3] pulse [25] detected; GHOST pulse
14:32:30.498948 GPIO [3] pulse [26] detected; real pulse
14:37:46.849414 GPIO [3] pulse [27] detected; GHOST pulse
14:43:16.992208 GPIO [3] pulse [28] detected; real pulse

I viewed my logfile to see if I could find any common grounds and it seems that the two above ghost pulses are happening when the RasPi get pulses from two other electrical meters almost simultaneously (within a few milliseconds).

Will it help to have hardware debouncing in this case?

# UPDATE 5, 2014-12-14 20:11 #

Though I have dramatically reduced the weird ghost pulses by replacing the pulse wires with network twisted pair cables, I am not sure I have 100% eliminated them as I suspect there to be minor issues when receiving multiple pulses simultaneous on several GPIO pins. I am considering to try out the hardware debouncing option, which I have not done yet - just to rule out if this can help or not.

Can someone please advice if this drawing below will be perfect for my setup? Is the resistor and capacitor good with a 30ms pulse from the electrical meter or should I instead use a bigger/10uF capacitor (10ms?)? Then of course I should scrap the internal pull-up resistor and the software debouncing.

The full Kamstrup 382L data sheet is here but this is what they write about its "S0 supply":

Sends 24 V via 2-wire and pulses by drawing the voltage to 0 V at every pulse.

This is what I plan to test:

Hardware debouncing

Sorry for reasking this again but I really want to make this as good as possible and not fry my RasPi (I have no clue about hardware);-)

  • If anyone can suggest an addon board I can buy which will completely eliminate the ghosting that would be great. Ideally I want the best possible solution I know I can trust and currently I am not too optimistic with my own setup (I don't know anything about hardware) – DHS Dec 10 '14 at 20:19
  • In my case I solved using 0.1uF capacitor between GND and GPIO – Alessandro Dionisi Mar 17 at 16:32
2

This is not really a Pi question, but an electrical engineering question.

I assume the "electrical meter" is producing contact closures, but this is unclear.

You are working in a noisy environment, so you should use good practice.

  1. Connect to the Pi GND as common NOT the +3.3v rail.

  2. Take measures to reduce interference. E.g. use twisted pair (or shielded cable) - a separate cable for each meter, with the only common connection the GND at the Pi end.

  3. Use low impedance circuitry e.g. a low value pullup e.g. 1kΩ unless there is power limitation.

  4. Adopt bounce suppression. Remember this needs to be BOTH on contact closure AND contact open. This can be done in software and/or with capacitor to slow transitions.

There are other isolation measures, but the above should suffice for most purposes.


EDIT 2. The picture you pasted seems to show a separate cable for each. Using power cable seems overkill, and simple twisted pair would be better. 3. You are using internal pullups i.e. 50-150kΩ. Use 1kΩ to reduce impedance. 4. I don't know the detail of Python bounce suppression, but the device only puts out 35mS pulses. I would use a capacitor from each input to GND. This ensures the contact closure will deliver a sharp pulse but bounce (or contact open) will be slowed down. You can calculate the time constant from R*C. 1kΩ * 100nF gives 100μS. I would use larger capacitors.

The rats nest of wiring is hardly going to be resistant to interference. A Pi with just unterminated wires will trigger interrupts in a normal environment. You need to use better layout.

  • I have updated my problem - does this change anything? So you recommend dropping the 3.3VDC and instead use a GND? Will I then need a 1K ohm resistor to 3.3VDC as a pull-up? – DHS Dec 9 '14 at 12:04
  • I can't claim that using GND as common will solve all your problems, but many people assume that you need +3.3V. This is bad practice (not least because of the risk of shorting +3.3V), but increases the possibility of interference. I would have used 1kΩ to +3.3V. I was going to suggest @joan piscope, but with the low number of pulses you could just log the actual times. If these are clustered it will suggest bounce, otherwise interference is likely. The electrical meterbox is a hostile environment. – Milliways Dec 9 '14 at 22:34
  • It's a little too soon to say this has been solved but I have now changed it to GND instead of 3.3V and it seems to have improved a lot on the ghosting though it has not completely been removed. Do you mind elaborating on point 2+3+4 in your answer? How can I do this? – DHS Dec 10 '14 at 19:13
  • Please see my last update as I think you are the one closest to fix my problem :-) Just to fully understand you - do you recommend I use a 1kΩ resistor between 3.3V and each GPIO plus a 100nF capacitor between GND and each GPIO? I will also change the pulse power cables with smaller cables (I have some network twisted pair cable I can use). – DHS Dec 12 '14 at 14:10
  • Yes. This looks like the right approach. – Milliways Dec 12 '14 at 22:22
4

I had exactly the same problem with these interrupt events under Python. I believe this is a sofware bug in the lib. I do not use these Interrupts anymore, but now I am polling the GPIO. This works perfect. The pulses has to be longer then 15 ms, as the Python sketch is scheduled by Linux. If you are interested, I can send the sketch. The Pi is used as a LAMP server, I can monitor Electrity (Solar system), Gas and Water with it. Also temperature is monitored with DS18B20 sensors.

3

No harm in using a capacitor. Connect one between each gpio you are reading and a ground pin. As close as possible to the gpio's pin.

Similarly no harm in using your own 10K resistor instead of the built-in (50K) resistor. The lower value will give better protection against interference on long wiring.

That said, a bounce-time of 350 ms should be more than sufficient to squash any spikes, so there is something odd going on.

  • I have updated my problem - does this change anything? So your recommendation is to drop the internal pull-down and use my own 10K ohm resistor between the GPIO and GND? And then a 0.1u capacitor between GPIO and GND too... in a parallel setup??? – DHS Dec 9 '14 at 12:02
  • Yes. But as I say the bouncetime of 350 ms should remove the need for a hardware solution. Could you confirm you are receiving one pulse per kWh? Personally I'd be looking at the pulses with my piscope to check that the pulses are real. – joan Dec 9 '14 at 12:57
  • Currently I have no chance of checking with piscope as I have no X environment (not sure if Raspbian has one?). I have now tried unplugging the electrical meter from the power souce (so only the pulse wires are connected to the RasPi but with no pulses) and still it gives a few pulses per hour!?!?? So for sure there is some interference somewhere - but how to avoid it? – DHS Dec 10 '14 at 11:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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