When lowering my Potentiometer, everything is fine, but when it hits zero, it looks fine for a moment but then jumps back to 100%. I have a B10K wired like this.
Is there anything I can do to stop this? I don't want my ksp ship to randomly accelerate...
Here is my code
#define POTENTIOMETER_PIN A0
void setup()
{
Serial.begin(9600);
}
void loop()
{
int data = analogRead(POTENTIOMETER_PIN);
int percentage = map(data, 0, 1023, 0, 100);
Serial.print("Potentiometer at ");
Serial.print(percentage);
Serial.println("%");
delay(100);
}
It sounds like a bad pot. If you have an ohmmeter measure the two pins and see if it goes to infinity when you are at minimum. From your description I would expect that. Replace it with a working pot would solve the problem. You can also reverse the gnd and Vcc connections and see if it behaves similar but jumps to zero.
Once you have the pot sorted, and you want a more stable 0-100% readout, try this sketch.
Leo..
// converts the position of a 10k lin(B) pot to 0-100%
// pot connected to A0, 5volt and ground
int rawValue;
int oldValue;
byte potPercentage;
byte oldPercentage = 101;
void setup() {
Serial.begin(9600);
}
void loop() {
// rawValue = analogRead(A0); // add dummy read, if needed
rawValue = analogRead(A0); // read pot
// ignore bad hop-on region of a pot by removing 8 values at both extremes
rawValue = constrain(rawValue, 8, 1015);
// add some deadband
if (rawValue < (oldValue - 4) || rawValue > (oldValue + 4)) {
oldValue = rawValue;
// convert to percentage
potPercentage = map(oldValue, 8, 1008, 0, 100);
// Only print if %value changes
if (oldPercentage != potPercentage) {
Serial.print("Pot is: ");
Serial.print(potPercentage);
Serial.println(" %");
oldPercentage = potPercentage;
}
}
}
my guess is that the wiper is leaving the track, so A0 is open circuit.
you can put a resistor (say 1M) across gnd and output that will fix that without significantly affecting the reading.
Does it work OK at the +Vcc end?
Potentiometer tracks are inherently noisy. While you COULD add some simple digital filtering in your code, its easier just to add a capacitor (around 10uF) between output & GND.
Cleaning only works for old pots. OP said that they were new.
I have seen beginners poke wires through the rivits of the carbon track, and solder that.
That definitely makes the pot unreliable, forever.
Leo..
Everything is shipped in containers over salt water... probably half a year ago. It is understandable that one or most of them have corrosion. I see it in "new" metal parts treated with a film of shipping oil.
I'm not sure what's happening here, I plugged the first pin on the left into GND, next I plugged the middle into A0, and then I put the last one into VCC. I have a Arduino Pro Micro board and I'm confused onto what's happening here. Does anyone know what could be causing this?