ACS712 Current sensor

Hi people, it's me again...

So I have this ACS712 sensor.

I connect it in series with my load (100Ohm resistor) and try to measure using this code:

int pet = 5;
void setup() {
 pinMode(pet, OUTPUT);
 digitalWrite(pet, HIGH);
 Serial.begin(9600);
}
float volts = 0;
float raw = 0;
float amps = 0;
float Vcc = 5;

void loop() {
 float average = 0;

 for (int i = 0; i < 1000; i++) {
   raw = analogRead(1);
   volts = (raw * (Vcc/1023)); //we get volts
   amps = (volts - Vcc / 2) / 0.185;
   average = average + amps;
   delay(1);
   if (i == 500 ) {
     Serial.print(amps);
     Serial.println(" A");
     Serial.print(volts);
     Serial.println(" Volts");
   }
 }
 Serial.print("average:  ");
 Serial.println(average/1000, 4);
}

I get readings which are often negative and oscillate a lot. My average is also wrong.
I measure the current with a multimeter and it is around 35mA.

Any hints for a new beginner ?

Where is that 1000 ohm resistor ?
Are you measuring an AC current ?
What are the numbers that you get as result ?

You can write the schematic on a piece of paper and make a photo of it, that would help us a lot.

The scheme is like this (with a 100_Ohm resistor instead of an led);

The current is DC.

The values are like this:
-0.81 A
2.35 Volts
average: -0.8445
-0.81 A
2.35 Volts
average: -0.8451
-0.75 A
2.36 Volts
average: -0.8485
-0.89 A
2.34 Volts
average: -0.8476
-0.96 A
2.32 Volts
average: -0.8531
-0.86 A
2.34 Volts
average: -0.8521
-0.91 A
2.33 Volts
average: -0.8573

Where A is ampers, volts are analoginput Volts and average is the Amperes averaged on 1000 samples.

That is how it should be used, that is okay.

I can not find a bug in the your sketch, but the sketch is not displayed well.
Can you put the sketch between < code > and < / code > tags ? It is the button with the scroll and the "<>".
The 5A version of the ACS712 is 185mV per Amp. I think your calculation is okay.

The ACS712 can measure the current in both directions.
The output is VCC/2 when there is no current.
If the voltage of the Arduino would be a perfect 5.00V, the output of the ACS712 should be 2.50V.

I think you use the USB to power your Arduino board. That is why the 5V is lower. The average of 1000 samples should normally be very stable, but your values might not be stable because the USB 5V is not stable.

Could you remove the load, and measure the 5V and the output of the ACS712. Is the output half of the value of the 5V ?

Peter_n:
The ACS712 can measure the current in both directions.
The output is VCC/2 when there is no current.
If the voltage of the Arduino would be a perfect 5.00V, the output of the ACS712 should be 2.50V.

I think you use the USB to power your Arduino board. That is why the 5V is lower. The average of 1000 samples should normally be very stable, but your values might not be stable because the USB 5V is not stable.

Could you remove the load, and measure the 5V and the output of the ACS712. Is the output half of the value of the 5V ?

You are right, I'm using arduino as power source (via USB). I use two different pins (5v and pin 5) to try to get ''clean'' result.

The ACS712 sends mostly negative values (below 2.5V) even though the current is positive (measured with multimeter).

You mean I measure the ouput of ACS when I connect it directly between the pin5 (or pin 5V) and the ground ? Wouldn't that be a short circuit which could fry something ?

No, measure the output when there is no current. Just remove the "load". I mean the "load" in the picture, the light bulb or led or resistor of 100 ohm.

Without load, can you use a multimeter to measure the voltage of the 5V pin and the output of the ACS712.

Let's see if we can tune the output for 0 amps.

Ahh, you mean to measure Voltage.

It is ~ 4.42 on Pin 5 (it varies between 4.35 and 4.5).

This is the pin with which I connect the load and acs712 with.

Now I need to modify the scheme in accordance with this ? Do I also need to measure the 5V pin (power for ACS712), does that one also has an effect on my circuit ?

Pin 5 ? Which pin is that ? I hope you use the "5V" pin and not the digital pin 5.

You could you make a photo of your wiring and attach it to a post.

