Using Arduino to read the ACS712 current sensor and the required mathematics to get an output in Amps.
Warning: contains algebra

By Julian

Youtuber, shed dweller, solar charge controller aficionado

14 thoughts on “Using the acs712 hall effect current sensor module part 2”
  1. Avataaar/Circle Created with python_avatars cmyanmar13 says:

    It makes only a tiny difference, but the correct scaling factor is 1024, not 1023. The ADC is already saturated to its maximum reading at just below 5 volts, specifically at 1023/1024*5 volts.

  2. Avataaar/Circle Created with python_avatars Damon Gardens says:

    VoltVal = Map(0,1024,-5,5);

  3. Avataaar/Circle Created with python_avatars Turbo Electric says:

    Just for the people watching this, there is a mistake here. The sensor only measures up to 5A not 27.03A. Most current sensors are calibrated to measure the current range by outputting in the -1v to +1v range regardless of power input.

  4. Avataaar/Circle Created with python_avatars احمد خلیل says:

    can i use it to calculate both current and voltages?

  5. Avataaar/Circle Created with python_avatars AlivisoBob says:

    Thank you for taking the time to share the formula and hardware I am ready to build this for my 18650 cells charging and discharging circuit.

  6. Avataaar/Circle Created with python_avatars Anbu says:

    Ohh three or zero three

  7. Avataaar/Circle Created with python_avatars caribbeanchild says:

    use map to map 0-5v to 0-1023 steps…. too easy!

  8. Avataaar/Circle Created with python_avatars John Harn says:

    You are correct in using 1023 in the divisor. While there are 1024 values between 0 and 1023, there are only 1023 steps. That gives about 5mV per step. Since the function to convert from Volts to Amperes is V = 185mV/A * I, the conversion from Volts to Amperes is I = V / 0.185 . Putting it all together, the counts per amp should be 37.85.
    A bit of Python code; not that you would want all of this in your C code:
    zero_offset = 514.0
    steps = 1023.0
    vref = 5.0
    v_per_amp = 0.185

    counts_per_amp = (steps * v_per_amp) / vref

    # to demonstrate 1 Amp of current
    counts = (zero_offset – 38)

    v = counts * vref / steps
    # m is for slope
    m = vref / (steps * v_per_amp)
    i = (zero_offset – counts) * m
    print 'slope = ', m
    print 'counts per amp = ', counts_per_amp

    print counts, ' counts = ', v, ' volts = ', i, ' amperes'

    Gives:

    slope = 0.0264193812581
    counts per amp = 37.851
    476.0 counts = 2.32649071359 volts = 1.00393648781 amperes

  9. Avataaar/Circle Created with python_avatars Dennis Olvany says:

    Good stuff. Math looks good: adjustedStepValue*ampsPerStep. Seems more efficient to calculate ampsPerStep outside the loop as maxVoltValue/maxStepValue/voltsPerAmp. You seem to indicate that maxVoltValue is equal to maxVoltValue/voltsPerAmp which is not the case since this is relative to maxVoltValue/2. I think it would be more accurate to say that maxVoltValue is equal to maxVoltValue/voltsPerAmp/2, but the total number of amps represented by the scale certainly is maxVoltValue/voltsPerAmp and the associated math is perfectly valid. 1023 is the only sensible divisor since maxVoltValue/1024*maxStepValue does not equal maxVoltValue. Of course this assumes that the step range is 0 to 1023 and not 0 to 1023 plus 1. Below is the algorithm and output in lua.

    maxVoltValue=5
    maxStepValue=1023
    voltsPerAmp=0.185
    ampsPerStep=maxVoltValue/maxStepValue/voltsPerAmp
    offset=maxStepValue/2
    for stepValue=312,712,50 do
    adjustedStepValue=stepValue-offset
    amps=adjustedStepValue*ampsPerStep
    print("stepValue="..stepValue..",amps="..amps)
    end

    stepValue=312,amps=-5.2706665609891
    stepValue=362,amps=-3.9496974980846
    stepValue=412,amps=-2.62872843518
    stepValue=462,amps=-1.3077593722755
    stepValue=512,amps=0.013209690629045
    stepValue=562,amps=1.3341787535336
    stepValue=612,amps=2.6551478164381
    stepValue=662,amps=3.9761168793427
    stepValue=712,amps=5.2970859422472

  10. Avataaar/Circle Created with python_avatars webslinger2011 says:

    How do you prevent a negative reading on current sensor? Force it to display Zero if the reading goes below zero?

  11. Avataaar/Circle Created with python_avatars Shaun Morgan says:

    5v/1024=.0048828125mv per step, 2v/.0048828125= 409.6 is about where you should see 2v

  12. Avataaar/Circle Created with python_avatars Jarrell Estes says:

    GREAT VIDEO . 6 PV solar panel Have reduced utility bill about 80% with GRID TIE no batteries. Switching loads with this sensor and Aderino . Direction of current indicates excess generation by solar panels. Great video!!

  13. Avataaar/Circle Created with python_avatars Saeed Ahmed says:

    can any one please give me correct code. i will be really happy. I am using ACS712 20 A and 30 Amp version.

  14. Avataaar/Circle Created with python_avatars Caius Citiriga says:

    nice vid! Thanks! Can you safely measure AC current at the same way with the ACS712?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.