I see similar questions but nothing really the same, hope this isnt duplicated.

Im trying to count LED pulses from a domestic energy meter, I had a basic While: loop running but when the count gets to a certain number I need to trigger other code that takes time to execute, meaning I could miss pulses, also I was sometimes getting double counted pulses due to the duration of the LED pulse.

Im now at this stage:

import RPi.GPIO as gpio
gpio.setup(17, gpio.IN, pull_up_down=gpio.PUD_UP)
total_energy = float(raw_input ("What is the start reading on your Solar PV meter: "))/1000

def currentenergycounter(current_energy):
        current_energy = current_energy + 0.000001
        return current_energy

current_energy = gpio.add_event_detect(17, gpio.RISING, callback=currentenergycounter, bouncetime=300)
while True:
        if current_energy >= energy_reporting_increment:
                total_energy = total_energy + current_energy
                energy_log = maintainenergylog()
                send_amount = calculateamounttosend()

But Im not getting any pulses counted, at least the While: loop isnt triggering, expect the problem is with gpio.add_event_detected but help appreciated.

1 Answer 1


I think the problem is in the way callbacks work.

def currentenergycounter(current_energy):
        current_energy = current_energy + 0.000001
        return current_energy

The callback function will not do what you expect.

Firstly dump the return statement. The software which actually calls your callback doesn't care and the value will be discarded.

Secondly the parameter the callback receives is probably the GPIO (channel in RPi.GPIO terms) which triggered the callback (presumably 17).

I suggest you try the following.

def currentenergycounter(channel):
        global current_energy
        current_energy = current_energy + 0.000001

You need the global statement so that the global current_energy variable is used rather than a same named local variable. Read up on Python globals.

You also need to change

current_energy = gpio.add_event_detect(17, gpio.RISING, callback=currentenergycounter, bouncetime=300)


gpio.add_event_detect(17, gpio.RISING, callback=currentenergycounter, bouncetime=300)
  • Thanks, Im getting this error now with the currentenergycounter function 'current_energy = current_energy + 0.000001' 'TypeError: unsupported operand type(s) for +: 'NoneType' and 'float''
    – Scalextrix
    May 28, 2017 at 13:40
  • @Scalextrix edited
    – joan
    May 28, 2017 at 14:03
  • OK I figured that out I needed to remove the current_energy = before the gpio.add_event_detect
    – Scalextrix
    May 28, 2017 at 14:14
  • OK there now seems to be a problem that the edge detection in gpio.add_event_detect isntsensitive enough, the LED pulses are not triggering the counter, but if I open/close the electric cupboard door to allow light in, the edge detection works. Weird as using gpio.input() worked just fine....
    – Scalextrix
    May 28, 2017 at 14:19
  • As a matter of interest run abyz.co.uk/rpi/pigpio/examples.html#Python_monitor_py to see if that picks up the pulses (sudo pigpiod then ./monitor.py)
    – joan
    May 28, 2017 at 14:22

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.