ACS715 + Arduino help

Support for Pololu products that are not covered by one of the above categories.

ACS715 + Arduino help

Postby KK4HFJ » Mon Apr 30, 2012 11:41 am

I've written a simple sketch to retrieve the amount of current flowing through the ACS715. Difficulties are the apparent negative readings for milliamps when low current is flowing, and non zero output when no current is flowing. Serial monitor capture is attached.

acs715 output.PNG
output at low current (radio in RX mode)
acs715 output.PNG (56.7 KiB) Viewed 16393 times


Code: Select all Expand
KK4HFJ
 
Posts: 9
Joined: Mon Apr 30, 2012 11:23 am

Re: ACS715 + Arduino help

Postby DavidEGrayson » Mon Apr 30, 2012 12:29 pm

Hello, KK4HFJ.

We spoke on the phone earlier. Your formula is correct, and it would work in a more Math-friendly environment such as Excel or Mathematica:

Code: Select all Expand


However, there are many quirks about arithmetic in C/C++ that will cause you problems.

When you divide an integer by an integer in C or C++, the result will actually be an integer, rounded down. So "(5000/1024)" is actually 4, which is way off from the value of 4.8828125 that you wanted. Some people resort to floating point arithmetic to fix this, but floating point arithmetic is very expensive on an AVR. A simpler solution is to multiply sensorValue by 5000 and then divide the product by 1024. I think you should cast to "long" (32-bit int) to make sure no overflows happen in the multiplication. So now we have:

Code: Select all Expand


That could should work for you, but because you wrote a floating point literal "0.133", your program will still be doing floating point arithmetic. An easy way to fix this is to change "/0.133" to " * 1000 / 133 ":

Code: Select all Expand


You can point out to your readers that "((long)sensorValue * 5000 / 1024)" has a physical meaning on its own; it's the voltage on the sensor's output in millivolts.

--David
DavidEGrayson
Pololu Employee
 
Posts: 2717
Joined: Thu Apr 03, 2008 12:30 pm
Location: Las Vegas, NV

Re: ACS715 + Arduino help

Postby KK4HFJ » Mon Apr 30, 2012 1:06 pm

That did the trick!

acs715 output.PNG
The higher number is when the radio is in TX, the lower number is the current consumption in receive.
acs715 output.PNG (26.99 KiB) Viewed 16379 times
KK4HFJ
 
Posts: 9
Joined: Mon Apr 30, 2012 11:23 am

Re: ACS715 + Arduino help

Postby KK4HFJ » Mon Apr 30, 2012 1:50 pm

I'm adding volts, watts, amp hours and watt hours next. See http://arduinotronics.blogspot.com/2012/04/monitoring-power-consumption-with.html as the project progresses. I'm a ham radio operator in EmComms (Emergency Communications), and this battery capacity meter will allow us to keep tabs on power consumption and battery capacity in a grid down scenario.

Steve Spence
KK4HFJ
http://arduinotronics.blogspot.com/
KK4HFJ
 
Posts: 9
Joined: Mon Apr 30, 2012 11:23 am

Re: ACS715 + Arduino help

Postby KK4HFJ » Thu May 10, 2012 4:55 pm

The project is finished, and now includes a LCD display. I'm monitoring volts, amps, watts, hours, amp hours, and watt hours. http://www.instructables.com/id/DIY-Amp-Hour-Meter-Arduino/

Image
KK4HFJ
 
Posts: 9
Joined: Mon Apr 30, 2012 11:23 am

Re: ACS715 + Arduino help

Postby KK4HFJ » Thu May 10, 2012 5:23 pm

Replacing the acs715 with a bidirectional acs714, so I can track charging and discharging, and use this as a "gas gauge" for my batteries.
KK4HFJ
 
Posts: 9
Joined: Mon Apr 30, 2012 11:23 am

Re: ACS715 + Arduino help

Postby Ben » Thu May 10, 2012 10:15 pm

Hello.

Very cool! Thanks for sharing your project with us.

- Ben
User avatar
Ben
Pololu Employee
 
Posts: 3440
Joined: Mon Aug 28, 2006 1:05 pm
Location: Las Vegas, NV

Re: ACS715 + Arduino help

