A brief overview of Allegro ACS712 current sensor (Part 2)

In the first part of this discussion, the features of ACS712 device were briefly discussed. Now we will use that theory to implement the ACS712 sensor to make a simple DC current meter. The analog output voltage from the sensor is measured through an ADC channel of the PIC16F1847 microcontroller. A voltage to current conversion equation will be derived and implemented in the firmware of the PIC microcontroller and the actual load current will be displayed on a character LCD.

Measuring dc current with ACS712 sensor

Experimental circuit setup

We are going to setup a test experiment to demonstrate the use of ACS712 to measure a DC current. I am using an ACS712-05B breakout module (you can find them cheap on ebay) for this purpose. It has got a 1 nF filter capacitor connected between pin 6 and ground, a 100 nF decoupling capacitor between power supply lines, and a power on LED soldered on the board. The power supply and output lines are accessible through header pins on one side, whereas, the current terminals are connected to a 2-pin terminal block on the opposite side, as shown below.

ACS712-05 current sensor module

The experimental circuit diagram of the DC current meter is shown below. A 2.7 ? (rated 2 Watt) resistor is connected in series with the current terminals and a varying dc voltage is applied to vary the current through the resistor and the current path. The output of the sensor module goes to AN0 (pin 17) ADC channel of the PIC16F1847 microcontroller. A 16×2 character LCD is used to display the measured current output.

Circuit diagram (Click to enlarge it)

I am using my PIC16F1847 breadboard module along with the Experimenter’s I/O board to demonstrate this experiment.

Sensor setup is done on a breadboard

Complete setup

The microcontroller uses the supply voltage (+5V) as reference for A/D conversion. The digitized sensor output is processed through software to convert it to the actual current value. The mathematics involved in the process is described in the white board below.

Mathematics involved in ADC conversion

Important note: The calculations shown above considered supply voltage Vcc = Vref = 5.0 V. Interestingly, the final equation relating I and Count remains the same even the power supply fluctuates. For example, suppose Vcc fluctuates and becomes 4.0 V. Then, the sensitivity of the ACS712-05B also changes to 0.185 x 4/5 = 0.148 mV. If you repeat the above calculations with Vcc = Vref = 4.0 V and sensitivity = 0.148 mV, you will end up with the same equation for I and Count. This was possible because of the ratiometric output of the ACS712 sensor.

The equation clearly tells that the current resolution for this setup is 26.4 mA, which corresponds to count 513, one count higher than the zero current offset. Therefore, this kind of arrangement is not suitable for measuring low current. You need an external Op-Amp circuit to enhance the resolution and be able to make more sensitive current measurement. If you are interested on that, you can visit Sparkfun’s ACS712 Low Current Sensor Breakout page that provides a circuit diagram for such an arrangement.

Software

The current-Count equation described above is implemented in the firmware of PIC16F1847 to compute current from digital count. The result is finally displayed on the LCD with a precision of two decimal places. The following software is written in C and compiled with the mikroC Pro for PIC compiler from mikroElektronika.

/*
 Project: Use of ACS712 sensor for DC current measurement
 MCU: PIC16F1847 running at 4.0 MHz using external resonator
      and MCLR is enabled
 Written by: Rajendra Bhatt (www.embedded-lab.com)
 Jan 22, 2012
*/
 
 // Define LCD connections
sbit LCD_RS at RB0_bit;
sbit LCD_EN at RB1_bit;
sbit LCD_D4 at RB4_bit;
sbit LCD_D5 at RB5_bit;
sbit LCD_D6 at RB6_bit;
sbit LCD_D7 at RB7_bit;
sbit LCD_RS_Direction at TRISB0_bit;
sbit LCD_EN_Direction at TRISB1_bit;
sbit LCD_D4_Direction at TRISB4_bit;
sbit LCD_D5_Direction at TRISB5_bit;
sbit LCD_D6_Direction at TRISB6_bit;
sbit LCD_D7_Direction at TRISB7_bit;
// End LCD module connections
 
void Display(unsigned int num){
 char temp[] = "I = 0.00 Amp";
 temp[4]   = num/1000 + 48;
 temp[6]   = (num/100)%10 + 48;
 temp[7]   = (num/10)%10  + 48;
 LCD_Out(2, 3, temp);
}
 
char message[]  = "ACS712-05 Sensor";
unsigned int ADC_Value, Factor;
unsigned long temp;
 
main(){
  ANSELA = 0x01;        // PORTA.0 is analog
  TRISA  = 0b00100001;  // RA5 and RA0 are inputs
  ANSELB = 0x00;        // PORTB pins are all digital
  TRISB  = 0x00;        // PORTB pins are all output
 
  Lcd_Init();
  Lcd_Cmd(_LCD_CLEAR);             // CLEAR display
  Lcd_Cmd(_LCD_CURSOR_OFF);        // Cursor off
  Lcd_Out(1,1,message);
  Factor = 264;       // To conver Count into current
 
  do{
    // Read multiple samples for better accuracy
    ADC_Value = ADC_Read(0);
    ADC_Value = ADC_Value + ADC_Read(0);
    ADC_Value = ADC_Value + ADC_Read(0);
    ADC_Value = ADC_Value/3;
    temp = (ADC_Value-512)*Factor ;
    ADC_Value = temp/10;
    Display(ADC_Value);
    Delay_ms(1000);
  }  while(1);
}

Download source and HEX files

Output

At zero input current, the ACS712 output should be ideally Vcc/2, which is equivalent to an ADC count of 512. A drift of 4.9 mV in the output voltage of ACS712 results in an offset of 1 LSB in the A/D conversion (for Vref = 5.0V, resolution of 10-bit ADC is 5V/1024 = 4.9 mV). Interestingly, one LSB is equivalent to 26mA of current for ACS712-05B. The drift of 4.9 mV or more in the zero current output voltage is considered likely to happen, which is reflected in the output shown on the LCD. I have seen the ACS712 output for zero input current fluctuating between 512 and 513 ADC counts. If it is 513, the displayed current would be 0.02 A. So it is always better to take multiple ADC measurements and then take their average.

Offset of 20 mA corresponding to 1 count

When the variable DC is set to 1.0 V, the current through the 2.7 ? resistor should be 370 mA. The measured value is 390 mA, which is off by 1 LSB.

Output for variable DC input equal to 1.0V

Output when DC input equal to 2.0 V

 

This concludes the discussion on the ACS712 current sensor. However, one concern is still not addressed. How would you measure an AC current with the ACS712 sensor? Keep in mind that the ACS712 sensor provides an instantaneous output corresponding to the current flowing through the conduction terminals. If the current flow is in positive direction (from pins 1 and 2 to pins 3 and 4), the sensitivity of the device is positive, and the ACS712 output voltage rises above Vcc/2. But if the current changes its direction, the sensitivity will be negative and the output of the ACS712 decreases below Vcc/2. This means, for an AC current, the 10-bit ADC output measured by the microcontroller oscillates about 512 counts. Therefore, the microcontroller needs to sample the sensor output fast enough so that the RMS value of the current can be computed from them.

References

ACS712 datasheet

ACS712 FAQ from Allegro website

Related Posts

37 comments

Leave a Reply

Your email address will not be published. Required fields are marked *