Slide potentiometer giving random values

When connecting this slide potentiometer, arduino keeps giving me random values. This is my code:

int potPin = 0; // define UNO board A0 pin connect the potentiometer
  
void setup() { 
  // Start the serial port, baud rate to 9600
  Serial.begin(9600); 
} 
  
void loop() { 
  // Read the potentiometer voltage
  int sensorValue = analogRead(potPin);   
  //Because the processing 'serial.read()' only support the value between 0-255, 
  //so you need to put a scale of 0-1023 numerical scale between 0-255
  int Voltage = map(sensorValue,0,1023,0,255); 
  Serial.write(Voltage);  
  delay(100); 
} 

It works fine with other potentio meters, just not the slider.
Should I add something to my code? Why does it give me these random values?

try this

const byte potPin = A0; // define UNO board A0 pin connect the potentiometer
  
void setup() { 
  Serial.begin(9600); 
} 
  
void loop() { 
  int sensorValue = analogRead(potPin);   
  Serial.println(sensorValue );  
  delay(100); 
}

Still giving me random values:

Have you connected the pot correctly, that is, at one end and at the center pin?

Your results indicate you may have connected to the ends, which yields a pretty fixed result.

this is not really random values, show photo of you setup

This is what the connection looks like.
Please correct me if i'm wrong, very new to this!

Here's a link to the slider where you can find the data sheet:

exchange two wires on the right

Still no logical numbers. The numbers i'm getting are stable but don't correspond to the place of the slider

Could this be an issue because of the cables i'm using?

@friedakroket, your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for problems with (nor for advise on) your project :wink: See About the Installation & Troubleshooting category.

I can’t see where your A0 wire is going ?

Hi, @friedakroket
Welcome to the forum.

Do you have a DMM to measure your slider to see if you have the right pins connected?
What readings do you get when you slide to either extremities of the travel?

Does your slider have an LED?

Thanks.. Tom.. :smiley: :+1: :coffee: :australia:

Your code should work, but names are a bit weird.
An A/D doesn't return a "Voltage", but an A/D value.

Test your pot.
Put the slider in the middle of the track.
Find the two pins that measure 10k ohm. That are the 5volt/ground connections.
Find the pin that measures 5k between that pin and ground or pin and supply. That's the wiper.
If you don't measure exact values, then you might have burned the pot.
An easy thing to do when you connect it the wrong way.

The attached simplified code should do the same.
Leo..


void setup() {
  Serial.begin(9600);
}

void loop() {
  Serial.println(analogRead(A0) >> 2); // 10bit to 8bit
  delay (100);
}

Highly inadvisable! :dizzy_face:

It's going to the slider connection in the middle on the right. The two end connections are in line, along the foreground edge of the potentiometer unit as viewed. This appears perfectly reasonable.

There is however another pin visible on the upper right of the potentiometer unit which just could be of some relevance.

Good pick!

You must have looked at the datasheet!

Thanks!!
I tested all the pins, found the ones i needed and now it works!

Thanks for the detailed explanation!

Frieda

Thanks everyone!

I think i was using the output pin instead of input which is why the led was turning on but i got random values. Found the right ones thanks to @Wawa 's explanation.

Frieda