EM-1: The microcontroller/programming part

This is the actual electronics for this project:

Here’s the theory.

The idea is  that you first need a reference voltage to compare your sensor readings to. The voltage at the street varies a bit, and the voltage inside a building varies a bit more, depending on demand and other factors. In our case, the lowest on record is 106 volts and the highest is 122 volts. Since the circuit and program generally calculate power as a function of both current and voltage (P = V*I) then you want to make certain that the voltage is known.

The electrical circuit is quite simple. I used the calculation and circuit on the openenergymonitor.com site. Thanks for sharing, Trystan.

The schematic for the voltage divider is

Click on it!

where R3 and R4 are 10 K Ohms and R1 and R2 are 100 Ohms and 1 K Ohms respectively. C1 is 10 uF. Basically, R3 and R4 take the voltage from the Arduino power supply, 5 volts, and divide it by 2. R1 and R2 take the voltage from the transformer (about 10 volts in my case) and divide it by 10 (1000/100), giving about 1 volt. So, the Arduino analog pin reads a voltage that varies from 2.5 volts – 1 volt to 2.5 volts + 1 volt, or 1.5 to 3.5 volts. This rough circuit has plenty of safety margin, and a simple program can tell the Arduino to transform this varying voltage into usable data.

Next we need to measure current (the flow of electricity at a given moment) with sensors. The current sensors are made of a circular (or square) ferrite core around which a transformer is wound (from very fine wire). The “laws of electrical current flow” are such that when an amount of electricity flows through (current) a ferrite (or any magnetic-permeable metal) loop, a wire wound around the ferrite loop will be “excited” by a proportional current. For this to work, the source current has to be alternating. This is exactly what we have in most places around the world, alternating 60 times per second in parts of the world, and 50 times per second in other parts of the world.

The number of turns in the wound coil determines the output current induced by the main current. For example, 3000 turns effectively divides the source current by … 3000. So, if the current in the red wire is 100 amps, the current induced into the secondary winding will be around 33 mA (100/3000 = .033).

The same voltage divider is used to provide 2.5 volts for input, but the burden resistor has to be calculated for the given sensor. The burden resistor effectively transform the current output of the sensor into a readable voltage sent to the Arduino. Many sensors do not need the burden resistor. They contain an equivalent transformer that acts as a transistor.

In my case, the voltage divider made of the two resistors that provide 2.5 volts with the electrolytic condenser providing noise filtering is shared between the sensors. The voltage divider (100/1000) and burden resistors are calculated for, and assigned to, a single sensor/analog pin.

I think that my theoretical knowledge makes sense, and measurements concur. I will test a different version that uses the Arduino 3.3v output as voltage source.

I’m using a prototyping board for the moment.  I’m using multiple sensors of different type (current generator and voltage generators) but they all share parts of the circuit. The transformer on the right is from my FCB1010. When I replaced the brain, the power supply became redundant. The Arduino in the PU-2 is powered by a standard 9 volt supply (or a 7-12 volt battery) or directly from a USB port at 5 volts.

The Arduino Code

I use an Arduino, of course. The idea is to measure analog data from the sensors and calculate Voltage, Amperage and different Power numbers. I based my code on Trystan Lea’s code. I actually blatantly copied it and then modified it a bit.

The Processing Code

I wanted to be able to read/process the information coming in from the Arduino. I linked the Arduino to a file server running Linux using a USB cable. The Processing code receives the information over the serial port.

The first step is to prepare the data on the Arduino. As mentioned, I use Trystan’s libraries and code. But I added the part to format the data:

  Serial.print(ch1.Vrms);
  Serial.print(",");
  Serial.print(ch2.Irms);
  Serial.print(",");
  Serial.print(ch2.apparentPower);
     Serial.print(",");
  Serial.print("|");

So, I’m sending numbers (voltage, current and apparent power values) separated by commas with a pipe character “|”(ASCII 124) to mark the end of the string.

On the computer, a simple program written in Processing wait for the data, effectively waiting for a “|”, and then processes it.

import processing.serial.*;

Serial myPort; 

PFont myFont;
float var1, var2, var3;

void setup() {
  size(400, 200);
  myFont = createFont("Arial", 18);
  textFont(myFont, 18); 

  println(Serial.list());
  //Get data from arduino through first USB port
  myPort = new Serial(this, Serial.list()[0], 57600); 

  //buffer input from arduino until "|" character
  //then call serialEvent
  myPort.bufferUntil(124);//Ascii value for "|"
}

void draw()
{
  background(0);
  fill(255);
  textFont(myFont, 18);
  //Show values in the box
  text("Volt: " + var1, 10, 50);
  text ("Amp: "+var2, 10, 70);
  text ("Power: " + var3, 10, 90);
}
//read the feed from Arduino in Processing
//Called automatically because of the "bufferuntil" statement
void serialEvent(Serial myPort) {

  String myString = myPort.readString();
  if (myString != null ) {
    //   myString = trim(myString); //remove whitespace around our values
    String inputs[] = split(myString, ',');
    //now assign your values in processing
    if (inputs.length == 4) {
      var1 = float(inputs[0]);
      var2 = float(inputs[1]);
      var3 = float(inputs[2]);
      print(var1+" ");
      print(var2+" ");
      println(var3+" ");
    }
  }
}

The code waits for a trigger using “bufferUntil()” and then calls serialEvent() which cuts the received string into pieces and then prints them on the console as it prints them inside the box.

The final version uses Pachube.com. It sends messages to Pachube requests so that the whole planet can see how inneficient my office power consumption is.

This entry was posted in Arduino, EM-1 and tagged . Bookmark the permalink.

2 Responses to EM-1: The microcontroller/programming part

  1. Nilin says:

    Hello sir,

    Why do you have to put the capacitor in the circuit?

    • rt says:

      It was in the original circuit, identified as a noise controller. I could have tested without it… But I didn’t even think about it????

Leave a Reply

Your email address will not be published. Required fields are marked *