OpenMarine
Adapting motor.ino for a ACS712 current sensor - Printable Version

+- OpenMarine (https://forum.openmarine.net)
+-- Forum: Pypilot (https://forum.openmarine.net/forumdisplay.php?fid=17)
+--- Forum: General discussion (https://forum.openmarine.net/forumdisplay.php?fid=18)
+--- Thread: Adapting motor.ino for a ACS712 current sensor (/showthread.php?tid=4819)



Adapting motor.ino for a ACS712 current sensor - yoenn - 2023-08-04

Hello,

I started building up a pypilot controller with a MD13S I had in possession. The setup seems to be working fine, but since this controller is missing a current sensor, I added a VMA323/ACS712 sensor.

This sensor measures currents from -20A (~0V) to +20A (~5V) with a 0A corresponding to an offset of 2.4V approximately. It has a sensitivity of 100mV/A.

I connected its output to the A1 pin of the Arduino nano.

This is what I've done to motor.ino so far:

- #define VNH2SP30, DISABLE_TEMP_SENSE, DISABLE_VOLTAGE_SENSE, DISABLE_RUDDER_SENSE (I do not have sensors for this yet)

- in TakeAmps(), replace:
Code:
if(pwm_style == 2)
  return v * 9 / 34 / 16;

with:

Code:
if(pwm_style == 2)
  v = v * 5000 / 1024 / 16;
  if (v > 2400)
    return v - 2400;

  return 0;

- Since I need an analog measurement range from 2.4V to 3.4V (approximately), and as far as I understand, the current definition of adcref=_BV(REFS0)| _BV(REFS1) provides a range from 0 to 1.1V, I replaced it with adcref=BV(REFS0) everywhere in the code (two lines changed) to get a 0 to 5V range.


Now when I plot the current on openplotter's pypilot scope, I can see that the voltage offset is properly handled (0 when no current is flowing into the sensor; if I replace 2400 with a lower value in the code, I get a positive value for 0 amps).
But the value increase when current starts flowing in the motor is invalid:
- 100mA --> 1.3

- 1.7 A  --> 3

Direct voltage measurement on the ACS712 sensor shows that the sensor works fine.


It seems this increase is not linear, which is what I would have expected. Is there something else I could have missed or misunderstood in the file?

Thanks for the help!



RE: Adapting motor.ino for a ACS712 current sensor - PaddyB - 2023-08-04

sorry to be negative but those current sensors really aren't very good, plus the ADC of an arduino isn't really accurate enough for the very small voltage changes needed to go down to less than an Amp resolution. A better way imho is to use an ina219 & use a different shunt from ebay. The INA has an built in low noise amplifier which can measure the tiny voltage difference across a shunt.


RE: Adapting motor.ino for a ACS712 current sensor - seandepagnier - 2023-08-04

If you change to the 0-5v range, you need to ensure you use ratiometric mode then. This will fix the voltage calculations and you will use different resistors there too. This can be enabled by tying D4 and D5 to gnd.

You will need special logic to handle this sensor with 2.4v center point and maybe some logic to zero low enough readings.

If you can please use the original code in github and tie D4 and D5 to gnd, then what do you get for current readings? Then modify TakeAmps()