# Measuring signal of mV range using PIC 16F877A

Discussion in 'Embedded Systems and Microcontrollers' started by ranjith_asp, Jun 28, 2012.

1. ### ranjith_asp Thread Starter New Member

Dec 15, 2007
1
0
I am planning to connect a couple of hall effect sensors to PIC Microcontroller.

The output signal range of the sensor is in mV ranges.

I want to know how to amplify this in order to connect to the PIC Analog ports, as PIC 16F877A has the Analog input range of 0-5V.

My questions:
1. What is the best way to amplify my mV range signals to a range of 0-3 VDC?

2. Can anyone know the resolution of the PIC's analog port? Is it 0.1 VDC or even lesser?

2. ### t06afre AAC Fanatic!

May 11, 2009
5,939
1,227
Then take a look at FIGURE 11-1: A/D BLOCK DIAGRAM. If we assume that VREF- is set to VSS. Each bit in the conversion result. Will have a "weight" equal to
(VREF+)/(2^10)=(VREF+)/(1024). As an example If VREF+ is equal to 5 volt and the conversion register after conversion contain the value 124 decimal. The ADC reading is equal to (5/1024)*124=0.605 volt. Refer to TABLE 17-14 for the range of ADC input and other signals

3. ### ErnieM AAC Fanatic!

Apr 24, 2011
7,948
1,818
An analog to digital converter will take an analog voltage and convert this to a pure number of courts by the relation:

Counts = (Vin / Vref) * 1023

Note when Vin = Vref you get an output count of 1023, which is the largest number from a 10 bit binary quantity. Keep in mind any over voltage to Vin will also give the same number, meaning you cannot "see" an over range.

If you solve for Vin and set the counts to one you get:

Vin = (Counts * Vref) / 1023 = Vref / 1023

Say you use a 4.096 reference voltage (that comes from a popular precision voltage reference chip). you would have a resolution of:

Vin = 4.096 volts / 1023 = .0040 volts

So you get a resolution of very close to 4 mV per count over the full input range.

Using such a reference you should take the max sensor output and amplify it so you get close to the 4.096V reference voltage. I usually leave a little room up top for unexpected large inputs.

The accuracy is different from the resolution. The accuracy is dependent on the gain between your sensor and the micro's analog input: if the gain has an accuracy of ±1% then your overall accuracy also varies by that, plus any sensor accuracy. The amp's offset will also affect the accuracy.

Such a system can be calibrated by applying a known input and comparing to the resultant output. If you can get two widely spaced readings you can calculate the correction (calibration) by assuming a straight line and doing some simple math to compute the slope and intercept.

If your sensor and amp are linear this will calibrate the system fairly accurately.

4. ### t06afre AAC Fanatic!

May 11, 2009
5,939
1,227
I do not quite agree in what you write. A ten bit ADC can produce 1024 different readings zero inclued. To be very pedantic. If we go back to the example I gave in my first post. The resolution will be (VREF+)/(2^10). But the range of the ADC will be 0 to (VREF+)-((VREF+)/(2^10))

5. ### upand_at_them Senior Member

May 15, 2010
248
29
You can amplify it with an op-amp, such at the LM324. But you'll need to know the full range of the sensor to come up with the appropriate gain of the op-amp.

6. ### ErnieM AAC Fanatic!

Apr 24, 2011
7,948
1,818
And to be pedantic right back at 'cha...

For "1024 different readings zero included." the largest number output is 1023, since zero counts as a state.

While it is quite true 2^10 = 1024, the largest 10 bit number is 1023 or in binary 11,1111,1111 (<== ten bits, see?).

7. ### MrChips Moderator

Oct 2, 2009
16,900
5,211
What's your point? The resolution is still Vref/1024.

t06afre and BillO like this.
8. ### BillO Distinguished Member

Nov 24, 2008
994
139

+ 1!

Thanks Mr. Chips.

9. ### ErnieM AAC Fanatic!

Apr 24, 2011
7,948
1,818
How do you reach that conclusion? Can you show your work?

How about showing just showing what input gives you 1 count.

10. ### t06afre AAC Fanatic!

May 11, 2009
5,939
1,227
This is for me quite basic ADC theory. Why are you so stubborn on this matter.

File size:
1.2 MB
Views:
213
11. ### THE_RB AAC Fanatic!

Feb 11, 2008
5,432
1,312

But even after explaining it in 5 different ways I still don't think it sunk in.

If you want to give it another chance the thread is here;

12. ### ErnieM AAC Fanatic!

Apr 24, 2011
7,948
1,818
(Also see his Attached note linking to the "ABCs of ADCs Analog-to-Digital Converter Basics" by Nat Semi)

