EEVblog Electronics Community Forum

Electronics => Microcontrollers => Topic started by: Etesla on December 05, 2017, 03:51:19 am

Title: Analog Input Issue
Post by: Etesla on December 05, 2017, 03:51:19 am
To be clear, I think this is a hardware issue. I am trying to read the value of a 10k pot using a pic 12f683 microcontroller.

When the pot is connected in such a way that the two 'outside' legs are connected to +5v and ground, and the middle leg is connected to pin 3 on the 12f683:
       I am able to read the input from the pot with no problem using the microcontroller, however the voltage measured from the middle of the pot to ground (via a multimeter) does not change in a linear way as it is turned. For example, if I turn the pot say, 10 degrees, the voltage goes up from 0v to 2.5v. The voltage then stays around 2.5v until the pot is turned to about 170 degrees, when it sharply rises again to 5v.

When the pot's middle pin is floating:
      The multimeter reading between the middle pin and ground reads as expected. turning the pot results in a nice, linear increase in voltage as expected.

Other Notes:
I also noticed that when using a pot with a significantly lower resistance (a 500 ohm pot), the issue was mostly resolved and the pot read as expected regardless of whether or not it was connected to the microcontroller. This is my current solution, but I suspect there is a better one.

All this leads me to believe that the input pin on the microcontroller is not a high impedance input. I am not sure how this is possible, and my multimeter reads very high resistance from pin 3 on the 12f683 to ground, and I thought all inputs on a microcontroller would have at least like a megaohm of input impedance...

Any ideas as to how or why this is happening???

Here's my code written in mikroC. All it is supposed to do is flash a led at a rate proportional to the voltage read on pin 3... It works fine except for the voltage coming off of the pot issue as described above. :




void newDelay(int t) {
 while(t > 0) {
  Delay_ms(1);
  t --;
 }
}

int readPot() {

int i = 0;
ADCON0.ADON = 1;

for(i = 0; i < 1000; i ++) {
}

ADCON0.B1 = 1;

while(ADCON0.B1) {
}

ADCON0.ADON = 0;

return ADRESL + (ADRESH * 256);


}

void b(int timer) {
 GPIO.GP2 = 1;
 newDelay(timer);
 GPIO.GP2 = 0;
 newDelay(timer);
}

void main() {

 CMCON0 = 0x07;

 TRISIO.GP2 = 0;
 TRISIO.GP4 = 1;
 ANSEL = 0b00001000;
ADCON0 =   0b10001100;

 
 while(1) {
 b(readPot() + 100);
}
}
Title: Re: Analog Input Issue
Post by: mikerj on December 05, 2017, 12:54:14 pm
What value do you have the configuration fuse set to, specifically the FOSC bits (2:0)?  If you inadvertently set this to INTOSC (101) instead of INTOSCIO (100), then you'd get the system clock being output on GP4, which would give you an average voltage of about 2.5v on that pin.
Title: Re: Analog Input Issue
Post by: Etesla on December 05, 2017, 03:00:57 pm
That was the issue. Thank you so much.
Title: Re: Analog Input Issue
Post by: mikerj on December 06, 2017, 08:55:43 am
Glad it was a simple fix :)