Help with ACS715 current sensor

Hello so I am having some trouble with the ACS715 current sensor. I followed this tutorial here and it does not seem to be working. The current should be reading something like .123 A but instead I am getting +-10... which obviously isn't close.

The exact code I have is

int get_amps(int pinin){
    int read_a;
    read_a = (int)(((((long)analogRead(pinin)*Vin)/1024)-500)*1000/133);
    }

Then in matlab where I am sending all the data I divide it by 1000 to get A.

Back in the arduino code I am calling this function like:

read_amps[0]=(get_amps(6)+2*read_amps[0])/3;
read_amps[1]=(get_amps(7)+2*read_amps[1])/3;
read_amps[2]=(get_amps(8)+2*read_amps[2])/3;
read_amps[3]=(get_amps(9)+2*read_amps[3])/3;
read_amps[4]=(get_amps(10)+2*read_amps[4])/3;

Averaging the latest values. I can't see anything wrong, maybe the breakout board is bad?

If anyone could help that would be greatly appreciated!

read_a = (int)((((long)analogRead(pinin)*Vin)/1024)-500)*1000/133);

Count the opening brackets.
count the closing brackets.
Leo..

Sorry, I did not copy and paste the code I just missed it retyping it. The code is on a different computer that is not connected to the internet. But there are the right amount of parenthesis in the actual code. Otherwise it wouldn't have complied to give me the wrong answer, but thank you!

What is the board? Where did you get 6, - 10 inputs? In referenced code analog pin passed A0, digital number w/o 'A' could be interpreted as digital pin

I would add a few serial.prints in the code and think in A/D values.
Then do the calculations on a calculator, and see if I get the same results.

The -500 in the maths line. Is that the "zero current" offset.
It might be 511 or 512.
Leo..

Magician:
What is the board? Where did you get 6, - 10 inputs? In referenced code analog pin passed A0, digital number w/o 'A' could be interpreted as digital pin

Micro, analogRead() doesnt assume its an analog pin? It seems to get the same raw number either way

Wawa:
I would add a few serial.prints in the code and think in A/D values.
Then do the calculations on a calculator, and see if I get the same results.

The -500 in the maths line. Is that the "zero current" offset.
It might be 511 or 512.
Leo..

That is what is looks like but changing it to 512 doesn't seem to change it that much as it still bounce from -3 to 10A when it should be around .120 A. When it does not have any current flowing it reads between 510-512 but when current is flowing it reads 513-514.

I'm starting to think the breakout board may be bad, but it is the only one I have to use. I am also wondering if it just cannot read the amperage that low?

https://www.arduino.cc/en/Main/ArduinoBoardMicro

A0 to A5 are labelled directly on the pins and the other ones that you can access in code using the constants from A6
trough A11

I don't have a micro to try, but it's not too difficult for you to change 6 to A6 and try, isn't it?

Magician:
https://www.arduino.cc/en/Main/ArduinoBoardMicro

I don't have a micro to try, but it's not too difficult for you to change 6 to A6 and try, isn't it?

No its not, I changed 8 to A8 and it did not change anything as far as the results I am getting

I changed 8 to A8 and it did not change anything as far as the results I am getting

May be it didn't change, but if it's me, I 'd stay close to original posted-verified code and use A8 . In case, if there are more than one issue with a project, the point is to minimize probability of error, because if you change only one part at a time than restore it back and try to debug another likely weak part, you will never find the root.
Even better if you split project in a small parts, for example connect a pot instead of sensor and verify that you have appropriate response from analog inputs, than check math, data link, and last step a sensor

Magician:
May be it didn't change, but if it's me, I 'd stay close to original posted-verified code and use A8 . In case, if there are more than one issue with a project, the point is to minimize probability of error, because if you change only one part at a time than restore it back and try to debug another likely weak part, you will never find the root.
Even better if you split project in a small parts, for example connect a pot instead of sensor and verify that you have appropriate response from analog inputs, than check math, data link, and last step a sensor

So its okay with just using 8 instead of A8, I just tested it with a potentiometer like you suggested. I also found that the reason it was being so jumpy was because of my averaging? So now it is much more stable but it is still wrong. I am getting 14.44A when it should be around.12A. The raw data read is 512.

