# Current Transducer for Spark

**URL:** <https://community.particle.io/t/current-transducer-for-spark/9262>\
**Category:** Hardware\
**Created:** [January 9, 2015, 8:48pm UTC](https://community.particle.io/t/current-transducer-for-spark/9262 "2015-01-09T20:48:44Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![Herner](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/herner/32/4292_2.png) [@Herner](https://community.particle.io/u/Herner)\
**Post date:** [January 9, 2015, 8:48pm UTC](https://community.particle.io/t/current-transducer-for-spark/9262/1 "2015-01-09T20:48:44Z")

</div>

Hey Guys did a search did not get any results. Has anyone have a recommendation of a current sensor for monitoring power consumption on an external device ( 115 volt ) with the spark core?

I am looking for a product recommendation. Thanks!

---

<div class="post-metadata">

**Author:** ![Moors7](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/moors7/32/588_2.png) [@Moors7](https://community.particle.io/u/Moors7)\
**Post date:** [January 9, 2015, 9:36pm UTC](https://community.particle.io/t/current-transducer-for-spark/9262/2 "2015-01-09T21:36:34Z")

</div>

Well, these topics popped up after I searched for “current”:

> **[Current sensor ACS712](https://community.particle.io/t/current-sensor-acs712/3775?redirected=true)**
>
> Iam using this sensor http://hacktronics.co.in/home/248-acs712-5a-range-current-sensor-module.html With no load or with 120mA load values from analogRead are about 3720± Is ACS712 capable to sense smalll current? Vout on acs712 shows 2.41V(2.5V=0A...

  
and this one:  

> **[INA219 High Side DC Current Sensor - Spark Library](https://community.particle.io/t/ina219-high-side-dc-current-sensor-spark-library/3039?redirected=true)**
>
> I have a few of these voltage & current sensors that I bought from Adafruit and I want to get them working with the Spark Core. I have not tried to get this up and running yet but I’m assuming some code modification will be needed. Can somebody...

---

<div class="post-metadata">

**Author:** ![Herner](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/herner/32/4292_2.png) [@Herner](https://community.particle.io/u/Herner)\
**Post date:** [January 9, 2015, 10:16pm UTC](https://community.particle.io/t/current-transducer-for-spark/9262/3 "2015-01-09T22:16:53Z")

</div>

Read both ( unlike some new folks I do use the search ) first one is not very reassuring to try that particular device and the second one is for measuring DC and I am looking to measure line voltage which is 115volts AC

Thanks though.

---

<div class="post-metadata">

**Author:** ![peekay123](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/peekay123/32/810_2.png) [@peekay123](https://community.particle.io/u/peekay123)\
**Post date:** [January 9, 2015, 10:25pm UTC](https://community.particle.io/t/current-transducer-for-spark/9262/4 "2015-01-09T22:25:23Z")

</div>

@Herner have you seen this post:

> **[Open Energy Monitor Port](https://community.particle.io/t/open-energy-monitor-port/3166)**
>
> OK here goes. Here is a port of the monitoring part of the Open Energy Monitor - very little needed to be changed. I had an Arduino/Raspberry Pi energy monitor running for a couple of years but the Spark frees me from a long run of wires and gives me...

---

<div class="post-metadata">

**Author:** ![bko](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/bko/32/20704_2.png) [@bko](https://community.particle.io/u/bko)\
**Post date:** [January 9, 2015, 10:30pm UTC](https://community.particle.io/t/current-transducer-for-spark/9262/5 "2015-01-09T22:30:22Z")

</div>

There are a lot of ways to do this depending on what your goals are.

The old (2009) Tweet-A-Watt from Lady Ada is good reading and Newegg has the Kill-A-Watts for around US$20. You are going to be hard-pressed to engineer your own solution for that price

> **[Tweet-a-Watt kits now available…](https://blog.adafruit.com/2009/03/26/tweet-a-watt-kits-now-available/)**
>
> Tweet-a-Watt kits are now available! A few months ago, using “off-the-shelf hardware”, we modified a Kill-a-Watt(TM) power meter to “tweet” (publish wirelessly) the daily KW…

I have used cheap little current transformers but they can be a bit non-linear. They are great if you just want a binary indication of the device is drawing current (above some threshold) or not, and you can be very isolated from the 120VAC.

Maybe if you told us what you are trying to do we could help more?

---

<div class="post-metadata">

**Author:** ![Herner](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/herner/32/4292_2.png) [@Herner](https://community.particle.io/u/Herner)\
**Post date:** [January 10, 2015, 12:02am UTC](https://community.particle.io/t/current-transducer-for-spark/9262/6 "2015-01-10T00:02:02Z")

</div>

@peekay123 great project I read it months ago but thanks for reminding me about it. I just re-read it and it will likely help me with some items on my list to knock off.

@bko You are right I am being a bit ambiguous in my description my apologies. I have a variable speed external device where I want to monitor its power consumption. It will use as few as 5 watts where it idles and as many as 80-100 watts.

Thanks all appreciate you helping me out.

---

<div class="post-metadata">

**Author:** ![Herner](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/herner/32/4292_2.png) [@Herner](https://community.particle.io/u/Herner)\
**Post date:** [January 10, 2015, 12:20am UTC](https://community.particle.io/t/current-transducer-for-spark/9262/7 "2015-01-10T00:20:22Z")

</div>

@peekay123 did a bunch more reading and since my voltage will almost always be a constant your link may be what I need afterall.

[http://openenergymonitor.org/emon/buildingblocks/how-to-build-an-arduino-energy-monitor-measuring-current-only](http://openenergymonitor.org/emon/buildingblocks/how-to-build-an-arduino-energy-monitor-measuring-current-only)

So long as EMON has been ported to spark that should do me fine.

---

<div class="post-metadata">

**Author:** ![bko](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/bko/32/20704_2.png) [@bko](https://community.particle.io/u/bko)\
**Post date:** [January 10, 2015, 5:52pm UTC](https://community.particle.io/t/current-transducer-for-spark/9262/8 "2015-01-10T17:52:55Z")

</div>

I dug a bit this morning and this is the current sensor that I used in the past:

[http://www.crmagnetics.com/Products/CR2550-P13.aspx](http://www.crmagnetics.com/Products/CR2550-P13.aspx)

You can replace the LED with your own opto-isolator or use it directly. They have other solutions but this was the cheapest way to go for me in the past.

---

<div class="post-metadata">

**Author:** ![nickgb](https://avatars.discourse-cdn.com/v4/letter/n/2bfe46/32.png) [@nickgb](https://community.particle.io/u/nickgb)\
**Post date:** [January 12, 2015, 9:42pm UTC](https://community.particle.io/t/current-transducer-for-spark/9262/9 "2015-01-12T21:42:03Z")

</div>

@Herner I have a Spark monitoring my house power consumption and Solar generation output and use a similar type of AC current clamp to the one typically used by the open energy monitor. They seem to work pretty well feeding the analog inputs of the Spark with a couple of analog components to shift the DC level and some filtering if you feel it necessary. The clamps I use are rated for 60 A and seem to work well down to 1A and below. I am not sure quite how linear they are but I can see the fluctuations from individual light bulbs being turned on and off so the resolution is good.

There is a 5A version which sound as though it would suit your application.

Look for the SCT-13-005 on ebay.

The advantage of a clamp is it’s isolated and relatively simple to fit. Disadvantage is it’s bulky.

---

<div class="post-metadata">

**Author:** ![Herner](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/herner/32/4292_2.png) [@Herner](https://community.particle.io/u/Herner)\
**Post date:** [January 12, 2015, 9:47pm UTC](https://community.particle.io/t/current-transducer-for-spark/9262/10 "2015-01-12T21:47:57Z")

</div>

Thanks @nickgb I also prefer the split core CT’s. Means you can put them on without messing with line voltage. For me size is not a big deal on THAT side of it 🙂 Thanks for suggesting one to try.

Are you willing to share your sketch and how you put it together? I have never used one before and am quite new to the hardware game.

---

<div class="post-metadata">

**Author:** ![nickgb](https://avatars.discourse-cdn.com/v4/letter/n/2bfe46/32.png) [@nickgb](https://community.particle.io/u/nickgb)\
**Post date:** [January 13, 2015, 7:50pm UTC](https://community.particle.io/t/current-transducer-for-spark/9262/11 "2015-01-13T19:50:07Z")

</div>

Hi @Herner. This is the main code, it use it used to be in an Arduino and was my first Spark project. I can’t claim to be original as most of this is inspired by the open energy monitor. There are two current probes on A1 and A2 and a voltage reference from a 6 volt transformer (reduced down) on A0, just like the Open Energy Monitor. The method below is a reasonably straight reproduction of a covariance calculation (= real power) method from Donald Knuth [Wikipedia - covariance](http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#cite_ref-5). It is a little more complicated than some methods but has the advantage that it takes any DC offsets out of the analog inputs automatically.

You will see there is some smoothing of the results at the bottom then I put the data into a structure and share it with other devices on my Home wifi as a UDP packet.

```auto
// main calculation of RMS voltage, RMS current and real power.
// this is based on the algorithm Knuth popularised for running mean and variance calculation
// Real Power is calculated as covariance of V & I

// initialise loop variables
n = 0;
vbm = vbn = V2m = V2n = 0;
ibm1 = ibn1 = I2m1 = I2n1 = 0;
ibm2 = ibn2 = I2m2 = I2n2 = 0;
ibm3 = ibn3 = I2m3 = I2n3 = 0;
Pm1 = Pn1 = 0;
Pm2 = Pn2 = 0;
Pm3 = Pn3 = 0;
errf = 0;

while( true ){ // loop runs 
    n += 1.0; // increment number of samples
    
    // save the last values of variables (some are needed below)
    ibm1 = ibn1; 
    I2m1 = I2n1;
    ibm2 = ibn2; 
    I2m2 = I2n2;
    ibm3 = ibn3; 
    I2m3 = I2n3;
    vbm = vbn; 
    V2m = V2n; 
    Pm1 = Pn1;
    Pm2 = Pn2;
    Pm3 = Pn3;

    // get the ADC values - voltage is read in the middle to minimise the phase difference between current and voltage
    data1_I = ((double)analogRead(A1))/Ical1;
    data0_V = ((double)analogRead(A0))/Vcal;
    data2_I = ((double)analogRead(A2))/Ical2;
    
    // Variance calculations remove any DC offset so are effective for RMS AC signals with no DC component
    // feed 1 is the solar feed into the house
    // feed 2 is the grid feed into the house (negative if exporting)
    // feed 3 is the sum of the other two feeds so *should* be the power used by the house.
    ibn1 = ibm1 + (data1_I - ibm1)/n; // Mean(I1)
    I2n1 = I2m1 + (data1_I - ibn1) * (data1_I - ibm1); // Var(I1)*n

    ibn2 = ibm2 + (data2_I - ibm2)/n; // Mean(I2)
    I2n2 = I2m2 + (data2_I - ibn2) * (data2_I - ibm2); // Var(I2)*n

    ibn3 = ibm3 + (data1_I + data2_I - ibm3)/n; // Mean(I3)
    I2n3 = I2m3 + (data1_I + data2_I - ibn3) * (data1_I + data2_I - ibm3); // Var(I3)  

    vbn = vbm + (data0_V - vbm)/n; // Mean(V)     
    V2n = V2m + (data0_V - vbn) * (data0_V - vbm); // Var(V)*n

    Pn1 = Pm1 + (data0_V - vbn) * (data1_I - ibm1); // Covar(V,I1) - this is power
    Pn2 = Pm2 + (data0_V - vbn) * (data2_I - ibm2); // Covar(V,I2)
    Pn3 = Pm3 + (data0_V - vbn) * (data1_I + data2_I - ibm3); // Covar(V,I3)
    
    if (te <= millis()) break;
}

te += looptime1; // time next iteration ends
  
// final calculations
Pt1 = Pn1 / n;
Pt2 = Pn2 / n;
Pt3 = Pn3 / n;
Vt = sqrt( V2n / n ) ;
It1 = sqrt( I2n1 / n ) ;
It2 = sqrt( I2n2 / n ) ;
It3 = sqrt( I2n3 / n ) ;
            
// filtered output variables
VOut += alpha * ( Vt - VOut );
IOut1 += alpha * ( It1 - IOut1 );
IOut2 += alpha * ( It2 - IOut2 );
IOut3 += alpha * ( It3 - IOut3 );
POut1 += alpha * ( Pt1 - POut1 );
POut2 += alpha * ( Pt2 - POut2 );
POut3 += alpha * ( Pt3 - POut3 );

// COntrol Variable for balancing energy use
EnergyBalance = max(-1000.0, min(1000.0, EnergyBalance + Pt2 * looptime1 / 1000.0));

msg1.Bal = (short) EnergyBalance;
msg1.Ps = (short) POut1;
msg1.Pg = (short) POut2;
msg1.Ph = (short) POut3;
msg1.V = (short) (VOut*10.0);
msg1.Is = (short) (IOut1*100.0);
msg1.Ig = (short) (IOut2*100.0);
msg1.Ih = (short) (IOut3*100.0);

```

---

<div class="post-metadata">

**Author:** ![Herner](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/herner/32/4292_2.png) [@Herner](https://community.particle.io/u/Herner)\
**Post date:** [January 13, 2015, 8:47pm UTC](https://community.particle.io/t/current-transducer-for-spark/9262/12 "2015-01-13T20:47:45Z")

</div>

Thank you for taking the time to share this! I am going to order some CT’s and give it a shot.

---

<div class="post-metadata">

**Author:** ![Herner](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/herner/32/4292_2.png) [@Herner](https://community.particle.io/u/Herner)\
**Post date:** [January 13, 2015, 8:51pm UTC](https://community.particle.io/t/current-transducer-for-spark/9262/13 "2015-01-13T20:51:27Z")

</div>

@nickgb I just found these ones which appears to be what you used. Am I right? If so I am going to order a bunch 😄

> **[5A SCT-013-005 Non-invasive AC current sensor Split Core Current Transformer |...](http://www.ebay.com/itm/5A-SCT-013-005-Non-invasive-AC-current-sensor-Split-Core-Current-Transformer-/271551054755)**
>
> Non-invasive current transformer, use this sensor clip to build your own Personal Energy Monitor. Core Material: Ferrit. Input Current:0A-5A. Mechanical strength:\>500(20°C). Anti-flaming rate:UL94-V0. | eBay!

---

<div class="post-metadata">

**Author:** ![nickgb](https://avatars.discourse-cdn.com/v4/letter/n/2bfe46/32.png) [@nickgb](https://community.particle.io/u/nickgb)\
**Post date:** [January 14, 2015, 9:49am UTC](https://community.particle.io/t/current-transducer-for-spark/9262/14 "2015-01-14T09:49:58Z")

</div>

@Herner Yes that’s the one. I’m using the -030 and -060 versions. You get 1 volt out for the rated current so for the -005 version 1Volt = 5A. I checked mine against a commercial clamp current meter to get calibration values (the Ical1 and Ical2 values in the code segment I posted earlier).

Having a voltage reference is useful if you want more than just current measurements. In my case with a micro generation solar system the power into the house can go negative (exporting) so the voltage reference is essential. I have a cheap home power usage meter but it is very inaccurate below 500Watts and can’t tell the difference between me selling electricity to the power company and them selling it to me. -

---

<div class="post-metadata">

**Author:** ![Herner](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/herner/32/4292_2.png) [@Herner](https://community.particle.io/u/Herner)\
**Post date:** [January 15, 2015, 1:00pm UTC](https://community.particle.io/t/current-transducer-for-spark/9262/15 "2015-01-15T13:00:27Z")

</div>

Thanks nick for your assistance it is really appreciated. I am going to be ordering a whack of these for my mad scientist lab.

As far as the voltage goes I am going to just code it as a constant since I will be using this on a residential grid where the power factor should stay prettty constant. Plus it will make it an easy start for me! 🙂
