1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

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

Discussion in 'Electronic Projects Design/Ideas/Reviews' started by abouabdelmajid, Aug 3, 2018.

  1. abouabdelmajid

    abouabdelmajid New Member

    Joined:
    Aug 20, 2017
    Messages:
    11
    Likes:
    0
    Hi everyone

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

    schéma.png
    i used this code

    Code (C):

    /*
    Measuring Current Using ACS712
    */

    const int analogIn = A0;
    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(){
     RawValue = analogRead(analogIn);
     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

    Somebody can help me please
     
  2. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,806
    Likes:
    435
    Location:
    Brisbane Australia
    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.
     
  3. ronsimpson

    ronsimpson Well-Known Member Most Helpful Member

    Joined:
    Apr 17, 2007
    Messages:
    8,082
    Likes:
    1,111
    Location:
    Loveland, CO USA
    ONLINE
    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?
     
  4. dave miyares

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    2
    Likes:
    -10


     
  5. schmitt trigger

    schmitt trigger Well-Known Member

    Joined:
    May 24, 2009
    Messages:
    1,128
    Likes:
    106
    Read the datasheet.
    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.
     
  6. rjenkinsgb

    rjenkinsgb Active Member

    Joined:
    Apr 29, 2018
    Messages:
    203
    Likes:
    26
    Location:
    Sheffield, England
    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: Aug 3, 2018
  7. Reloadron

    Reloadron Well-Known Member Most Helpful Member

    Joined:
    Dec 23, 2009
    Messages:
    6,909
    Likes:
    302
    Location:
    Cleveland, Ohio USA
    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
     
  8. dave miyares

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    2
    Likes:
    -10


     
  9. dr pepper

    dr pepper Well-Known Member Most Helpful Member

    Joined:
    Oct 6, 2008
    Messages:
    5,154
    Likes:
    307
    Location:
    North west UK
    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
     
    • Like Like x 1
  10. Nigel Goodwin

    Nigel Goodwin Super Moderator Most Helpful Member

    Joined:
    Nov 17, 2003
    Messages:
    39,815
    Likes:
    739
    Location:
    Derbyshire, UK
    ONLINE
    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.
     
    • Like Like x 1
  11. dr pepper

    dr pepper Well-Known Member Most Helpful Member

    Joined:
    Oct 6, 2008
    Messages:
    5,154
    Likes:
    307
    Location:
    North west UK
    Yes Ok C++.
    If you know C you can write code for a 'duino.
    I like the near zero resistance of the Ac712.
     
  12. Nigel Goodwin

    Nigel Goodwin Super Moderator Most Helpful Member

    Joined:
    Nov 17, 2003
    Messages:
    39,815
    Likes:
    739
    Location:
    Derbyshire, UK
    ONLINE
    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
     
  13. Reloadron

    Reloadron Well-Known Member Most Helpful Member

    Joined:
    Dec 23, 2009
    Messages:
    6,909
    Likes:
    302
    Location:
    Cleveland, Ohio USA
    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
     
  14. dr pepper

    dr pepper Well-Known Member Most Helpful Member

    Joined:
    Oct 6, 2008
    Messages:
    5,154
    Likes:
    307
    Location:
    North west UK
    '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.
     
  15. schmitt trigger

    schmitt trigger Well-Known Member

    Joined:
    May 24, 2009
    Messages:
    1,128
    Likes:
    106
    We all have that same terminal disease.
     
  16. Reloadron

    Reloadron Well-Known Member Most Helpful Member

    Joined:
    Dec 23, 2009
    Messages:
    6,909
    Likes:
    302
    Location:
    Cleveland, Ohio USA
    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
     
  17. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,806
    Likes:
    435
    Location:
    Brisbane Australia
    I've wasted so much money on the contents of my box. However, one day......

    Mike.
     
  18. dknguyen

    dknguyen Well-Known Member

    Joined:
    May 6, 2006
    Messages:
    7,716
    Likes:
    179
    I inventoried mine over the last 6 months. Must use. Too much money.
     
  19. dr pepper

    dr pepper Well-Known Member Most Helpful Member

    Joined:
    Oct 6, 2008
    Messages:
    5,154
    Likes:
    307
    Location:
    North west UK
    Beats spending cash on fags & booze.
     
  20. ClydeCrashKop

    ClydeCrashKop Well-Known Member

    Joined:
    Apr 28, 2005
    Messages:
    1,115
    Likes:
    202
    Location:
    Florida
    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. :happy:
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice