Programming a Current Sensor ACS712 , questions and opinions needed ! help

hello Guys,
what i am trying on the long term is a device to measure the capacity of a battery and display it on a 7 segments later on in percentage display .
So i started first to learn about the Current Sensor which I have and it’s the ACS712, you can check it on this link
now i made a small arduino program which give me the voltage of an LED connected to 5V power, please check the schematic using Fritzing in the attachment files

below you will find my sketch which shows the Voltage on the LED and the step voltage for each bit from 0 to 1023, i would like please to know your opinion if it is correct, the red LED voltage is 2V and the resistor is 1K and not the 220 ohms on the schematic (because i couldn’t find a 1K resistor in fritzing)

next step is to calculate the consumption over time, so many question i have about that:
1 - when i made the calculation by hand to find the current i got I = 3mA = 0.003 A, does this means the LED is consuming a 3mA per hour instead of the 18mA that should get according the datasheet ?

2 - when i calculate this over time, would the final answer stays when i turn off the arduino ? (i think not) so how can this be done if i need to make a detector for the battery capacity percentage which should always be there when i turn off the circuit and then on ?

here is the sketch:

int AnalogPin = A0;
int data = 0;

int voltage = 5000; //mV
int analog = 1024; //from 0 to 1023 we have 1024 number
int Step = (voltage/analog) ; //mV
int Vout;

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

void loop(){

  data = analogRead(AnalogPin);
  Vout = (data * Step)/1000;
  
  Serial.print("Step: ");
  Serial.print(Step);
  Serial.println("mV");
  Serial.print("Vout: ");
  Serial.print(Vout);
  Serial.println("V");
  
  
  Serial.println(data);
  delay(500);

}

thanks in advance :slight_smile:

1 - when i made the calculation by hand to find the current i got I = 3mA = 0.003 A, does this means the LED is consuming a 3mA per hour instead of the 18mA that should get according the datasheet

It consumes 3mA, full stop. Not "per hr". mA is an absolute value.

The timed consumption is 3mAhrs. IE, 3mA drawn for 1 hr. 72mAhrs per day etc.

With 2v across the diode there is 3v across 1k so it is passing 3mA.

When you turn off the Arduino you need to save your value in the EEPROM before turning it off.

Weedpharma

weedpharma:
It consumes 3mA, full stop. Not “per hr”. mA is an absolute value.

The timed consumption is 3mAhrs. IE, 3mA drawn for 1 hr. 72mAhrs per day etc.

With 2v across the diode there is 3v across 1k so it is passing 3mA.

When you turn off the Arduino you need to save your value in the EEPROM before turning it off.

Weedpharma

thanks weedpharma for the reply,
about the EEPROM ok so now i know what’s the point from it :smiley: what i need to do after i finish from this is to learn about the EEPROM

by the way, i was modifying my codes again and again until i found a tutorial on instructable this one it uses a for loop for a sample of ADC data ! but i did not understood why is that been used ?

here are my codes in which i used it, after i used it i got some good results but i had to make all the variables float for it to work and i received a 0.25 A average on the monitor which is not realistic because the LED should consume 0.003 A ! so where does this excessive amperage come from ? is it the microcontroller ?!

Final version of codes

int AnalogPin = A0;
float data = 0;

float voltage = 5000; //mV
float analog = 1024; //from 0 to 1023 we have 1024 number
float Step = (voltage/analog) ; //mV
float Vout;

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

void loop(){
for(int i=0;i<150;i++)
{
  data += analogRead(AnalogPin);
  delay(2);
}
  data = data/150;
  Vout = (data * 5)/1024;
  float actualVal = Vout - 2.5; 
  float  amps = actualVal*10 ;
  
 
  Serial.print("Step: ");
  Serial.print(Step);
  Serial.println("mV");
  Serial.print("Vout: ");
  Serial.print(Vout);
  Serial.println("V");
  Serial.print("Current: ");
  Serial.print(amps, 2);
  Serial.println("A");
  Serial.println(data, 0);
  delay(500);

}

The loop is used to average the current draw over many reads. 150 reads is probably more than is required.

Other members have suggested using a least two reads before the read can be considered as accurate.

For the value read, we would need to see a drawing of the circuit that the sensor is wired into.

Weedpharma

weedpharma: The loop is used to average the current draw over many reads. 150 reads is probably more than is required.

Other members have suggested using a least two reads before the read can be considered as accurate.

For the value read, we would need to see a drawing of the circuit that the sensor is wired into.

Weedpharma

ahhhhh okkk ! i think i got it ! you mean because the value float around 515, 512, 513 ... so we must take some values and then calculate a consistent one ! but why did he used += here why is he incrementing !? data += analogRead(AnalogPin);

