Potentiometer reading


Closed Thread
Results 1 to 13 of 13
  1. #1
    Join Date
    Jun 2011
    Location
    Philippines
    Posts
    223

    Default Potentiometer reading

    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:
    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

  2. #2
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,611


    Did you find this post helpful? Yes | No

    Default Re: Potentiometer reading

    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
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  3. #3
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,516


    Did you find this post helpful? Yes | No

    Default Re: Potentiometer reading

    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.

  4. #4
    Join Date
    Jun 2011
    Location
    Philippines
    Posts
    223


    Did you find this post helpful? Yes | No

    Default Re: Potentiometer reading

    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.


    Name:  Pot2.png
Views: 4686
Size:  20.0 KB

    Thanks and regards,
    tacbanon


  5. #5
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,516


    Did you find this post helpful? Yes | No

    Default Re: Potentiometer reading

    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.

  6. #6
    Join Date
    Jun 2011
    Location
    Philippines
    Posts
    223


    Did you find this post helpful? Yes | No

    Default Re: Potentiometer reading

    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

  7. #7
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,611


    Did you find this post helpful? Yes | No

    Default Re: Potentiometer reading

    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 ...

    Alain
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  8. #8
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    3,795


    Did you find this post helpful? Yes | No

    Default Re: Potentiometer reading

    what is your reference voltage?

    Ioannis

  9. #9
    Join Date
    Jan 2005
    Location
    Puerto Rico
    Posts
    133


    Did you find this post helpful? Yes | No

    Default Re: Potentiometer reading

    i use this and work
    DEFINE ADC_BITS 8
    DEFINE ADC_CLOCK 3 'Uses internal RC clock
    DEFINE ADC_SAMPLEUS 10 'Microsecond sample time
    My Hobbies is PicBasic PRO

  10. #10
    Join Date
    Oct 2011
    Posts
    54


    Did you find this post helpful? Yes | No

    Default Re: Potentiometer reading

    Hi tacbanon

    Define 20 OSC
    This should read
    Define OSC 20

    Phil

  11. #11
    Join Date
    Oct 2011
    Posts
    54


    Did you find this post helpful? Yes | No

    Default Re: Potentiometer reading

    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

  12. #12
    Join Date
    Jun 2011
    Location
    Philippines
    Posts
    223


    Did you find this post helpful? Yes | No

    Default Re: Potentiometer reading

    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

  13. #13
    Join Date
    Jun 2011
    Location
    Philippines
    Posts
    223


    Did you find this post helpful? Yes | No

    Default Potentiometer reading(solved)

    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.
    Code:
    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
    Name:  slider.jpg
Views: 2034
Size:  171.9 KB

    regards,
    tacbanon

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts