PDA

View Full Version : Potentiometer reading



tacbanon
- 5th January 2012, 14:25
Hello everyone, I'm trying to read the value of a 50K Slider/potentiometer. I'm using ADCIN command on a Pic16F877A at 20Mhz. But the reading is not steady(jumping from one value to another). How can I have a smooth reading from 0 - 255? I read that it is better to use 10K potentiometer, but I have none for now...hope someone can help me out.
Code:


' Define ADCIN parameters
Define 20 OSC
Define ADC_BITS 8 ' Set number of bits in result
Define ADC_CLOCK 3 ' Set clock source (3=rc)
Define ADC_SAMPLEUS 50 ' Set sampling time in uS


adval Var Byte ' Create adval to store result


TRISA = %11111111 ' Set PORTA to all input
ADCON1 = %00000011 ' Set PORTA.0,1,2,5 = A/D, PortA.3 = +Vref
mainloop:
ADCIN 1, adval ' Read channel 1 to adval
Serout2 PORTC.6,84, ["Value: ", DEC adval, 13, 10] ' Display value to serial
Pause 100 ' Wait .1 second
Goto mainloop ' Do it forever


End

Acetronics2
- 5th January 2012, 15:08
Hi, Tacbanon,

What does the PIC datasheet say about the source impedance for ADC ???

if no other pot value ... just use a voltage follower ( rail-to-rail OPA or use a special supply ... )

Also try to have only the A/D used inputs as inputs ( or ground them ) and tie pot case to ground ...

Alain

HenrikOlsson
- 5th January 2012, 15:59
I can never remember which one to use but I see you have the result, left-justified. Try right-justifying it instead and see if that helps (ADCON1.7=1).

The resistance of your pot is 5 times the recommeded maximum impedance but I don't think it should make the readings unstable. Try increasing the sample time by changing the DEFINE ADC_SAMPLEUS 50 to something higher and see what happends.

By the way, what do you mean by jumping from value to another? Is that a count or two or how much is it "jumping"? A little filtering might be all that is needed.

And finally, since you have the ADC configured for external VRef, make sure you have proper decoupling on your VRef input.

/Henrik.

tacbanon
- 5th January 2012, 23:53
Thanks Alain and thanks Henrik for the reply. I'm still new to ADC and so far I was able to make my LM35 work based on that code. As I understood in ADCON1 the four least significant bits controls the 8 A to D ports.
ADCON1 = 000011 gives me
PORTA.0,1,2,5 = A/D, PortA.3 = +Vref. Which now I think is not really what I need. I need only 3 ADC inputs and I don't have an external VRef for ADC..please correct me if I'm wrong, I should set it to either ADCON1 = 000010. But I still have the same results, please see the attached image to make it clear how the response look like and my pin connections.



Henrik:
Try right-justifying it instead and see if that helps (ADCON1.7=1).
I tried that but as I slide to the fullest i have 3-4 reading only.



6211


Thanks and regards,
tacbanon

HenrikOlsson
- 6th January 2012, 08:11
Hi,
Ah, so you had it configured for external VRef but didn't use that - that would explain it. Strange it didn't help when you reconfigured...

Have you verified the pot and connections, does the voltage at RA1 actually change when you sweep the wiper of the pot? Don't you have ANY other pot with lower resistance you can try with? If not just try with a simple resisotr divider.

Since the forum messes up displaying binary numbers I'm going to write them in decimal instead.
If you set the lower four bits to 0100 you'll get 3 analog inputs on AN0, AN1 & AN3 with Vdd/Vss as VRef.

tacbanon
- 6th January 2012, 09:44
Hi Henrik, I will try it to play it some more later tonight...and give feedback..

If not just try with a simple resistor divider.
Okay, I will try that later if nothing came up positive...

Thanks and regards,
tacbanon

Acetronics2
- 6th January 2012, 15:19
Hi, tacbanon

You should check that ...



Define 20 OSC
Define ADC_BITS 8 ' Set number of bits in result
Define ADC_CLOCK 3 ' Set clock source (3=rc)
Define ADC_SAMPLEUS 50 ' Set sampling time in uS



... I just see 5 errors ... :surprise:

Alain

Ioannis
- 6th January 2012, 15:55
what is your reference voltage?

Ioannis

jetpr
- 7th January 2012, 02:55
i use this and work
DEFINE ADC_BITS 8
DEFINE ADC_CLOCK 3 'Uses internal RC clock
DEFINE ADC_SAMPLEUS 10 'Microsecond sample time

Sherbrook
- 7th January 2012, 16:56
Hi tacbanon

Define 20 OSC
This should read
Define OSC 20

Phil

Sherbrook
- 7th January 2012, 17:30
Hi again
Read 'Selecting the A/D conversion clock' in the datasheet
It says R/C clock source not recommended at frequencies above 1mHz
For 20mHz clock source should be %010 or %110 (2 decimal or 6 decimal) ie
Define ADC_CLOCK 2
or
Define ADC_CLOCK 6

Phil

tacbanon
- 7th January 2012, 23:11
Hi jetpr and Sherbrook, I tried your suggestions but did not work for me...I will try to get another pot today and try it again...thanks for sharing your ideas.

Regards,
tacbanon

tacbanon
- 9th January 2012, 01:19
Hi everyone, I got the code to work but on 10K pot... Thanks for the inputs everyone.
I used the same connections and the codes from forum's input.


DEFINE OSC 20
' Define ADCIN parameters
DEFINE ADC_BITS 8
DEFINE ADC_CLOCK 3
DEFINE ADC_SAMPLEUS 50


adval Var Byte ' Create adval to store result(slider)


TRISA = %11111111
ADCON1 = %00000100

mainloop:


ADCIN 0, adval ' Read channel 0 to adval
Serout2 PORTD.2, 84, [$1B,$45] ' Clear
Serout2 PORTD.2, 84, [" Slider: ",#adval]'


Pause 150 ' Wait .15 second
Goto mainloop ' Do it forever


End


6219

regards,
tacbanon