Interfacing ACS712 Current Sensor with Arduino

A small Tip to correctly measure the current using ACS712 Hall effect current sensor

When you want to measure currents, ACS712 Hall effect current sensor is the most ideal sensor comes into your mind. I wanted this current measuring functionality in one of my projects and it was the first time that I had to interface with ACS712 sensor. So, I just googled it and found a tutorial with a code. Hopefully, it worked well. My particular application was to make a constant current source using a switching mode power supply. Which means, I had to move this current measuring circuit to a different place and power it using a DC power supply instead of USB cable. Once I tested it with the original application I realized that the current sensor output is incorrect. Because it showed few hundreds of milliamps even without a load. 

The main reason for this is the analog read value you are getting when the current is zero, has changed due to some reasons. I implemented this circuit in a breadboard. Therefore, loose cables is one reason. However, if you are using this with a development board where you use headers to connect with the sensor, I am pretty sure that you may encounter the same problem. This can be corrected if you can update the analog read value for the zero current in your code (generally you get something around 512 as this value). For that you have to plug it to your computer and upload the corrected code again. But, it very painful since you may have to do it again and again when the sensor shows an incorrect reading. Let me tell you a very simple method to do the same thing easily.

It is non other than using a simple method to calibrate the sensor. With a push button, you can do this very easily and all you have to do is, pressing it whenever you  saw an incorrect reading for zero current. The complete wiring diagram is shown below.

Note that I have used the motor add the battery only to demonstrate a load. You can replace them with your own setup which you want to measure current. I used the 20A version of the ACS712 sensor. When you are implementing code, there is a parameter you want to edit if you are using 5A or 30A version of ACS172 sensor. Arduino code is shown below.


#include <LiquidCrystal.h>

#define button 10
#define sensor A0

LiquidCrystal lcd(7, 6, 5, 4, 3, 2);

bool calPressed = 0;
float calSenOutMean = 512;
long prevsample = 0;
long sampleSum = 0;
int sampleCount = 0;

float mvperdiv = 4.8828125; // 5000mV/1024

void setup() {

  pinMode (button,INPUT);
  
  Serial.begin(9600);
  lcd.begin(16, 2);
}

void loop() {

  // Detect button press for calibration
  if (digitalRead(button) == HIGH){
    calPressed = 1;
  }
  
  // When button is pressed...
  if (calPressed){
    long calLastSample = 0;
    long calSampleSum = 0;
    int calSampleCount = 0;

    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Calibrating...");
    
    while (calSampleCount < 1001){
    
      if (millis() > calLastSample + 1){
        int calSenOut = analogRead(sensor);
        calSampleSum += calSenOut;
        calSampleCount++;
        calLastSample = millis();
      }
    }

    calSenOutMean = calSampleSum/calSampleCount;
    calPressed = 0;

    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Done");
  }

  if (millis() > prevsample + 1){
    int senOut = analogRead(sensor);
    sampleSum += senOut;
    sampleCount++;
    prevsample = millis();
  }

  if (sampleCount == 1000){
    float sampleMean = sampleSum/sampleCount;
    float Amps = abs(calSenOutMean - sampleMean)*mvperdiv/100.0; // Change this 100.0 to 185.0 for 5A version and 66.0 for 30A version 

    // Ignore noisy measurements
    if (Amps <= 0.05){
      Amps = 0.00;
    }
    
    Serial.println(Amps);
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Current = ");
    lcd.setCursor(12,0);
    lcd.print(String(Amps));

    sampleSum = 0;
    sampleCount = 0;
  }
}

No comments:

Post a Comment

Popular Posts