Problem measuring current

mikroC, mikroBasic and mikroPascal PRO for Microchip’s 8-bit PIC MCUs.
Post Reply
Author
Message
Ahmed_shahid_jawad
Posts: 5
Joined: 01 May 2018 19:52

Problem measuring current

#1 Post by Ahmed_shahid_jawad » 21 Oct 2018 20:08

hello my frinds

I am a beginner in learning pic


I'm trying to measure the current ,, i used

1- sensor ACS712-20A
2- pic18f45k22
3- lcd 16-2
4- i used internal oscillator 16Mhz

The problem is reading is unstable ,,

i writted this code

Code: Select all

sbit LCD_RS at LATB2_bit;
sbit LCD_EN at LATB3_bit;
sbit LCD_D4 at LATB4_bit;
sbit LCD_D5 at LATB5_bit;
sbit LCD_D6 at LATB6_bit;
sbit LCD_D7 at LATB7_bit;

// Pin direction
sbit LCD_RS_Direction at TRISB2_bit;
sbit LCD_EN_Direction at TRISB3_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;



#define sensor           porta.f2
#define sensor_dir       trisa.f2

#define start            portb.f0
#define start_dir        trisb.f0

#define sw1              portc.f6
#define sw2              portc.f5
#define sw3              portc.f4
#define sw4              portd.f3
#define sw5              portd.f2

#define sw1_dir          trisc.f6
#define sw2_dir          trisc.f5
#define sw3_dir          trisc.f4
#define sw4_dir          trisd.f3
#define sw5_dir          trisd.f2

#define led1             latc.f3
#define led2             latd.f0
#define led3             latd.f1

#define led1_dir         trisc.f3
#define led2_dir         trisd.f0
#define led3_dir         trisd.f1



float read_data=0; char x=0; float Voltage=0; float Amps=0; float i=0;  float y=0;
char txt[15]; float read_sen=0;       float save[101];
void init()
{

 IRCF0_bit=1;
 IRCF1_bit=1;
 IRCF2_bit=1;
 
 ANSELA=4;
 ANSELB=0;
 ANSELC=0;
 ANSELD=0;
 ANSELE=0;
 
 LCD_INIT();
 LCD_CMD(12);
 LCD_CMD(1);
 
 sw1_dir=1;
 sw2_dir=1;
 sw3_dir=1;
 sw4_dir=1;
 sw5_dir=1;
 
 led1_dir=0;
 led2_dir=0;
 led3_dir=0;
 
 led1=1;
 led2=1;
 led3=1;
 
 sensor_dir=1;
 start_dir=1;
 adc_init();

}




unsigned int ACS712_20a()
{


  unsigned char i =0;

   read_data=0;
   read_sen=0;

      for(i=0;i<100;i++)
      {

    read_data+= ADC_Read(2);

      Delay_ms(5);
      }
      
      read_data/=100;
      
    if(read_data<=512)
    {
     read_data=0;
     }
    else
    {
     read_data= ((((read_data)*5)/1024)-2.5)/0.100;
    }

    return (read_data*1000);
}

void main() 
{
 init();

 
 
 while(1)
 {

 read_sen=ACS712_20a() ;

    

    
 lcd_out(1,1,"Current is=");

 floatToStr(read_sen,txt);
for(x=0;x<15;x++)
 {
  if(txt[x]!=' '){ lcd_chr_cp(txt[x]) ;}
 }
 

 
 
  Delay_ms(250);
 }
}
this design circuit

[img][IMG]https://s1.gulfupload.com/i/00053/17eficrelssi_t.jpg[/img]
[/img]

this design pcb

[img][IMG]https://s1.gulfupload.com/i/00053/wuw411sg3yd7_t.jpg[/img]
[/img]

For example, when measuring the 160mA , reading varies from 155 to 170 up and down

What is the cause of this problem ??

dangerous
Posts: 748
Joined: 08 Mar 2005 16:06
Location: Nottinghamshire, UK

Re: Problem measuring current

#2 Post by dangerous » 29 Oct 2018 17:00

Hello, I think this should be in the MikroC section, but as a MikroBasic user I have had this issue myself,and it is not compiler related but rather to do with the A-D on board the chip.

I think you have hit the A-D resolution limitation issue. At 10 bits the minimum resolution is 1/1024th of the reference voltage.

For any 10 bit converter, the voltage step is plus or minus 1 LSB, or 1/1024th of the reference voltage owing to quantization errors.

For a ASC712_ 20 converter with 100mV / A, fsd is only 2V at 20A which corresponds to a resolution about 0.019 A (or close to 20mA).

So the values you are reading are about 1 LSB jumps (155 + 20 = 175). You may be able to slug it slightly with a capacitor to ground from the input pin. That assumes you are using the 2.048 internal reference.

There is also the issue of input impedance on the A-D itself. The output of the ACS712 is quoted as 4.7K Ohms. The A-D load is not specified but the input current is as the storage capacitor charges. This will affect the reading you get. Add a buffer to the input if the accuracy if significantly out. Also drift in the reference will upset the accuracy.

To improve resolution , use an external higher resolution A-D (24 bits is much better), and use the closest reference voltage you can to the full-scale reading. If you only want to read relatively low currents, use the 5A version of the ASC712. that gives about 185mV / A.

The internal reference amplifier can be configured to amplify the 1.024V reference voltage by 1x, 2x or 4x, to produce the three possible voltage levels. With 1.024 the minimum resolution is (surprisingly) 1.0 mV and you will get close to that with the 5A version. Vo = typically 0.925V at 5A, so using the 1.024 ref would be a good option. As the internal reference amplifier is used so the resolution decreases to 2mV at 2.048v and 4 mV at 4.096mV.

For best accuracy, use an external reference. These can be found cheaply now with 0.5% or better accuracy.

I hope this explains the errors you are seeing.

Post Reply

Return to “PIC PRO Compilers”