Postby KK4HFJ » Fri May 11, 2012 2:11 pm

The one thing that concerns me is the accuracy of the voltage divider, and it's constant "wandering". It's reading low, and wanders around constantly by a half a volt or so, like there's noise on the line. Any ideas?

Code: Select all Expand
Attachments
voltage-divider.png
voltage-divider.png (6.84 KiB) Viewed 16265 times
KK4HFJ
 
Posts: 9
Joined: Mon Apr 30, 2012 11:23 am

Re: ACS715 + Arduino help

Postby Ben » Fri May 11, 2012 4:31 pm

The ADC on an AVR can't very accurately measure signals with a high output impedance, and your voltage divider definitely counts as high-impedance. From the ATmega328P datasheet:

The ADC is optimized for analog signals with an output impedance of approximately 10 kΩ or less. If such a source is used, the sampling time will be negligible. If a source with higher impedance is used, the sampling time will depend on how long time the source needs to charge the S/H capacitor, with can vary widely. The user is recommended to only use low impedance sources with slowly varying signals, since this minimizes the required charge transfer to the S/H capacitor.


I think you'll have better results if you make your voltage divider resistors smaller, such as 10k and 5k, though this would waste 10 times more power (maybe 1.6 mW for your current configuration vs 17 mW for the new one).

- Ben
User avatar
Ben
Pololu Employee
 
Posts: 3440
Joined: Mon Aug 28, 2006 1:05 pm
Location: Las Vegas, NV

Re: ACS715 + Arduino help

Postby KK4HFJ » Fri May 11, 2012 8:37 pm

Excellent, thanks. Will update that and report back.
KK4HFJ
 
Posts: 9
Joined: Mon Apr 30, 2012 11:23 am

Re: ACS715 + Arduino help

Postby KK4HFJ » Sat May 12, 2012 2:44 am

I have 4 possible nominal battery bank voltages. Each battery bank has a higher possible charge voltage during certain charge cycles. I've called this max, and will prevent a voltage of over 5v being presented to the Arduino pin during all charge cycles including equalize.

Solving for R1

R1 = ((R2*Vin)/Vout)-R2

with a R2 of 5k ohms, I get the following values of R1 for 4 battery voltages:

nominal max R1 R2 Ratio
12 17 12 5 2.4
24 34 29 5 5.8
36 51 46 5 9.2
48 68 63 5 12.6

All resistances in k ohms.

If I solve for R2

R2 = R1 / (Vin/Vout - 1)

With an R1 of 12k ohms, I get the following values for the 4 battery voltages:

nominal max R1 R2 Ratio
12 17 12 5 2.40
24 34 12 2 6.00
36 51 12 1.3 9.23
48 68 12 0.9 13.33

All resistances in k ohms.

Solve for Vout to make sure Vout never exceeds 5v

Vout = (R2/(R1+R2))*Vin
KK4HFJ
 
Posts: 9
Joined: Mon Apr 30, 2012 11:23 am

Re: ACS715 + Arduino help

Postby KK4HFJ » Wed May 16, 2012 10:57 am

I'm adding a low voltage disconnect circuit with a hybrid relay (eliminates MOSFET heating and relay arcing) to protect the battery from excessive discharge. When sketch starts, the MOSFET is enabled, then the relay. Upon voltage drop below predermined value, the relay drops, then the MOSFET. Use a SSR for an AC Load. Full instructable at http://www.instructables.com/id/DIY-Amp-Hour-Meter-Arduino/.
Attachments
hybrid-relay.png
hybrid-relay.png (11.84 KiB) Viewed 16145 times
KK4HFJ
 
Posts: 9
Joined: Mon Apr 30, 2012 11:23 am

Re: ACS715 + Arduino help

Postby jwatte » Wed May 16, 2012 8:37 pm

If you have an input impedance problem, you can buffer the input signal with a single-ended opamp, such as an MCP601 or similar. Tie the output directly to the - terminal as well as the Arduino input pin, and the input to the + terminal.
User avatar
jwatte
 
Posts: 300
Joined: Tue May 15, 2012 6:56 pm


Return to Other Pololu Products

Who is online

Users browsing this forum: No registered users and 1 guest