LDR · Light Dependent Resistor · Raspberry PI

Interfacing a Electricity Meter with LDR and LM393

EDF installed a Actaris “A14C5 ST” Electricity meter at our home. Unfortunately it is a “ST” version which has no “teleinfo” interface. “ST” seems to mean “Sans Teleinfo”. However there is a pulsing yellow LED on the Actaris meter. The pulses are approximately 15ms long. For each Wh electricity consumption one pulse is emitted.

First I thought I could interface the LED with a TSL2561 Light sensor and use its interrupt. The interrupt is triggered whenever the light intensity gets below the low threshold or above the high threshold. On the lower end, its unfortunately a “lower or equal” logic. So for the periods where there is no light emission from the electricity meter, the light intensity is zero and the the interrupt triggers all the time – so this device would be usable only if I add some light to ensure a permanent, small illumination. I decided to give up this direction eventually , although I would have liked the TSL2561’s I2C Interface.

s-l500Next, I purchased a low-cost light sensor, equipped with a LDR (Light Dependent Resistor) and a comparator circuit exhibiting the LM393 IC. Its switching threshold can be adjusted with a potentiometer. Someone has re-engineered the circuit diagram of this module, shown below with my corrections and modifications. The modifications marked with the red circles are explained later.                  mh-sensor_modified

By coincidence, the LDR fits exactly into the recess of the yellow LED, no glue or scotch tape is required to fix it!

For final mounting I will only put the LDR into the recess and accommodate the module somewhere in the cable canal. To avoid interference, I use a shielded cable between the LDR and the cable. This can be seen on the picture of the Actaris A14C5 ST meter below.

dsc03651_ink_ink_liThe digital output of the light sensor module is connected via 2m cable to a GPIO on the Rraspberry Pi. The signal is active low, which means the signal is HIGH without light at the input and LOW during the light pulse. The python script waits for an interrupt on the falling edge, then increments a counter and waits for 0.4s to debounce the signal.

So far so good.

Unfortunately, I observe interrupts on the GPIO that don’t correspond to a light pulse of the Actaris meter, leading to a wrong pulse count and a un-sync to the electricity meter. Further looking into that, the root cause seems to be the event when the Raspberry Pi switches a relay, which is connected to it for other purposes.

Software Improvements

First solution is a software approach. When such a additional pulse happens, it is between two regular ones. Assuming a constant current consumption, the time difference between pulses is a constant delta = d0. In the case of an accidental detection, the time difference to the previous pulse is d1, the time difference between the accidental pulse and the next regular pulse is d2. After that, a regular pulse happens and the time difference between pulses is d0 again. in this case,

d1 + d2  ≅  d0
d1 ≤ d0/2  and  d2 ≤d0/2

Reality is more fuzzy, but the principle works well. Here is the resulting algorithm, based on 4 recorded deltas.

def peak_detect(ts): #requires a list with 4 entries
    if len(ts) < 4:
        return False

    # the 2. or the 3rd delta must be smaller or equal the half of the 1st. 0.6 is a approximation.
    tcomp0 = ts[0] *0.6
    if not ((ts[1] < tcomp0) or (ts[2] < tcomp0)):
        return False

    # the 2nd or 3rd delta must be significantly smaller than the 4th.
    tcomp2 = ts[3] * 0.7
    if not ((ts[1] < tcomp2) or (ts[2] < tcomp2)):
        return False

    # the sum of the 2nd and the 3rd delta is approximately equal the 1st/4th
    tsum = ts[1] + ts[2]

    if tsum < ts[0] or tsum < ts[3]:
        return True
    else:
        return False

In case a “peak” is detected, the count is simply decremented.

Hardware improvements

The better solution however is to avoid unwanted spikes. I added a blocking 10nF capacitor on the GPIO pin. The pull up resistor at the LM393 sensor module is 10kΩ, so relatively low currents can cause a spike. I reduce this to 1kΩ . These changes can be seen at the circuit diagram above in red circles.

With the changes, the accidental pulse detections are suppressed.  The software “peak detection” stays in the code – who knows.

Some Interesting Measurements
scope_falling_edge-with-no-mod
Falling edge with no modification of the LM393 sensor module, taking 2us . It is ringing, oscillating with approx. 4Mhz.
scope_falling-edge-with-1k-and-10n-mod
Falling edge after the modification. it takes approx. 4us.
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s