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