Measuring Current (0-60amps) with Arduino build

I did some testing on my ebike, it appears that it is working correctly. I am using a 750w 24vdc motor, at start up it is 55 amps for split second then quickly ramps down to about 4 amps to turn the no load tire at full speed.

Any feedback, suggestions, bad code grammar?


// Current sensing program written by Steelmesh
// Using ACS756 hall effect current sensor
// 10k pulldown resistor on VOUT
// 0.1uf cap at VIN 5v from Arduino
// Hall Sensor pins: (1)VCC- 5v Arduino, (2)GND- Ground Arduino, (3)VIOUT- Analog Input Arduino, (4) from power source, (5) to load

int sensorMv = 0;
int pin3 = 0; //arduino analog pin 3, NOT pin 3 of the hall sensor
int sensorCal = 0; //initially calibrate the hall sensor
int sensorCalCount = 0; //used to stop cal after >0
int sensorOut = 0;
int maxamp = 0;

void setup(){

Serial.begin(9600);

}

void loop(){

if (sensorCalCount < 1) //first initial cal, assumes no motor current
{
delay(1000);
sensorMv = analogRead(0);
delay(2000); //just so I turn on serial monitor and be able to capture the serial.print writing the values for me
sensorCal = sensorMv; //calibrates the Hall Effect signal to become 0 point (no amps)
sensorCalCount = sensorCalCount + 1;
maxamp = (1023 - sensorCal) / (40 / 4.9); //conversion of ACS756 signal delta of 40mV-to-1 amp
Serial.print("Setup data sensorCal/maxamp ");
Serial.print(sensorCal);
Serial.print("/");
Serial.print(maxamp);
Serial.println();
}
else // start the real loop
{
sensorMv = analogRead(0);
sensorMv = constrain(sensorMv, sensorCal, 1023); //set sensorCal to min value
sensorMv = map(sensorMv, sensorCal, 1023, 0, maxamp); //set sensorCal to 0 amps, then 1023-sensorCal is range, divided by 40mV per amp or 8.16units per amp

pin3 = analogRead(3); //all the pin3 stuff is testing code, using a pot to mimic the sensor
pin3 = constrain(pin3, sensorCal, 1023);
pin3 = map(pin3, sensorCal, 1023, 0, maxamp);

sensorOut = analogRead(0) * 4.9; //quick conversion to mV from the sensor

Serial.print("sensor amps ");
Serial.print(sensorMv);
Serial.print(" ");
Serial.print("pot test amps ");
Serial.print(pin3);
Serial.print(" ");
Serial.print("sensorCal ");
Serial.print(sensorCal);
Serial.print(" ");
Serial.print("Sensor mV out: ");
Serial.print(sensorOut);

Serial.println();
delay(100);
}
}


is the pull-down resistor necessary? Will it act like a buffer for the signal, or should use a 1uf cap? I will find oscope hehe

Please use code tags when posting code.

You don't need the pulldown resistor, however you probably want to smooth the current reading using a resistor between the Hall sensor output and the Arduino pin, and a capacitor from the pin to ground. For example, 10K and 1uF.

Regarding your code, it is good practice to declare variables locally instead of globally when you are only using them in a single function, for example:

  int sensorMv = analogRead(0);
  sensorMv = constrain(sensorMv, sensorCal, 1023); //set sensorCal to min value
  sensorMv = map(sensorMv, sensorCal, 1023, 0, maxamp);   //set sensorCal to 0 amps, then 1023-sensorCal is range, divided by 40mV per amp or 8.16units per amp 
  
  int pin3 = analogRead(3); //all the pin3 stuff is testing code, using a pot to mimic the sensor
  pin3 = constrain(pin3, sensorCal, 1023); 
  pin3 = map(pin3, sensorCal, 1023, 0, maxamp); 
  
  int sensorOut = analogRead(0) * 4.9; //quick conversion to mV from the sensor

Variables maxamp and sensorCal do need to be declared globally because you need their values to persist between function calls.