# Measure analog value in MCC

#### Meidling

Joined Dec 15, 2020
4
Hello,

my name is Niklas and I just have a problem with the analog measurement of a value. I use a PIC 16F1827 and program in MCC. I would like to perform a current measurement via an ACS712.

My first problem is that I need to measure about 300 mA. Therefore I have to change Vref+ and Vref- of the ADC to get better values. I put Vref- on an external pin that I supply with 2 V.
I supply Vref+ with the Fixed Voltage Reference, so that I don't have to occupy another pin.
I am not sure if my settings are correct.

My second problem is the code of the analog measurement. I have now written this code:

C:
uint8_t Sensor_Messung(adc_channel_t addr_pin){

}

void main(void)
{
// initialize the device
SYSTEM_Initialize();

while (1)
{
if(Sensor_Messung(ACS3) >= 400)LED1 = HIGH;        // Just an example
else LED2 = HIGH;
}
}
So I can call one of my analog pins at any time. Can anyone tell me if this code is correct?

I attached 2 files.

Moderators note : please use code tags for pices of code

#### Attachments

• 10 KB Views: 2
• 6.9 KB Views: 2

#### ronsimpson

Joined Oct 7, 2019
1,121
If you leave the ADC measuring from 0 to 5V and the ACS712 outputs 1/2 supply up (or down) then you will loose 1 bit of resolution. So your 8 or 10 bit ADC will only give you 7 or 9 bits. Is that good enough? Will save a pin. Making 2V and adding a pin is a source of error and used hardware. Doing this in software is simple.
external pin that I supply with 2 V.
I think you could use 1/2 supply on that pin.

#### Meidling

Joined Dec 15, 2020
4
If you leave the ADC measuring from 0 to 5V and the ACS712 outputs 1/2 supply up (or down) then you will loose 1 bit of resolution. So your 8 or 10 bit ADC will only give you 7 or 9 bits. Is that good enough? Will save a pin. Making 2V and adding a pin is a source of error and used hardware. Doing this in software is simple.

I think you could use 1/2 supply on that pin.
I have solved the problem with the measurement. I only have to use ADC_GetConversion(ACS3). After that I receive a value. Are my settings for the ADC Vref+ and Vref- correct?

#### magicChristian

Joined Nov 20, 2016
24
Hello Meidling, first you should know that it is difficult to measure 300 mA with ACS712. There are different variants and we hope you have the version ACS712-05 with a range of +- 5A. The output valtage is Vcc/2 +- i * s, s is the sensitivity with 185 mV/A. with 300 mA and 5V supply 2,5 +- 0,055 V. Because the Zero output is not exact you have to measure the output with no current and later the output with actual current. i = (Vx - Vo)/ s. With 10 bit ADC you have a resolution in current of 30 mA/digit. In order to be mor precise you have to use an op amp to subtract the zero voltage or to use one of currenr sensor amps like
TI INA280 or similar but with no galvanic isolation.