about the circuit i put one above with fritzing

but why did he used += here why is he incrementing !?

How else would you average N values? Most people would add the N values, and then divide by N.

PaulS: How else would you average N values? Most people would add the N values, and then divide by N.

oops ! sorry yes you're right my mistake :/ thanks for your reply will comment it right away

i still have 2 more problems here which i need to make sure if it's right or wrong: 1 - i am getting these values for current, which looked a bit weird to me if we go back to the schematic from fritzing which i shared up top, 0.25A ! is this real ?! all i have is an LED connect to +5 and a resistor, the current it gets from arduino +5V pin, and why it started by the 0.08A first ?

2 - the second is about using the Float data type, is there any other way of getting it ? or i think there is by making the rest calculus but would it consume more time according to the float or less ? (i can do the rest method becuase the final result will be shown on a 7-segment display so it is easy)

Step: 4.88mV Vout: 2.51V Current: 0.08A AnalogRead: 514 Step: 4.88mV Vout: 2.52V Current: 0.25A AnalogRead:517 Step: 4.88mV Vout: 2.52V Current: 0.25A AnalogRead:517 Step: 4.88mV Vout: 2.53V Current: 0.25A AnalogRead:517

  Vout = (data * 5)/1024;

So, Vout is the average of the input values, converted to a voltage. I guess, then, that you'll be computing Vin to use as an output.

  float  amps = actualVal*10 ;

Current, in amps, is voltage times resistance in Ohms. So, the current comes from a 10 Ohm resistor. Is this true?

PaulS:   Vout = (data * 5)/1024;

So, Vout is the average of the input values, converted to a voltage. I guess, then, that you'll be computing Vin to use as an output.

Vin = 5V or 5000mV but i used 5 in Vout inside the parentheses is there anything i missed ?

PaulS:   float  amps = actualVal*10 ;

Current, in amps, is voltage times resistance in Ohms. So, the current comes from a 10 Ohm resistor. Is this true?

this one i got it from the instructable link, as i understood he multiplied by 10 just to convert from for example 0.2 to 2

this one i got it from the instructable link, as i understood he multiplied by 10 just to convert from for example 0.2 to 2

Multiplying 0.2 volts by 10 does not produce 2 amps, unless the 10 is 10 Ohms.

hello everyone, i managed to make a more simple sketch, turns out it was very simple task but i wasn't seeing clearly, i should just get the ADC value and multiply it by the 5 volts voltage divided by 1024 then subtract the offset voltage and divide by the scaled voltage, below you can see the sketch i made but i am having a trouble with the values i am getting on the monitor for the current, if i do hand calculation for the current through the LED :

Power voltage = 5V red LED voltage = 2V

270 ohms Resistor Voltage = 5 - 2 = 3V

I = Vresistor/resistor = 3V / 270 ohms = 0.0111 Amp

(for schematic it is the same schematic i shared in the beginning of the post)

now when i see the results on the monitor, i am getting : Analog Value = 513 V = 2.50488 Amps = 0.02639

Analog Value = 514 V = 2.50977 Amps = 0.05279

Analog Value = 515 V = 2.51465 Amps = 0.07918

about the analog value if i make an average value with for loop, things are getting messy and i get a very high amp value around 0.25 or more so i kept it without analog average value.

so i need to know please why i am getting a higher amps results than the one calculated by hand ?

here are the codes

/*
Current Sensor Measurement ASC712
*/

const int AnalogPin = A0;

//Set scale factor according to the Allegro ASC712 datasheet
int mVperAmp = 185; //mV

/* Scale Factor
5A bi-directional = 185
*/

//Set Offset in mV
int ACSoffset = 2500; //2.5 Volts for 0A current

int ReadAnalogValue = 0;
double Voltage = 0; //doiuble has more precision than the float but it is a decimal point
double Amps = 0;

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

void loop(){
  ReadAnalogValue = analogRead(AnalogPin); 
  Voltage = (ReadAnalogValue / 1024.0) * 5000; //Step for each bit is 5000 mV divided by 1024.0
  Amps = ((Voltage - ACSoffset) / mVperAmp);
  Voltage = Voltage/1000; //Conversion from mV to Volts
  
  Serial.print("Analog Value = " ); // shows pre-scaled value 
  Serial.println(ReadAnalogValue); 
  Serial.print("mV = "); // shows the voltage measured 
  Serial.println(Voltage,5); // the '3' after voltage allows you to display 3 digits after decimal point
  Serial.print("Amps = "); // shows the voltage measured 
  Serial.println(Amps,5); // the '3' after voltage allows you to display 3 digits after decimal point
  Serial.println(" ");
  delay(2500); 
}