demo code
const int sensorPin = A0; // pin that the sensor is attached to
int sensorValue;
int time=1;
float current;
float effective_value;
void setup() {
Serial.begin(115200);
}
void loop() {
int sensorMax = 0;
while (millis() <time*1000) {
sensorValue = analogRead(sensorPin);
// record the maximum sensor value
if (sensorValue > sensorMax) {
sensorMax = sensorValue;
}
}
time++;
current=(float)sensorMax/10245/8002000000;
effective_value=current/1.414;
Serial.println("The amplitude of the current is(in mA)");
Serial.println(current, 1);
Serial.println("The effective value of the current is(in mA)");
Serial.println(effective_value, 1);
}
Its a current-transformer for measuring AC current only. It will be designed mainly for mains frequencies - the datasheet will say more.
The breakout board appears to have an adjustable load resistance already fitted so it should be outputing an AC low voltage output. Unfortunately that's not good news since the Arduino analog inputs don't like negative voltage inputs - the documentation at seeed suggests directly connecting it to the analog inputs as far as I can see which is not good - at the very least have a 10k resistor in series with its output to act as protection.
Even then you only get to sample only the positive half-cycles.
The code example they give isn't particularly clever - its assuming the mains current waveform is sinusoidal, which is unlikely to be the case for many mains loads. The smart technique is to sample frequently and calculate the root-mean-square value over the whole AC cycle.
Thank you very much MarkT.
Ok i'll use a bridge in order to get only positive value, and i'll add little capacitor to obtain a less variable wave.
i'll try to find some code to determinate the root-mean-square value
I don't think you realize that a bridge rectifier will drop a substantial voltage (compared to your signal) and give gross errors. Any signal less than a diode forward-voltage-drop will be lost.
If you are not interested in measuring rms but are happy with just peak measurements then their approach is OK, but you do want to protect the analog input with a that current-limiting resistor. You could add a diode shorting out the reverse signal (anode to ground, cathode to analog input).
ok so i'll put a diode for the reverse signal only.
then i'll calculate the root-mean-square of some sample.
The question is, ok but the wave is sinusoidal not a square wave so i'll introduce huge amount of errors... i'll take a lot of ramp value....
if i'll attach a capacitor, the average value should be closest to the real ?
If you sample at 1kHz, say, and square and then average the samples you get the mean-square - take the square root to get RMS.
If you just take the peak value then there are two problems:
you assume the waveform is sinusoidal - typically this won't be the case, since most loads are not linear (only heaters and tungsten lighting are linear really). Also the voltage may not be very sinusoidal (it should be but all the non-linear devices tend to affect it).
If the load fluctuates the peak value will consistently read high.
So it matters if accurate current measurement is your goal, but not if you just need a ballpark reading or want to detect changes.
To be accurate you still have to calibrate the TA12 and ideally measure the negative half-cycles too...
They have done all the heavy lifting as far as sampling data directly from AC lines is concerned - i.e. putting on an external DC bias to keep all the voltages inside a measurable range for the Arduino, processing the bias back out in software later, calculating power, etc. All that and documented circuits to keep your Analog pins working.
I'd go over the stuff there before going any further, the examples they have listed and the two AC power theory pages are excellent primers.