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:
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
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?
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);
}
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:
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.
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:
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.
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.
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.
1024 - has to be 1023.
500 - this is zero offset, if Vin=5000, than x0.1=500, but 4600 or 4800 creates difference, measure and calibrate it
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!