Potentiometer analogRead() maxes out midway

Hello!
I am trying to use a 10K potentiometer (looks like the one in this link) for positional control with a Particle Photon (firmware version 2.0.1). The control knob rotates about 280 degrees. When doing an analogRead, I do get values from 1 through 4094 but the issue is that I get 4094 as I get close to 180 degree rotation of the knob and it remains at 4094 from 180 to 280 degrees. I have a few pots and all behave the same way. I’ve tried connecting the pot to all ADC pins on the photon but that does not change the behavior. I’ve tried this with 3 of my photons - same result.
Any thoughts on why this could happen? I need to be able to measure a rotation of ~ 210 degrees for my application.

Here is the code I am using. Any help is greatly appreciated!

int potpin= A1; //A1
int potValue = 0;
int potMappedVal = 0;


void setup() 
{

pinMode(potpin, INPUT);

Particle.variable("potValue", potValue);
Particle.variable("potMappedVal", potMappedVal);

}

void loop()

{
    potValue = analogRead(potpin);
    potMappedVal = map (potValue,1, 4094, 0, 270);
     delay(20);
    
}

@krazyvshank, the link your provided does not appear nor does it work.

Can you please described how you have the potentiometer hooked up?

Hi @peekay123. That’s strange. the link works for me.
Let me attach a picture of the pot.
I’ve connected the pot’s left pin to VIN, right pin to GND and the middle pin to A1.
https://media.digikey.com/Photos/Bourns%20Photos/PTV09A-4020F-B103.JPG

Use 3.3v not VIN.

4 Likes

Thank you @jstobaugh
It was as simple as that! It works fine now.

@jstobaugh - I do have a follow up question if you don’t mind. The pot reading works fine once I connected it to 3V3 and GND. Pot data pin connected to A1.
I have a servo connected to A5, VIN and GND. As soon as the servo begins running, the pot reading remains stuck at the last value it read before the servo started running. It does not change when I rotate the knob.
I checked the voltage with a multimeter between 3V3 and GND. The reading was 3.3v initially and 3.29V when the servo runs.
Any thoughts on why this could be happening?

Thank you!

Could you provide your latest code?

Sounds like something blocking in your code. I use A5 for a servo and A0 for battery monitor and it works fine.

Hmm…okay. Both potentiometer and servo are powered from the 3v3 and the VIN pins. Does that matter? As in, do I have to use an external power source for the servo?

Sure. Here you go.

Servo myservo;
int port = A5;
int potpin= A1;
int potValue = 0;
int potClosedUpValue = 0;
int potOpenValue = 90;
String tilting = "";
int continuous(String command);

void setup() {
  myservo.attach(port);
  pinMode(potpin, INPUT);

  Particle.function("continuous", continuous);
  Particle.variable("potValue", potValue);
}

void loop() {
  potValue = map (analogRead(potpin),0, 4095, -40, 220);
  if (tilting == "o") {
    if (potValue >= potOpenValue) {
      myservo.detach();
      tilting = "";
    }
  }
  else if(tilting == "c") {
    if (potValue <= potClosedUpValue) {
      myservo.detach();
      tilting = "";
    }
  }
}

int continuous(String command) {
  if (command == "o" && potValue < potOpenValue) {
    myservo.attach(port);
    delay(100);
    tilting = command;
    myservo.write(180);    
  } 
  else if(command == "c" && potValue > potClosedUpValue) {
    myservo.attach(port);
    delay(100);
    tilting = command;
    myservo.write(0);
  }
}

Assuming you are using an RC servo I would run it on the 5 volts. You need to verify current draw of the servo and make sure 5 volt input current is higher. Also a capacitor would be wise on the 5 volt connection going to the servo.

Yes, it is an RC servo and I’ve connected it to VIN which I believe is 5V.
Do you think not using an external power source for the servo causes this pot behavior?

Your continous() function is not returning an int value while promising to - that is an absolute no-go!
With 2.0.0 this will definitely cause an SOS+1 panic (hard fault).

1 Like

@ScruffR - Thank you. That was it. I had no idea it could result in such a strange behavior. Pot reading changes now with the shaft even with the servo running.
Great community here. Thanks again!

1 Like