[help me] Alternating CurrentAC HST21-200A

I want to measure current with HST21-200A, But the current code is wrong. You help me with this problem… Thank

int HST21 = A0;
double Voltage = 0;
double VRMS = 0;
double AmpsRMS = 0;

void setup()

void loop()
Voltage = Vpp();
VRMS = Voltage * 200.0;
AmpsRMS = VRMS * 0.707;
Serial.print("AmpsRMS: ");

float getVPP()
float result;

int readValue;
int maxValue = 0;

uint32_t start_time = millis();
while((millis()-start_time) < 1000) //sample for 1 Sec
readValue = analogRead(HST21);
if (readValue > maxValue)

maxValue = readValue;

// Subtract min from max
result = maxValue * 4.0 /1023.0;

return result;

The Arduino can be damaged by negative voltages (the negative-half of the AC waveform) or by voltages greater than +5V, although the current transformer might not put-out enough current to kill it.

If you want to be safe, [u]bias[/u] the input at 2.5V (that will allow a 5V peak-to-peak, or about 1.75V RMS, AC waveform). And/or use a [u]voltage protection circuit[/u]. (Be sure to subtract the bias out of your ADC reading.)

If necessary, add a [u]voltage divider[/u] (2 resistors) to knock-down the voltage.

I see a few problems with your code - Your main loop is never calling getVPP(), so that function never runs and you never read the ADC, etc.

And, although the function getVPP() is defined, Vpp is not.

Your math looks messed-up, and the default voltage reference for the ADC is 5V, not 4V.

I didn't find the manufacturer's datasheet for that part, but if 4V is 200A, your code should give you that. 4V RMS is 5.6V peak (which the Arduino can't actually read), but let's start with that... 5.6V would "theoretically" give you an ADC reading of 1146, and that should convert to 200A.

With some algebra, you can figure-out a single ratio (factor) that converts the peak ADC reading to Amps, without making the conversion to voltage or to RMS every time in software. (It is a good idea to explain the conversion-factor in a comment in case you forget where it came from, or in case someone else reads your code.)

If you actually have to read 200A you'll need a voltage divider. And again, that can be figured-into your ADC-to-Amp conversion factor.

I suggest you start simple - Just write a loop that reads (and shows) the raw ADC readings.

When that "looks right" add some code to find the peak.

Then, add the timer to find the peak ADC reading every second or so.

If you know the current, do the calculations "manually" to check your math.

Add the calculation-factor for Amps and compare it to the known current or you manual calculation.

You can also test your code/conversions by writing/forcing the ADC variable, instead of using a real reading. (You can't write to the ADC, but you can write/set the value of a variable.)

Hint - When I write a function (such as your getVcc() function) I usually start with a "do nothing" function that just prints-out a message, like "Running getVcc()". And if returns a value, I'll return a known value. Once I know the function is compiling and being called and running, I'll "develop" the function, writing and testing a few lines at a time until it's completely done and debugged.

Your entire program should be developed that way... Don't try to write the whole program at once. "Develop" it, a little at a time, testing as you go. That takes some practice because the code has to "make sense" to the compiler... You can't just write the "top-half" of program because it won't compile.