# ovf with max(x,y) on analogRead

Why do I get an ovf on line 30 (noise=maxCurr;)? Performing 10x (for loop) analogRead(0) and taking the maximum value (maxCurr) gives ovf.

The noise formula actually should be noise=maxCurr-minCurr; but for debugging I disply maxCurr only.

``````/*
Measuring Current Using ACS712
*/

void setup(){
Serial.begin(9600);
}

void loop(){
float current;
float maxCurr;
float minCurr;
int rawcurr;
float noise;
static char disp[7];
for( int i=0; i<10; i++)
{
current = current + (516 - rawcurr) * 30/1023.0;
maxCurr = max(maxCurr, rawcurr);
minCurr = min(minCurr, rawcurr);
delay(50);
}
current = current / 10.0;
dtostrf(current, 5,3,disp);
//maxCurr = (516 - maxCurr)*30/1023,0;
//minCurr = (516 - minCurr)*30/1023,0;
noise = maxCurr;
delay(10);
//Serial.print( current);
//Serial.print((float)(current),2);
//Serial.print(" , ");
Serial.print(disp);
Serial.print(" , ");
Serial.print(noise);
Serial.print(" , ");
Serial.print(current, 3);
Serial.print(" , ");
Serial.println(rawcurr);
}
``````

Solved: I do not reset the maxCurr and minCurr variables entering the loop so carrying the values from the previous loop back into te for loop. In setup I write now: float maxCurr = 0; float minCurr = 0;

I hope this gets your approval?

New issue: now I always get value 0 for minCurr.

What do I do wrong?

I hope this gets your approval?

No, it does not. Creating new variables that are local to setup(), and that have the same name as global variables, does not make sense.

Learn when to use a type in front of a variable name, and when not to. THAT would meet with our approval.

What do I do wrong?

You made code changes without posting the code, so you are asking us to guess what you are doing wrong. I don't care much for guessing games, so I'll pass.

What PaulS said. Plus this:

``````float minCurr = 0;
``````

is not going to work. The analogRead function returns values from 0 to 1023. Therefore min(minCurr,rawcurr) will always return zero. You need to initialize minCurr to a value that is at least as high as the highest value that can occur.

Pete

Hi PaulS and el_supremo, thanks for the feedback; I corrected the code for the min calculation.

Here is the code: please peruse and I appreciate your comments. I am sorry for my previous post, code will always be included in the future.

``````/*
Measuring Current Using ACS712
*/
void setup(){
Serial.begin(9600);
}

void loop(){
float current;
float maxCurr = 0;
float minCurr = 516;
int rawcurr;
float noise;
static char disp[7];
for( int i=0; i<10; i++)
{
current = current + (516 - rawcurr) * 30/1023.0;
maxCurr = max(maxCurr, rawcurr);
minCurr = min(minCurr, rawcurr);
delay(50);
}
current = current / 10.0;
dtostrf(current, 5,3,disp);
noise = (maxCurr - minCurr)*30/1023.0;
delay(10);
//Serial.print( current);
//Serial.print((float)(current),2);
//Serial.print(" , ");
Serial.print(disp);
Serial.print(" , ");
Serial.print(noise);
Serial.print(" , ");
Serial.print(current, 3);
Serial.print(" , ");
Serial.println(rawcurr);
}
``````
``````rawcurr = analogRead(0);
current = current + (516 - rawcurr) * 30/1023.0;
maxCurr = max(maxCurr, rawcurr);
minCurr = min(minCurr, rawcurr);
``````

Why are maxCurr and minCurr float, when rawcurr is int? Why is rawcurr not rawCurr?

PaulS:

``````rawcurr = analogRead(0);
``````

current = current + (516 - rawcurr) * 30/1023.0;
maxCurr = max(maxCurr, rawcurr);
minCurr = min(minCurr, rawcurr);

``````

Why are maxCurr and minCurr float, when rawcurr is int? Why is rawcurr not rawCurr?
``````

The true answer is that I do not know, but you are correct: I changed the code to reflect you comments.

How do I make a printout of the serial monitor?

``````/*
Measuring Current Using ACS712
*/
void setup(){
Serial.begin(9600);
}

void loop(){
float current;
int maxCurr = 0;
int minCurr = 516;
int rawCurr;
float noise;
static char disp[7];
for( int i=0; i<10; i++)
{
current = current + (516 - rawCurr) * 30/1023.0;
maxCurr = max(maxCurr, rawCurr);
minCurr = min(minCurr, rawCurr);
delay(50);
}
current = current / 10.0;
dtostrf(current, 5,3,disp);
noise = (maxCurr - minCurr)*30/1023.0;
delay(10);
//Serial.print( current);
//Serial.print((float)(current),2);
//Serial.print(" , ");
Serial.print(disp);
Serial.print(" , ");
Serial.print(noise);
Serial.print(" , ");
Serial.print(current, 3);
Serial.print(" , ");
Serial.println(rawCurr);
}
``````

How do I make a printout of the serial monitor?

It shows text. Highlight the text, copy it, and paste it here.

Thanks PaulS; I had to stop the autoscroll to get it to work, but here it is:

This is at 0A current.

current noise current rawCurr