No wonder there is such confusion about this seemingly simple concept when such nonsense is printed. Referring to the Nat Semi app note, I agree with their definition of resolution on sheet 4 where they state "Resolution may also be defined as the size of the LSB (Least Significant Bit) or one count."

Hey, it's a definition, a priori true, but the usefulness of a definition lies in it's predictive ability. Also note no where in this app note do they even attempt to derive the resolution expression, it is merely stated, then they "play with the numbers" to get the desired result.

A theoretical (paper) converter is described as such:

Pay close attention to the conversion voltages on the right side coming out of the converter. They are centered on the inputs of 0.5, 1.5, 2.5, ... 6.5 and 7.5 volts. There is some slight of hand here to remove an extra volt from the input range: there is an extra half volt at the low end, and a missing half volt on the top. Thus the range of this converter is not 8 volts, but 7 volts, and if you compute the resolution as I recommend ( [voltage span]/[max count] ) you get the correct result of a resolution 1 volt, computed by:

[voltage span]/[max count]
[7 volts] / [7 counts] = 1 volt/count

While it is true:

8 volts / 2^3 counts
= 8 volts / 8 counts
= 1 volt/count

is the correct result it is completely unclear where the 8 volts term comes from. The circuit model doesn't use an 8 volt reference anywhere!

(Oh look, they draw 8V into the converter! But nowhere does 8V appear in the conversion outputs.)

The OP asked not about some theoretical converter, but about a Microchip PIC, and these have the min count centered about the point where the input equals the low reference voltage (typically zero), and the max count centered about the point where the input equals the high reference voltage. Applying some simple math to describe a linear straight line relationship yields the results I have stated.

In the other linked thread WBahn makes some excellent points (better then I could have made) in centering the results of the defining equation to minimize quantization errors. He keeps well in mind the goal here is given an A2D count how do we compute the most likely voltage that produced this resultant count.

The only point I am completely stubborn over is my complete refusal to entertain using the results from the "Journal of Computationally Efficient Methods for Predicting Inexact Results."

If any of you nay sayers would actually produce your work (as I have numerous times) I may help find either your error, or it may be correct the way you have defined your terms. One cannot comment on work not shown.

If anyone is unsure of the correct method, I only suggest to do the math yourself. You may learn something.

13. ### MrChips Moderator

Oct 2, 2009
16,900
5,211
I used to have the opinion that the resolution of an ADC or DAC was Vref/(2^n - 1)
but a discussion on AAC convinced me that I was wrong and that the resolution is in fact Vref/(2^n).

I will see if I can locate that thread.

Last edited: Jun 30, 2012
14. ### ErnieM AAC Fanatic!

Apr 24, 2011
7,948
1,818
Ah ha, someone who posts their work:
Note the referenced figure for the Microchip PIC A2D shows the bottom transition centered on ZERO LSB (in volts), which is used here as the Vref- voltage. Their graph is not quite true for the top transition as it does not correctly show the transition centered about Vref+. (Hey, these are persnickety details and data sheets get copied over and over when it's the same module being used. Best to check the A2D reference chapter on the PIC32's where they finally get this figure correct.)

Now let's look at that data range again:

3 = 3 to 4V or 3.5V nominal
2 = 2 to 3V or 2.5V nominal
1 = 1 to 2V or 1.5V nominal
0 = 0 to 1V or 0.5V nominal

The nominal value is the center of where a bit transitions, and if you base your estimates against these numbers you get the best predictability doing the reverse calculation of going from counts to applied voltage.

Such a physical example circuit inside a PIC would have a reference voltage of 0.5V and 3.0V.

In this example, we thus have a range of:

Max input - min input = Vref+ - Vref- = 3.5V - 0.5V = 3.0V

and thus a resolution of :

3.0V / 3 counts = 1 volt/count

Sep 7, 2009
2,803
596
16. ### BillO Distinguished Member

Nov 24, 2008
994
139
According to Wikipedia

The voltage resolution of an ADC is equal to its overall voltage measurement range divided by the number of discrete values:

where M is the ADC's resolution in bits and EFSR is the full scale voltage range (also called 'span'). EFSR is given by:

May 11, 2009
5,939
1,227
18. ### THE_RB AAC Fanatic!

Feb 11, 2008
5,432
1,312
Arrogant much?

I spent about 3 days explaining this to you in the other thread I linked to above, including numerous proofs and you're still going on that scaling should be /(n-1)?

Linear data scaling between two scales is done using inpututput, ie; output/input there is NO correct solution of output/(input-1) no matter how much you want it to be true.