Posts: 23
Joined: Wed Mar 28, 2018 4:04 pm

CT Clamp with Raspberry Pi (AC Current Sensor)

Mon Apr 01, 2019 11:42 am

Hi there,

I trying to use my raspberry pi to measure the output from an AC current clamp. I have an ADC on my Pi to manage the analogue output from the following board...

https://thepihut.com/products/gravity-a ... ent-sensor

I'm supplying 5V to the DFRobot board and reading the output voltage through my ADC, all fine there. The 0 value of the sensor sits around 0.042V and rises up to 2V at max current. I'm trying to develop a method of converting the voltage output to a tangible AC power reading but am struggling to get something that makes sense. I've found various forums referencing Arduino code but I can't see anything different as my understanding is that a Arduino just takes in an analogue reading too, like an ADC.

Does anyone have any experience with this sort of thing and could help?

Many thanks,


Posts: 1528
Joined: Wed Dec 07, 2016 2:29 pm

Re: CT Clamp with Raspberry Pi (AC Current Sensor)

Mon Apr 01, 2019 12:13 pm

It's called Root Mean Square
http://practicalphysics.org/explaining- ... rrent.html

The C and C++ code is on rosetta for n samples to return the RMS
The assumption is there is a few samples per cycle and n should be exactly 1 cycle by preference.
That means a number of samples for every 20msec for 50hz or 16.6667ms for 60hz
If you do that it covers one cycle exactly so helps with accuracy and it doesn't matter where it starts and stops on the wave its 1 cycle
https://rosettacode.org/wiki/Averages/R ... n_square#C

It uses doubles and is not super fast but there are Fixed integer versions of RMS around but they are complex approximations I don't intend to go into.

A hack is to take the waveform across a diode into a capacitor which simply holds the peak the Voltage is 0.7071 whatever the peak held is.
https://www.electronics-tutorial.net/an ... -detector/

User avatar
Posts: 5734
Joined: Sun Jul 22, 2012 4:14 pm

Re: CT Clamp with Raspberry Pi (AC Current Sensor)

Mon Apr 01, 2019 3:11 pm

Looking at the example Arduino code it looks like the unit produces a DC voltage proportional to the peak AC current, so a simple multiplication by 0.707 will give RMS current value. And the schematic (now that I've looked at it) confirms this is what it does.

From https://www.dfrobot.com/wiki/index.php/ ... U:SEN0211)

Code: Select all

float readACCurrentValue()
  float ACCurrtntValue = 0;
  unsigned int peakVoltage = 0;  
  unsigned int voltageVirtualValue = 0;  //Vrms
  for (int i = 0; i < 5; i++)
    peakVoltage += analogRead(ACPin);   //read peak voltage
  peakVoltage = peakVoltage / 5;   
  voltageVirtualValue = peakVoltage * 0.707;  	//change the peak voltage to the Virtual Value of voltage
  /*The circuit is amplified by 2 times, so it is divided by 2.*/
  voltageVirtualValue = (voltageVirtualValue * Vref / 1024) / 2;  
  ACCurrtntValue = voltageVirtualValue * ACTectionRange;
  return ACCurrtntValue/1000;

Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

Posts: 23
Joined: Wed Mar 28, 2018 4:04 pm

Re: CT Clamp with Raspberry Pi (AC Current Sensor)

Tue Apr 02, 2019 4:04 pm

Thanks for you help everyone - I think I'm sorted now. I was doing the bits to volts conversion twice which wasn't helping.

For anyone else trying to do this is in the future. I also found that code supplied with the board works well from about 200W upwards but not for less than 200W, I changed the formula slightly after finding a directly proportional relationship for 50 - 200W loads and introduced an IF statement to switch formulas after a set value (~200W).


User avatar
Posts: 885
Joined: Thu Sep 26, 2013 1:54 pm
Location: Heath, MA USA
Contact: Website

Re: CT Clamp with Raspberry Pi (AC Current Sensor)

Tue Apr 02, 2019 8:43 pm

You could always double check by measuring the current to a 100 watt incandescent bulb, or a few of them. I'm not sure how close to 100.0000 watts they come but as dummy loads you can switch in and out a few incandescent light bulbs aren't bad. 2 should read about twice the current as 1, etc. Gives you a check on linearity.

Expecting an A/D myself any day now. Planning to hook it to a new ZeroW and a photodiode to look at global dimming, reporting over wifi.

Posts: 1
Joined: Sun Aug 04, 2019 9:53 pm

Re: CT Clamp with Raspberry Pi (AC Current Sensor)

Sun Aug 18, 2019 7:53 am

Hi there,

I am trying to achieve the same thing using exactly the same hardware. Did you by any chance use a burden resistor for the CT sensor like many tutorials suggest? Or does the ADC come with one? I can't seem to find a diagram for it.

Return to “C/C++”