[PIC] Analog To Digital Conversion

Status
Not open for further replies.

KhaledOsmani

Full Member level 6
Joined
May 4, 2014
Messages
384
Helped
1
Reputation
2
Reaction score
1
Trophy points
18
Visit site
Activity points
3,933
It Looks like I have a misunderstanding in ADC process inside a PIC microcontroller.

If I want to read an Analog value (voltage) from a sensor, and convert it to decimal:

Assume an LM35DZ sensor, is powered up, and connected to a PIC circuit, where its output is connected to PORTA.0

It either has an analog output of 0 to 5V.

The ADC here is of 10bits.

So in decimal it would be from 0 to 1024 (2^10)

Assume that the conversed decimal value of this input (RA0) is put into temporary variable in a file register, labeled X for example,,

What I saw on the internet was that this X must be multiplied by the factor of 0.4887

This constant is got from 5000/1023, then it said that the actual value must be divided by 10, so 500/1023 = 0.4887
because every 10mV = 1 degree, so if we divide the above answers by 10 we will have the temperature in degree’s Celsius.

So TEMP = X * 0.4887

PROBLEM STATEMENT: I am not receiving an accurate output on the LCD, about the temperature, I also am not noticing any changes in the output, when presenting the sensor to extreme temperature fluctuations, i.e bringing ice boxes near to it, or closing a lighter next to it.

What are my wrong concepts about this title (ADC)? What are the issues, and How I can have dynamic representation of accurate temperature changes, when fronting the LM35DZ circuit to different temperature conditions?

Is the formulae of TEMP correct?
 

Hi,

did you checked the output of temperature sensor via multimeter or oscilloscope? did it changed when you varied the temperature. if this is working post your code or may be configuration of ADC here so that the members have a look at where you are going wrong.

Enjoy!
 

Hello,

Yes I did that check, I supplied the sensor with a voltage source (9V, it operates from 4 to 30V).
I checked the output, it is 0.20.
I had put an ice box near it, the output dropped from 0.20 to 0.19, then 0.18 then 0.17 and so on.
I had lighten fire near it (controllably), so the output increased from 0.20 to 0.21 to 0.22 and so on, but not at a fast level, perhaps of the surface connected to the sensor.

does the normal output of 0.20V means that the temp is 20C? Here now it is 20C as seen on mobile and as feeling, lol.
So the sensor is working efficiently.

As it was stated, the sensor is connected to PORTA , pin 0:


Code ASM - [expand]
1
2
3
4
5
6
7
8
BSF ADCON2,ADFM
        MOVLW 0x01
        MOVWF R0L
        CALL A001
        MOVF ADRESL,W
        MOVWF 0x019
        MOVF ADRESH,W
        MOVWF 0x01A



This was used to save the value of the analog signal (Voltage) entering in PORTA.0 and convert it into decimal ,
 
Last edited by a moderator:

A few points to consider:

1. The 5000/1023 is not correct, the formula is "Vref/number of possible ADC steps". Vref might be 5V but you can choose any reasonable value you want if you configure the PIC to use an external reference. If you set it to use VDD and VSS as the ADC limits, and you are running off a 5V supply, the calculation is 5/1023 = 0.004887 (4.887mV) per ADC step. So the actual voltage you are reading is the ADC value *0.004887 Volts.

2. Be careful with the calculation. You can use floating point arithmetic but the code will be smaller if you scale everything up by a factor of 100 or so then divide the answer by the same scaling factor. There is a danger that you lose accuracy while dealing with small fractions and you may even be seeing a complete failure of the result due to rounding errors.

3. Check you are actually producing a voltage and it is proportional to temperature, also make sure the ADC is configured properly to take the reading and from the correct pin.

4. 10mV per degree is a small voltage change but large compared to the resolution of the ADC. Consider using an amplifier to increase the voltage change so more ADC steps cover the same temperature change. It will greatly increase the accuracy of measurement.

Brian.
 
Vref as a reference to the PIC voltage source or?
What do you mean by configure the pic to an external Vref?

- - - Updated - - -

Considering an INTERNAL ADC, of PIC,what choice for ADC clock source must be used? what ADC acquisition time in microseconds must be set also?

- - - Updated - - -

Such that A001:


Code ASM - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
A001:   RLCF R0L,F
        RLCF R0L,F
        MOVLW 0x80
        ANDWF ADCON2,F
        MOVLW 0x3C
        ANDWF R0L,W
        IORLW 0x01
        MOVWF ADCON0
        MOVLW 0x03
        IORWF ADCON2,F
        MOVLW 0x12
        MOVWF R4L
        RCALL DL01
        BSF ADCON0,GO

 
Last edited by a moderator:

Please read the data sheet for information on how to configure the ADC. The code you posted makes no sense at all without knowing what type of PIC you are using and the circuit around it, in fact it looks like a section of code from the output listing of a compiler rather than real source code.

Basically:
1. configure the PIC pins so the one you are using (RA0) is an analog input.
2. configure the ADC result format (left justified/right justified)
3. configure the ADC clock source, the calculation is in the data sheet, the value depends on the source impedance (what you are measuring from) and PIC clock speed.
4. configure the reference source, usually you can set the lower and upper thresholds to VSS and VDD or connect them to other PIC pins so you can use you own voltages from outside the PIC.
5. decide whether you are using interrupts or polling to know when the ADC conversion has finished and add an ISR routine if necessary.
6. Start the ADC conversion and wait for it to complete. The result is then in the ADC results register which may be a single value or split across a high and low register if your PIC is 8-bit.

tip: When writing code, use the names of the registers and the names of the bits in them (as listed in the data sheet), it makes the program MUCH easier to read and follow.

Brian.
 
Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…