# Mesure DC curent of a 12V motor using Acs712 and arduino Nano

Status
Not open for further replies.

#### abouabdelmajid

##### New Member
Hi everyone

i am trying to mesure the curent of a 12v DC motor using Acs712 and arduino nano. i used this diagram

i used this code

C:
/*
Measuring Current Using ACS712
*/
int mVperAmp = 185; // use 100 for 20A Module and 66 for 30A Module
int RawValue= 0;
int ACSoffset = 2500;
double Voltage = 0;
double Amps = 0;

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

void loop(){
Voltage = (RawValue / 1024.0) * 5000; // Gets you mV
Amps = ((Voltage - ACSoffset) / mVperAmp);
Serial.print("Raw Value = " ); // shows pre-scaled value
Serial.print(RawValue);
Serial.print("\t mV = "); // shows the voltage measured
Serial.print(Voltage,3); // the '3' after voltage allows you to display 3 digits after decimal point
Serial.print("\t Amps = "); // shows the voltage measured
Serial.println(Amps,3); // the '3' after voltage allows you to display 3 digits after decimal point
delay(2500);
}
instead of having the right value 12.92ma (measured by my amperemeter) i am having wrong negative values

#### Pommie

##### Well-Known Member
What are all the other values printed? I.E raw etc. What reading do you get? Is it connected the right way around? Do you need to set the pin to input?

Mike.

#### ronsimpson

##### Well-Known Member
RawValue / 1024.0
I am not a software person so pardon my not understanding:
I think RawValue is a number from 0 to 255. Divide by 1024? Now what is the number range?

#### schmitt trigger

##### Well-Known Member
The ACS712's quiescent voltage (meaning zero current) is half its supply voltage.
Depending on the polarity of the current flow, this voltage increases decreases.

So first things first: measure with a DMM the actual ACS712 output voltage with and without current.

#### rjenkinsgb

##### Well-Known Member
Be wary mixing number types with C code, compilers may reduce the calculation (or part of it) to the simplest type.

eg. If the result of a calculation is float or double, all other values involved should be as well to avoid the chance of errors.
Add a decimal point to constants to force them to float, or decimal point and "l" (lower case L) to force a double.

Try changing these two lines:

Voltage = ((double)RawValue / 1024.0l) * 5000.0l; // Gets you mV
Amps = ((Voltage - (double)ACSoffset) / (double)mVperAmp);

It may make no difference or it could give a different result; it's worth a try.

Last edited:

##### Well-Known Member
It looks to me like you are using the 5 Amp version so the sensitivity is 185 mV/Amp. Using your Arduino you have a 10 bit analog input so 2^10=1024. You are also likely using a 5 volt reference so for your analog input channel 0 to 5 volts = 0 to 1023 bits or 1024 quantization levels. This is where your code covers:
Voltage = (RawValue / 1024.0) * 5000; // Gets you mV
So the incoming analog value now becomes 0 to 5000 mV or 0 to 5.0 Volts. You also have 5 volts divided down by 1024 bits = 5 / 1024 = 0.00488 or about a 4.88 mV and trying to measure 13 mA for your motor you will have poor resolution. This does not even take into account noise or A/D conversion error. Figure 1.0 Amp = about 185 mV so 100 mA = 18.5 mV and 10 mA = 1.85 mV. You mention about a 12 mA motor load?

Also as drawn it looks like you are measuring the high side current? I would try connecting your battery (supply) positive (+) directly to your motor positive (+) and your motor negative (-) to your ACS 712 IP + (Current Positive) and the ACS 712 IP - (current negative) to your supply negative (-). Your Arduino common should share your ACS 712 common and both powered by 5 volts.

Ron

#### dr pepper

##### Well-Known Member
I've just done this.
Rob has it right, only that sensor can measure + and - current, therefore when theres no current it outputs 2.5v.
Github has some arduino drivers for it, I dont know what processor your using however it is in C:
https://github.com/rkoptev/ACS712-arduino

#### Nigel Goodwin

##### Super Moderator
I've just done this.
Rob has it right, only that sensor can measure + and - current, therefore when theres no current it outputs 2.5v.
Github has some arduino drivers for it, I dont know what processor your using however it is in C:
https://github.com/rkoptev/ACS712-arduino
Well it's not C, it's C++ with an Arduino - but easily converted to C. It's not really a 'driver' either, just a simple library to make using it even easier, it's just a question of reading the analogue voltage out, subtracting the zero voltage offset (2.5V) and scaling accordingly.

I did buy some of these, but their resolution is fairly poor, though obviously great for higher currents. I must admit I do like the INA219, and you can set the range to whatever you like by changing the shunt resistor.

#### dr pepper

##### Well-Known Member
Yes Ok C++.
If you know C you can write code for a 'duino.
I like the near zero resistance of the Ac712.

#### Nigel Goodwin

##### Super Moderator
I like the near zero resistance of the Ac712.
Yes, that's a good thing - although you don't really drop much voltage with an INA219 either. And of course the AC712 is affected by magnetic fields

##### Well-Known Member
I do have one of these laying around which I was given. Never used it for anything but I see they have a new version out. The merit or advantage is the sensor is mounted on a breakout board and it includes an amplifier with a gain pot and Vref pot. Using a board with amplifier included should make scaling the unit easier.

Ron

#### dr pepper

##### Well-Known Member
'one of these' didnt work, but 'new version out' does.
Nifty having a means of calibration on the board.
Mine is the 20a version as my load goes to around 15a or so.
Its fairly well away from stray fields and gives me reasonable accuracy.
I have a 5a one too in my buy it now play with it later box.

#### schmitt trigger

##### Well-Known Member
I have a (insert anything here) in my buy it now play with it later box.
We all have that same terminal disease.

##### Well-Known Member
Actually I was involved in another forum where an Arduino came up. So I dug one out to play around with. Then I remembered years ago someone gave me a LCD display which I actually found and got it working with the Arduino. Since I retired, especially during winters when I am not out on my bike or the rifle range I find myself digging out all the junk I said someday I'll do something with this.

Ron

#### Pommie

##### Well-Known Member
I have a 5a one too in my buy it now play with it later box.
I've wasted so much money on the contents of my box. However, one day......

Mike.

#### dknguyen

##### Well-Known Member
I've wasted so much money on the contents of my box. However, one day......

Mike.
I inventoried mine over the last 6 months. Must use. Too much money.

#### dr pepper

##### Well-Known Member
Beats spending cash on fags & booze.

#### ClydeCrashKop

##### Well-Known Member
When you have a creative idea and find that you have everything you need to build it while it is fresh in your mind, it is all worth while. That happened to me just this weekend.