[SOLVED] How to measure electric current with Grove Electricity Sensor?


could you please be so kind and give a hint to me?

I have a Grove Elextricity Sensor.

I have installed it following https://www.dexterindustries.com/GrovePi/get-started-with-the-grovepi/setting-software/

I can detect it correctly with

sudo i2cdetect -y 0

I use the Python Code you publish here: http://wiki.seeed.cc/Grove-Electricity_Sensor/

under Heading With Raspberry Pi

That is the installation I have made (see pictures) and I certainly tried the socket both ways (changed 180 degrees) to assure that the flow of power is correct.

But the sample program always shows a value of zero.

What the … do I do wrong?

Thank you and best regards, Didi

Is it in the right port?

If A0 is the right port, then yes, it is.

Hi @walter.demmelhuber,

In order to have greater than 0 readings, you need to have a current flowing through the wire.

Here are some questions / suggestions I have:

  1. Do you have an active consumer? I see there’s a wall charger there, but I’m not sure whether it’s active or not. And I’d probably stick with a higher wattage consumer (like an incandescent light-bulb) - not some 1-3W consumer - it may be possible that the sensor can’t detect such a small electrical current.

  2. Are you using a Raspberry Pi that predates 2012?

  3. Can you try reading from A0 port with another sensor and tell us what it outputs? If you have bought the GrovePi Starter Kit, you may also have received a Grove Light Sensor - please try this out.

Thank you!

1 Like

Hello, thank you for your first input.

I am a step ahead now. The values of grovepi.analogRead(sensor) range from 0 to around 36 alternating when I have an active computer screen connected. Is this about right?

about 1) yes, the Raspberry was connected to the socket but then I assume that the required power is too low
about 2) no, 2017 brand-new
about 3) no, but above gives results now

The fluctuation is normal, correct?

Hi @walter.demmelhuber,

Glad to hear it started working.

Since you’re measuring the AC induction current, it’s normal to have that fluctuation - it’s actually a sinusoidal signal.
The sinusoidal’s frequency depends on the region you live in : 50Hz for Europe and 60Hz for the US.

What we first need to do is find the peak value of the sinusoidal, then calculate the current's amplitude and finally get the effective current value (which we are interested in).

Here’s a script I wrote which should work on your setup. It hasn’t been tested, but please let me know if there’s an error with it.
I’m suggesting you use Python 3.

import grovepi
import time
import math
import sys

# Preferably use Python 3!
# Preferably use Python 3!
# Preferably use Python 3!

# this function reads the peak value of an analog pin 
# time_window specifies for how long the function reads data
def getPeakValue(time_window, analog_port):
    current_time = time.time()

    peak_value = 0
    while time.time() - current_time <= time_window:
        new_value = grovepi.analogRead(analog_port)
        if new_value > peak_value:
            peak_value = new_value


    return peak_value

# this function calculates the peak current value
def getAmplitude(analog_port):
    read_period = 0.2
    sensor_value = getPeakValue(read_period, analog_port)
    # taken from the datasheet
    # the lowest effective current it can read is 6mA -> 1.3Wh at 220 VAC 
    amplitude_current = float(sensor_value) / 1024 * 5 / 800 * 2000000 # taken from the datasheet

    return amplitude_current

# calculates the effective current we're interested in
def getEffectiveCurrent(analog_port):
    current_amplitude = getAmplitude(analog_port)

    effective_current = current_amplitude / math.sqrt(2)

    return effective_current

analog_port = 0 # connected to analog port A0 

    while True:
        effective_current = getEffectiveCurrent()
        print("effective current = {} mA".format(effective_current))

# in case the user ends the program by pressing CTRL-C        
except KeyboardInterrupt:

# in case there's an error while communicating with the GrovePi    
except IOError:
    print('[IO Error]')

Also, please don’t forget that the sensor’s lowest amplitude it can detect is 8.6mA - which means you should go with higher-wattage consumers, like above 5Wh.
For instance, a Raspberry Pi won’t get detected.

Fun fact: I’ve used the information you gave me and reached the conclusion that you’re monitor is using 68Wh if you have 220VAC at the outlet or 34Wh if you’re on 110VAC.

I’m waiting to see how it went for you.

Thank you!

Thank you very much indeed.

That is much more than I need and I can go on with my project.

Only error I got with Python 3 was ‘ImportError: No module named ‘grovepi’’ whereas it works with Python 2

But that seems to be another story.

Sorry, beginner :slight_smile:

Hi @walter.demmelhuber,

It’s nice to hear that it helped you.
It would be interesting to show us what readings you’re getting when you plug in a PC or a laptop. Maybe even a freezer?

Regarding the ImportError: No module named grovepi when trying to run with Python 3 might mean the installation hasn’t been done properly.
But just as you have said, this is another story for another thread and since it runs with Python 2 and it’s not troubling you, it shouldn’t be of any concern.

Is it okay to close the thread now?
Can we mark it as solved?

Thank you!

Hello Robert,

this shows when I connect a 750 watt vacuum cleaner to the sensor.

effective current = 0.0 mA
effective current = 0.0 mA
effective current = 0.0 mA
effective current = 0.0 mA
effective current = 8830.20309026 mA
effective current = 4954.58120607 mA
effective current = 3892.88523334 mA
effective current = 3633.93499609 mA
effective current = 3590.77662321 mA
effective current = 3590.77662321 mA
effective current = 3538.98657576 mA
effective current = 3564.88159949 mA
effective current = 3556.24992491 mA
effective current = 3530.35490119 mA
effective current = 3590.77662321 mA
effective current = 3599.40829779 mA
effective current = 3599.40829779 mA
effective current = 3599.40829779 mA

Thank you for helping me out.

Yes, it can be marked as solved.

Best regards from Germany, Didi

Hi @walter.demmelhuber,

Thank you for sharing with us these data samples. It seems to be working just about right.

If you have any more issues / questions, please inform us and we will try to have a solution for it.

Also, here’s a plot of the data you’ve shared with us.

As you can see, we can separate this graph into 3 segments/events:

  1. This is the moment when there’s no active consumer.

  2. This is the surge of power that’s created by your vacuum cleaner - this is what happens with basically any electrical device - of course, at different scales.

  3. This is the the moment when the electrical device (in our case, the vacuum cleaner) is normally operating.

Thank you!