If the 5V pin is 4.42V, that is low. Do you have a cheap USB cable or a laptop ?
You could use a power supply of 7.5V to 9V to the DC barrel jack (if you have an Arduino Uno).

Actually yes, I used the digital pin5 (as output) to power the load circuit. If you look at the 1st picture (post#2) there are in effect 2 circuits:

I. One with the load and acs712 measuring leads
II. One for the ACS712

I measured the source voltage of the 1st circuit (digital pin5) I and it was around 4.42V.

So I need to connect everything to be powered from the same 5V (via breadboard) ?

Well, a breadboard has sometimes bad contacts, they can cause troubles.

Is there a reason why digital pin 5 (called 'pet' in the sketch ?) is used ?

Could you remove the load, to see if it is possible to measure 0 amps, and connect the sensor in a normal way:
Connect Arduino 5V pin to ACS712 module VCC.
Connect Arduino GND pin to ACS712 module GND.
Connect Arduino A1 to ACS712 module OUT.

Peter_n:
Well, a breadboard has sometimes bad contacts, they can cause troubles.

Is there a reason why digital pin 5 (called 'pet' in the sketch ?) is used ?

Could you remove the load, to see if it is possible to measure 0 amps, and connect the sensor in a normal way:
Connect Arduino 5V pin to ACS712 module VCC.
Connect Arduino GND pin to ACS712 module GND.
Connect Arduino A1 to ACS712 module OUT.

Thats why I used the 'pet' pin. If you do it like that, how do I measure any current (I need another source, the ''battery'' on the picture above, so I used 'pet' as my 'battery').

I connected it like you said (just the 3 pins you told me, so 0 -current should be on measuring leads ?), and here is what I get on analogeread:

-0.22 A
2.46 Volts
average_amps:  -0.1921
-0.09 A
2.48 Volts
average_amps:  -0.1941
-0.33 A
2.44 Volts
average_amps:  -0.1866
-0.04 A
2.49 Volts
average_amps:  -0.1839
-0.17 A
2.47 Volts
average_amps:  -0.1851
-0.41 A
2.42 Volts
average_amps:  -0.1879
-0.20 A
2.46 Volts
average_amps:  -0.1846
-0.30 A
2.44 Volts
average_amps:  -0.1821
-0.17 A
2.47 Volts
average_amps:  -0.1882
-0.20 A
2.46 Volts
average_amps:  -0.1875
-0.09 A
2.48 Volts
average_amps:  -0.1898
-0.17 A
2.47 Volts
average_amps:  -0.1872
-0.28 A
2.45 Volts
average_amps:  -0.1840
-0.17 A
2.47 Volts
average_amps:  -0.1867
-0.09 A
2.48 Volts
average_amps:  -0.1906
-0.28 A
2.45 Volts
average_amps:  -0.1915
-0.33 A
2.44 Volts
average_amps:  -0.1850
-0.17 A
2.47 Volts
average_amps:  -0.1897
-0.12 A
2.48 Volts
average_amps:  -0.1858
-0.25 A
2.45 Volts
average_amps:  -0.1925
-0.09 A
2.48 Volts
average_amps:  -0.1910
-0.04 A
2.49 Volts
average_amps:  -0.1895

It doesn't measure the 2.5V as 0, (as the 5V pin doesn't give the full 5V, with a multimeter I get around 4.55, 4.6V).

No that I measure it, my 5V pin oscillates from 3 to 5 ,quite big.

Could it be I fried it (I have missconnected somethings yesterday) ?

How do I know the difference between a fried 5V pin and maybe just a low power ?

P.S
It isn't fried, I just tried it with an external power supply and I get a solid 5V on 5V pin.

Perhaps something is not connected right, using the USB power should not cause oscillation.

I'm sorry, but I still no idea how you have connected it. I also don't have you sketch between code tags, so I can not try your sketch.
What is your goal ? To measure the output current of digital pin ?

A value of -0.19A for a 5A range is 4%. That is getting closer, but still not good enough.

I have temporarly given up on this project (and Arduino), some private stuff has risen.

Just wanted to say thanx for help Peter

Sorry to hear that. I hope it is good private stuff and not bad private stuff. Best wishes to you.