Using Arduino to read the ACS712 current sensor and the required mathematics to get an output in Amps.
Warning: contains algebra
Warning: contains algebra
Good morning all…
Youtuber, shed dweller, solar charge controller aficionado
This site uses Akismet to reduce spam. Learn how your comment data is processed.
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.
VoltVal = Map(0,1024,-5,5);
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.
can i use it to calculate both current and voltages?
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.
Ohh three or zero three
use map to map 0-5v to 0-1023 steps…. too easy!
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
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
How do you prevent a negative reading on current sensor? Force it to display Zero if the reading goes below zero?
5v/1024=.0048828125mv per step, 2v/.0048828125= 409.6 is about where you should see 2v
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!!
can any one please give me correct code. i will be really happy. I am using ACS712 20 A and 30 Amp version.
nice vid! Thanks! Can you safely measure AC current at the same way with the ACS712?