I am getting 14.44A when it should be around.12A. The raw data read is 512.

I run this code:

   for( int i = 0; i < 1024; i += 10 ) {
        int read_a;
        int analog_pinin = i;
        
        read_a = (int)(((((long)analog_pinin * 5000)/1024)-500)*1000/133);
          Serial.print(F("\n\tanalog: "));
          Serial.print( i, DEC);
          Serial.print(F("\tread_a: "));
          Serial.print( read_a, DEC);
    
        }

Output:

analog: 0	read_a: -3759
	analog: 10	read_a: -3398
	analog: 20	read_a: -3030
	analog: 30	read_a: -2661
	analog: 40	read_a: -2293
	analog: 50	read_a: -1924
	analog: 60	read_a: -1563
	analog: 70	read_a: -1195
	analog: 80	read_a: -827
	analog: 90	read_a: -458
	analog: 100	read_a: -90
	analog: 110	read_a: 278
	analog: 120	read_a: 639
	analog: 130	read_a: 1007
	analog: 140	read_a: 1375
	analog: 150	read_a: 1744
	analog: 160	read_a: 2112
	analog: 170	read_a: 2481
	analog: 180	read_a: 2842
	analog: 190	read_a: 3210
	analog: 200	read_a: 3578
	analog: 210	read_a: 3947
	analog: 220	read_a: 4315
	analog: 230	read_a: 4684
	analog: 240	read_a: 5045
	analog: 250	read_a: 5413
	analog: 260	read_a: 5781
	analog: 270	read_a: 6150
	analog: 280	read_a: 6518
	analog: 290	read_a: 6887
	analog: 300	read_a: 7248
	analog: 310	read_a: 7616
	analog: 320	read_a: 7984
	analog: 330	read_a: 8353
	analog: 340	read_a: 8721
	analog: 350	read_a: 9082
	analog: 360	read_a: 9451
	analog: 370	read_a: 9819
	analog: 380	read_a: 10187
	analog: 390	read_a: 10556
	analog: 400	read_a: 10924
	analog: 410	read_a: 11285
	analog: 420	read_a: 11654
	analog: 430	read_a: 12022
	analog: 440	read_a: 12390
	analog: 450	read_a: 12759
	analog: 460	read_a: 13127
	analog: 470	read_a: 13488
	analog: 480	read_a: 13857
	analog: 490	read_a: 14225
	analog: 500	read_a: 14593
	analog: 510	read_a: 14962
	analog: 520	read_a: 15330
	analog: 530	read_a: 15691
	analog: 540	read_a: 16060
	analog: 550	read_a: 16428
	analog: 560	read_a: 16796
	analog: 570	read_a: 17165
	analog: 580	read_a: 17533
	analog: 590	read_a: 17894
	analog: 600	read_a: 18263
	analog: 610	read_a: 18631
	analog: 620	read_a: 19000
	analog: 630	read_a: 19368
	analog: 640	read_a: 19736
	analog: 650	read_a: 20097
	analog: 660	read_a: 20466
	analog: 670	read_a: 20834
	analog: 680	read_a: 21203
	analog: 690	read_a: 21571
	analog: 700	read_a: 21932
	analog: 710	read_a: 22300
	analog: 720	read_a: 22669
	analog: 730	read_a: 23037
	analog: 740	read_a: 23406
	analog: 750	read_a: 23774
	analog: 760	read_a: 24135
	analog: 770	read_a: 24503
	analog: 780	read_a: 24872
	analog: 790	read_a: 25240
	analog: 800	read_a: 25609
	analog: 810	read_a: 25977
	analog: 820	read_a: 26338
	analog: 830	read_a: 26706
	analog: 840	read_a: 27075
	analog: 850	read_a: 27443
	analog: 860	read_a: 27812
	analog: 870	read_a: 28180
	analog: 880	read_a: 28541
	analog: 890	read_a: 28909
	analog: 900	read_a: 29278
	analog: 910	read_a: 29646
	analog: 920	read_a: 30015
	analog: 930	read_a: 30383
	analog: 940	read_a: 30744
	analog: 950	read_a: 31112
	analog: 960	read_a: 31481
	analog: 970	read_a: 31849
	analog: 980	read_a: 32218
	analog: 990	read_a: 32578
	analog: 1000	read_a: -32589
	analog: 1010	read_a: -32221
	analog: 1020	read_a: -31852