0.164 , 0.34 , 0.164 , 507
0.029 , 0.29 , 0.029 , 513
0.037 , 0.32 , 0.037 , 520
0.082 , 0.69 , 0.082 , 504
0.113 , 0.40 , 0.113 , 511
0.021 , 0.21 , 0.021 , 514
0.124 , 0.32 , 0.124 , 513
0.095 , 0.45 , 0.095 , 510
0.063 , 0.45 , 0.063 , 511
0.074 , 0.48 , 0.074 , 509

Strange that there is a current being “measured”, or rather, calculated.

At 2A:

1.626 , 0.71 , 1.626 , 451
1.555 , 0.42 , 1.555 , 452
1.552 , 0.45 , 1.552 , 466
1.512 , 0.53 , 1.512 , 461
1.504 , 0.32 , 1.504 , 461
1.623 , 0.37 , 1.623 , 454
1.518 , 0.79 , 1.518 , 447
1.631 , 0.58 , 1.631 , 456

So not quite right yet: at 2A it shows about 1.6A calculated.

I did meanwhile write a value 0 in the float current at the start of the loop:

``````/*
Measuring Current Using ACS712
*/
void setup(){
Serial.begin(9600);
}
void loop(){
float current = 0;
int maxCurr = 0;
int minCurr = 516;
int rawCurr;
float noise;
static char disp[7];
for( int i=0; i<10; i++)
{
current = current + (516 - rawCurr) * 27/1023.0;
maxCurr = max(maxCurr, rawCurr);
minCurr = min(minCurr, rawCurr);
delay(50);
}
current = current / 10.0;
dtostrf(current, 5,3,disp);
noise = (maxCurr - minCurr)*27/1023.0;
delay(10);
//Serial.print( current);
//Serial.print((float)(current),2);
//Serial.print(" , ");
Serial.print(disp);
Serial.print(" , ");
Serial.print(noise);
Serial.print(" , ");
Serial.print(current, 3);
Serial.print(" , ");
Serial.println(rawCurr);
}
``````
``````current = current + (516 - rawCurr) * 27/1023.0;
``````

Where did the 516 and 27 magic numbers come from? Perhaps they are not right for the sensor you have.

Post a link to the sensor and a schematic.

Hi,

Have you measured the voltage at the analog input pin, for zero current, then 2A? This will help you to get your offset and gain values for calculation correct.

Tom... :)

PaulS:

``````current = current + (516 - rawCurr) * 27/1023.0;
``````

Where did the 516 and 27 magic numbers come from? Perhaps they are not right for the sensor you have.

Post a link to the sensor and a schematic.

The ACS outputs 2.5V (measured 2.490Vdc to 2.510Vdc with Fluke) at 0A, and 0.185mV/A. The Arduino ADC scale is 1023 units for FSD of 5V. And 0A corresponds to 1023/2=512, halfway the ADC scale. 5V / x = 0.185V / 1A.
So x = 5 / 0.185 = 27. Final formula: actual current in A = (512 - ADC value) x 27 / 1023

In my program I mistaken entered 516 instead of 512: correction below.

At 0A the output shows around 0A, at 2A, the output is around 1.4A:

current noise current rawCurr

1.293 , 0.29 , 1.293 , 457
1.383 , 0.66 , 1.383 , 462
1.415 , 0.40 , 1.415 , 463
1.441 , 0.18 , 1.441 , 461
1.372 , 0.69 , 1.372 , 461
1.462 , 0.58 , 1.462 , 458
1.380 , 0.45 , 1.380 , 453

``````/*
Measuring Current Using ACS712
*/
void setup(){
Serial.begin(9600);
}
void loop(){
float current = 0;
int maxCurr = 0;
int minCurr = 516;
int rawCurr;
float noise;
static char disp[7];
for( int i=0; i<10; i++)
{
current = current + (512 - rawCurr) * 27/1023.0;
maxCurr = max(maxCurr, rawCurr);
minCurr = min(minCurr, rawCurr);
delay(50);
}
current = current / 10.0;
dtostrf(current, 5,3,disp);
noise = (maxCurr - minCurr)*27/1023.0;
delay(10);
//Serial.print( current);
//Serial.print((float)(current),2);
//Serial.print(" , ");
Serial.print(disp);
Serial.print(" , ");
Serial.print(noise);
Serial.print(" , ");
Serial.print(current, 3);
Serial.print(" , ");
Serial.println(rawCurr);
}
``````

When I push 2A, the ACS outputs 2.28V... not quite the 2.5V - 2 x 0.185V/A..

Same result on a spare ACS, and they both are the 05B version thus 0.185V/A.

Have you got the output of the sensor and its supply bypassed with 0.1uF Caps?

Thanks.. Tom... :)

Yes, the sensor output is filtered with a 100nF cap on one unit and a self-installed extra 100nF cap on the other unit.

Hi, Exactly what model ACS712 do you have 5A or 30A?

Can you post a picture of the ACS712 and its connections to the arduino?

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Tom..Sorry but I couldn't find any reference to which model.

Tom, both units here are the 5A model.

The circuit is 5V and GND from the Arduino, and the ACS output via breadboard to a 4.7k pulldown (load) resistor. Short wires, 10cm leads.

Picture to follow.

Increase the load R to 10k or try without it, 4k7 is the data sheet MIN.