Bad data reading

Hi

I have a problem with the current sensor Low Current Sensor ACS723

My code written in arduino ide environment:

const int analogInPin = 14;

const int infoPin = 27;

// Number of samples to average the reading over

// Change this to make the reading smoother… but beware of buffer overflows!

const int avgSamples = 10;

int sensorValue = 0;

float sensitivity = 100.0 / 500.0; //100mA per 500mV = 0.2

float Vref = 2500; // Output voltage with no current: ~ 2500mV or 2.5V

float calculatecurrent;

void setup() {

// initialize serial communications at 9600 bps:

Serial.begin(115200);//, SERIAL_8N1, RX, TX);

pinMode(infoPin, OUTPUT);

}

void loop() {

// read the analog in value:

for (int i = 0; i < avgSamples; i++)

{

sensorValue += analogRead(analogInPin);

// wait 2 milliseconds before the next loop

// for the analog-to-digital converter to settle

// after the last reading:

delay(2);

}

sensorValue = sensorValue / avgSamples;

// The on-board ADC is 10-bits → 2^10 = 1024 → 5V / 1024 ~= 4.88mV

// The voltage is in millivolts

float voltage = 1.22 * sensorValue; //4.88 * sensorValue;

// This will calculate the actual current (in mA)

// Using the Vref and sensitivity settings you configure

float current = (voltage - Vref) * sensitivity;

// This is the raw sensor value, not very useful without some calculations

//Serial.print(sensorValue);

/*************************************************************************************

  • Step 1.)

  • Uncomment and run the following code to set up the baseline voltage

  • (the voltage with 0 current flowing through the device).

  • Make sure no current is flowing through the IP+ and IP- terminals during this part!

  • The output units are in millivolts. Use the Arduino IDE’s Tools->Serial Plotter

  • To see a plot of the output. Adjust the Vref potentiometer to set the reference

  • voltage. This allows the sensor to output positive and negative currents!

*************************************************************************************/

//Serial.println(voltage);

//Serial.print(“mV”);

/*************************************************************************************

  • Step 2.)

  • Keep running the same code as above to set up the sensitivity

  • (how many millivolts are output per Amp of current.

  • This time, use a known load current (measure this with a multimeter)

  • to give a constant output voltage. Adjust the sensitivity by turning the

  • gain potentiometer.

  • The sensitivity will be (known current)/(Vreading - Vref).

*************************************************************************************/

/*************************************************************************************

  • Step 3.)

  • Comment out the code used for the last two parts and uncomment the following code.

  • When you have performed the calibration steps above, make sure to change the

  • global variables “sensitivity” and “Vref” to what you have set up.

  • This next line of code will print out the calculated current from these parameters.

  • The output is in mA

*************************************************************************************/

//Serial.print(" current mA");

calculatecurrent = abs(current);

Serial.println(calculatecurrent);

//Serial.write(10);

if ((calculatecurrent < 40) || (calculatecurrent > 85)){

digitalWrite(infoPin, HIGH);

//Serial.write(10);

}

else{

digitalWrite(infoPin, LOW);

//Serial.write(0);

}

// – DO NOT UNCOMMENT BELOW THIS LINE –

Serial.print(“\n”);

// Reset the sensor value for the next reading

sensorValue = 0;

}

I use ESP32 v4 microcontroller to control a linear motor. Without load it has about 20 mA under load it increases to even approx. 100 mA.

The sensor shows a value of about 70 (not including noise). Under load the value changes but it is not the correct value how can I fix it?

Please use code tags when posting “</>” editor button. Also, please post a schematic diagram of your setup.

How do you know that the reading is incorrect?

The ESP32 ADC is highly nonlinear, and nearly useless, although it is possible that has been fixed in recent revisions of the chip.

I plugged the sensor in series with the linear motor (the way you plug in an ammeter).

The reading is incorrect because I measured with a multimeter.

On the Arduino Uno board it is similar

  • - Did you calibrate?
  • - Are you using the same code on the esp as you are on the uno?
  • - Keep metal away from the sensor, (especially anything magnetic) that will affect the reading.
  • - Do not use a metal screwdriver to adjust the pots.
  • - What voltage are you powering the sensor with? it needs 5 volts to work properly.
  • Hook a multi meter up to the output of the sensor and see what voltage reading you get for a known current. That will tell you if the sensor is working correctly.

    Yes the same code on esp and on uno works identically (uno has a 10 bit converter and esp has a 12 bit converter but I just changed the numbers in the code to read correctly)

    all screwdrivers are metal but i use insulated handle

    I supply 5 V as the manufacturer wrote from the power supply

    How to calibrate the sensor properly? I can only see when I adjust Vref it changes instead of 70 to, for example, 100 and not proportionally, and the Gain potentiometer does nothing

    Yes the same code on esp and on uno works identically (uno has a 10 bit converter and esp has a 12 bit converter but I just changed the numbers in the code to read correctly)

    It shouldn’t be the same because the code is written to produce values based on a 5 volt analog input. The Uno has a 5 volt input while the ESP has a 3.3 so you should see different values.

    all screwdrivers are metal but i use insulated handle

    Won’t work, no metal means no metal at all. Metal will skew your readings while you’re adjusting pots making it impossible to calibrate. an old plastic analog CRT TV alignment tool would be ideal but I’ve seen people shave plastic guitar pics down to work. Don’t turn the pots past their end stops or you will destroy them.

    How to calibrate the sensor properly? I can only see when I adjust Vref it changes instead of 70 to, for example, 100 and not proportionally, and the Gain potentiometer does nothing

    Calibrations directions are in the [[guide](https://learn.sparkfun.com/tutorials/current-sensor-breakout-acs723-hookup-guide?_ga=2.254154763.205848082.1668034384-595284128.1648020192), it's possible you may have turned the gain pot too far.

    I’d try again with a different (brand new) board and use a multimeter to confirm it’s working. Once it’s working then connect it to an Uno and get it working there before moving onto an ESP. I’d also have the current wires connected reversed so that you’re measuring negative current rather than positive since the ESP’s analog pin maxes out at 3.3 volts and the board already outputs 2.5 volts with no current detected. (this way, current produces a voltage less than 2.5 volts and your ESP can handle that just fine)](Current Sensor Breakout (ACS723) Hookup Guide - SparkFun Learn)