No wonder, you have 14A at 512. There are many flows in the code, you have to rewrite it or find another one. Author using integer math, that is not necessary, and generates a lot of trouble when used by beginners. Switch to float.
Your averaging code is also prone to overflow troubles, cast to long:

 read_amps[0]=(get_amps(6)+2*((long)read_amps[0]))/3;

or better to float, and save your time to track -32768 + 32767 limits

What is your Vin?

I take time to look over ACS data sheet:
http://www.allegromicro.com/~/Media/Files/Datasheets/ACS715-Datasheet.ashx
so you should, especially pay attention to:

Quiescent output voltage (V IOUT(Q) ). The output of the device
when the primary current is zero. For a unipolar supply voltage,
it nominally remains at V CC × 0.1 . Thus, V CC = 5 V translates
into V IOUT(Q) = 0.5 V. Variation in V IOUT(Q) can be attributed to
the resolution of the Allegro linear IC quiescent voltage trim and
thermal drift.
Electrical offset voltage (V OE ). The deviation of the device
output from its ideal quiescent value of V CC × 0.1 due to non-
magnetic causes. To convert this voltage to amperes, divide by the
device sensitivity, Sens.

Magician:
I run this code:

   for( int i = 0; i < 1024; i += 10 ) {

int read_a;
       int analog_pinin = i;
       
       read_a = (int)(((((long)analog_pinin * 5000)/1024)-500)*1000/133);
         Serial.print(F("\n\tanalog: "));
         Serial.print( i, DEC);
         Serial.print(F("\tread_a: "));
         Serial.print( read_a, DEC);
   
       }



Output:



analog: 0 read_a: -3759
analog: 10 read_a: -3398
analog: 20 read_a: -3030
analog: 30 read_a: -2661
analog: 40 read_a: -2293
analog: 50 read_a: -1924
analog: 60 read_a: -1563
analog: 70 read_a: -1195
analog: 80 read_a: -827
analog: 90 read_a: -458
analog: 100 read_a: -90
analog: 110 read_a: 278
analog: 120 read_a: 639
analog: 130 read_a: 1007
analog: 140 read_a: 1375
analog: 150 read_a: 1744
analog: 160 read_a: 2112
analog: 170 read_a: 2481
analog: 180 read_a: 2842
analog: 190 read_a: 3210
analog: 200 read_a: 3578
analog: 210 read_a: 3947
analog: 220 read_a: 4315
analog: 230 read_a: 4684
analog: 240 read_a: 5045
analog: 250 read_a: 5413
analog: 260 read_a: 5781
analog: 270 read_a: 6150
analog: 280 read_a: 6518
analog: 290 read_a: 6887
analog: 300 read_a: 7248
analog: 310 read_a: 7616
analog: 320 read_a: 7984
analog: 330 read_a: 8353
analog: 340 read_a: 8721
analog: 350 read_a: 9082
analog: 360 read_a: 9451
analog: 370 read_a: 9819
analog: 380 read_a: 10187
analog: 390 read_a: 10556
analog: 400 read_a: 10924
analog: 410 read_a: 11285
analog: 420 read_a: 11654
analog: 430 read_a: 12022
analog: 440 read_a: 12390
analog: 450 read_a: 12759
analog: 460 read_a: 13127
analog: 470 read_a: 13488
analog: 480 read_a: 13857
analog: 490 read_a: 14225
analog: 500 read_a: 14593
analog: 510 read_a: 14962
analog: 520 read_a: 15330
analog: 530 read_a: 15691
analog: 540 read_a: 16060
analog: 550 read_a: 16428
analog: 560 read_a: 16796
analog: 570 read_a: 17165
analog: 580 read_a: 17533
analog: 590 read_a: 17894
analog: 600 read_a: 18263
analog: 610 read_a: 18631
analog: 620 read_a: 19000
analog: 630 read_a: 19368
analog: 640 read_a: 19736
analog: 650 read_a: 20097
analog: 660 read_a: 20466
analog: 670 read_a: 20834
analog: 680 read_a: 21203
analog: 690 read_a: 21571
analog: 700 read_a: 21932
analog: 710 read_a: 22300
analog: 720 read_a: 22669
analog: 730 read_a: 23037
analog: 740 read_a: 23406
analog: 750 read_a: 23774
analog: 760 read_a: 24135
analog: 770 read_a: 24503
analog: 780 read_a: 24872
analog: 790 read_a: 25240
analog: 800 read_a: 25609
analog: 810 read_a: 25977
analog: 820 read_a: 26338
analog: 830 read_a: 26706
analog: 840 read_a: 27075
analog: 850 read_a: 27443
analog: 860 read_a: 27812
analog: 870 read_a: 28180
analog: 880 read_a: 28541
analog: 890 read_a: 28909
analog: 900 read_a: 29278
analog: 910 read_a: 29646
analog: 920 read_a: 30015
analog: 930 read_a: 30383
analog: 940 read_a: 30744
analog: 950 read_a: 31112
analog: 960 read_a: 31481
analog: 970 read_a: 31849
analog: 980 read_a: 32218
analog: 990 read_a: 32578
analog: 1000 read_a: -32589
analog: 1010 read_a: -32221
analog: 1020 read_a: -31852




