Esp32 reading DC current with ACS712 20A

I have some trouble to read correct ampere value from the sensor.
The circuit has sensor output on ADC1_3* ( 39 ) and I have a car lamp of 12V 40W,
and with a multimeter I can read a current of 2.61A. If I go with the multimeter on pin 39 I can read a voltage of 2.766V with load and 2.502V with no load. I following same example and write the following code, but the result return me value between 138.15 and 141.08 . Where am I wrong ?

float getAnalogAmp()
{
  //Measuring Current Using ACS712

  int   sampling = 250;

  double mVperAmp = 100; // use 185 for 5A Module, 100 for 20A Module, and 66 for 30A Module
  float ACSoffset = 2470.345;

  double RawValue = 0;
  double V = 0;
  double Amps = 0;

  for(int i=0; i<sampling; i++)
    {
    RawValue += analogRead(ADC_AMP);
    delay(1);
    }
  
  RawValue /= sampling;
   
  V = (RawValue / 1023.0) * 5000; // Gets you mV
  Amps = ((V - ACSoffset) / mVperAmp);

  Serial.print( "Amps:");
  Serial.println( Amps );
  return Amps;

  
}

The ESP32 has a 12 bit A:D converter and the ESP32 is a 3V3 device.

void fReadCurrent( void * parameter )
{
  const TickType_t xFrequency = 1000; //delay for mS
  const float mVperAmp        = 185.0f;
  float    ADbits             = 4096.0f;
  float    ref_voltage        = 3.3f;
  float    mA                 = 0.0f;
  float    adcValue           = 0.0f;
  float    Voltage            = 0.0f;
  float    Power              = 0.0f;
  float    offSET             = 0.0f;
  int      printCount         = 0;
  uint64_t TimePastKalman     = esp_timer_get_time(); // used by the Kalman filter UpdateProcessNoise, time since last kalman calculation
  SimpleKalmanFilter KF_I( 1.0f, 1.0f, .01f );
  /*
     185mv/A = 5 AMP MODULE
     100mv/A = 20 amp module
     66mv/A = 30 amp module
  */
  String powerInfo = "";
  powerInfo.reserve( 150 );
  while ( !MQTTclient.connected() )
  {
    vTaskDelay( 250 );
  }
  TickType_t xLastWakeTime = xTaskGetTickCount();
  for (;;)
  {
    adc1_get_raw(ADC1_CHANNEL_3); // read once discard reading
    adcValue = ( (float)adc1_get_raw(ADC1_CHANNEL_3) );
    //log_i( "adcValue I = %f", adcValue );
    Voltage = ( (adcValue * ref_voltage) / ADbits ) + offSET; // Gets you mV
    mA = Voltage / mVperAmp; // get amps
    KF_I.setProcessNoise( (esp_timer_get_time() - TimePastKalman) / 1000000.0f ); //get time, in microsecods, since last readings
    mA = KF_I.updateEstimate( mA ); // apply simple Kalman filter
    TimePastKalman = esp_timer_get_time(); // time of update complete
    printCount++;
    if ( printCount == 60 )
    {
      xSemaphoreTake( sema_CalculatedVoltage, portMAX_DELAY);
      Power = CalculatedVoltage * mA;
      //log_i( "Voltage=%f mA=%f Power=%f", CalculatedVoltage, mA, Power );
      printCount = 0;
      powerInfo.concat( String(CalculatedVoltage, 2) );
      xSemaphoreGive( sema_CalculatedVoltage );
      powerInfo.concat( ",");
      powerInfo.concat( String(mA, 4) );
      powerInfo.concat( ",");
      powerInfo.concat( String(Power, 4) );
      xSemaphoreTake( sema_MQTT_KeepAlive, portMAX_DELAY );
      MQTTclient.publish( topicPower, powerInfo.c_str() );
      xSemaphoreGive( sema_MQTT_KeepAlive );
      powerInfo = "";
    }
    xLastWakeTime = xTaskGetTickCount();
    vTaskDelayUntil( &xLastWakeTime, xFrequency );
  }
  vTaskDelete( NULL );
} //void fReadCurrent( void * parameter )

Oopss...
Many thanks, I try to make the correction follow your code, and will let you know.
See later

It's working....
Thanks, have a nice day or night.

Hi atomino, currently working on with same acs712, can you please show how you connected the sensor to the ESP32? Thanks

Hi Idahowalker
May I ask how am I getting this error, I kindly ask for your assistance. Thanks

can you plz share the code