No wonder, you have 14A at 512. There are many flows in the code, you have to rewrite it or find another one. Author using integer math, that is not necessary, and generates a lot of trouble when used by beginners. Switch to float.
Your averaging code is also prone to overflow troubles, cast to long:


read_amps[0]=(get_amps(6)+2*((long)read_amps[0]))/3;



or better to float, and save your time to track -32768 + 32767 limits

What is your Vin?

I take time to look over ACS data sheet:
http://www.allegromicro.com/~/Media/Files/Datasheets/ACS715-Datasheet.ashx
so you should, especially pay attention to:

I realized I needed to cast as long for averaging just a couple minutes ago, thank you for confirming it! I had taken it out though to get back to the bare bones of the code so I can figure out what is wrong. Vin is 5.008.

Based on your chart I should be getting a raw data reading of 120-130,so why would it be reading so high?

Based on your chart I should be getting a raw data reading of 120-130,so why would it be reading so high?

Read a quote from acs715.
see this:

it nominally remains at V CC × 0.1 . Thus, V CC = 5 V translates
into V IOUT(Q) = 0.5 V.

If you don't want to put a wagon in front of horse, you should measure a voltage at zero current, print out a raw adc-read, should be ~1023x0.1=102, than tweak your code to get "0" calculated value.

read_a = (int)(((((long)analog_pinin * 5000)/1024)-500)*1000/133);

Variables to tweak:

  1. Vin - I don't think you have exactly 5.000 V from USB, for example my arduino UNO getting 4.6 - 4.8 depends on cable I used.
  2. 1024 - has to be 1023.
  3. 500 - this is zero offset, if Vin=5000, than x0.1=500, but 4600 or 4800 creates difference, measure and calibrate it
  4. 133 - likely sensitivity 30A sensor, what is yours, you still didn't mention, 20 or 30?

Magician:
Read a quote from acs715.
see this: If you don't want to put a wagon in front of horse, you should measure a voltage at zero current, print out a raw adc-read, should be ~1023x0.1=102, than tweak your code to get "0" calculated value.

read_a = (int)(((((long)analog_pinin * 5000)/1024)-500)*1000/133);

Variables to tweak:

  1. Vin - I don't think you have exactly 5.000 V from USB, for example my arduino UNO getting 4.6 - 4.8 depends on cable I used.
  2. 1024 - has to be 1023.
  3. 500 - this is zero offset, if Vin=5000, than x0.1=500, but 4600 or 4800 creates difference, measure and calibrate it
  4. 133 - likely sensitivity 30A sensor, what is yours, you still didn't mention, 20 or 30?

I have mine running off a 12VDC power supply and I've check the 5v, its 5.008v. and mine is the 30 so it is not bidirectional, and has a sens range of 129-137.

Why 1023? The signal is quantized to 1024 bins wouldn't you divide by 1024?

Either way I think I got it working. For some reason I have to multiply by 2 to get the correct amps out, I don't know why but it has worked for the 2 circuits I ran. Thank you for all your help!