Panel Mount 10K Dual Linear Potentiometer w/ On-Off Switch

I'm trying to understand how to code a Panel Mount 10K Dual Linear Potentiometer w/ On-Off Switch. I find all kinds of examples of simple pots with no on/off switch. The pot I am using has six pins and then the two switch pins.

I understand how to wire up the G, V and center pin, but I don't know how to wire up the on/off pins and read their state in the serial window?

Does anyone know of any tutorials or example scripts using this kind of pot?

Here is what this pot looks like: adafruit product 3393

Thank you...

Your pot has two seperate tracks. The outer pins (1 & 3 on the datasheet) are the track ends, and the track wiper is the center pin (2). The green part is the switch.

I dont know your application, but to read all the values I suggest you connect Vcc to pin 3 for each pot track, and ground to both pin 1's AND one pin of the switch.

The wiper pins you connect to analog pins - say A3 &A4, and use analogRead() to get their values.
https://www.arduino.cc/reference/en/language/functions/analog-io/analogread/

The remaining switch pin you connect to a digital pin set as input-pullup.
You get the switch stae by a digitalRead();
https://www.arduino.cc/reference/en/language/functions/digital-io/digitalread/

More info about switches

johnerrington:
The wiper pins you connect to analog pins - say A3 &A4, and use analogRead() to get their values.

Say A2 and A3 - you always want to reserve A4 and A5!

Paul__B:
you always want to reserve A4 and A5!

Why?

Do you know what function they have?

Yes, they're analog inputs. But now I'm guessing there's something else ... ?

Indeed they do have an important - and often used - alternate function.

Ha, found it: SDA and SCL. Didn't know they are connected to A4/A5 as well as 27/28! Thanks!

Erik_Baas:
Ha, found it: SDA and SCL. Didn't know they are connected to A4/A5 as well as 27/28!

That totally depends on which microcontroller you have. They are A4, A5 on a 328 on UNO, Nano, Pro Mini, but different on Mega, different again on Leonardo/ Pro Micro.

Not to mention the ESP8266!

johnerrington thank you very much.

UPDATE: to clarify: for the switch function there are only two pins. I connect one to ground and the other to D7 pin on the board. Then in the serial monitor I read the switch values, which should be just 0 (off) or 1 (on) correct?

My code below always shows 0 in the monitor, as I turn the switch on and off.

/*
 * Created by ArduinoGetStarted.com
 *
 * This example code is in the public domain
 *
 * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-potentiometer
 */

float floatMap(float x, float in_min, float in_max, float out_min, float out_max) {
  return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}

int ledPin = 13;  // LED connected to digital pin 13
int inPin = 7;    // pushbutton connected to digital pin 7
int val = 0;

// the setup routine runs once when you press reset:
void setup() {
  pinMode(inPin, INPUT);    // sets the digital pin 7 as input  
  Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop() {
  // read the input on analog pin A0:
  //int analogValue = analogRead(A0);
  // Rescale to potentiometer's voltage (from 0V to 5V):
  //float voltage = floatMap(analogValue, 0, 1023, 0, 5);

  val = digitalRead(inPin);   // read the input pin
  Serial.print(val);
  
  // print out the value you read:
  //Serial.print("Analog: ");
  //Serial.print(analogValue);
  //Serial.print(", Voltage: ");
  //Serial.println(voltage);
  delay(1000);
}

rfresh737:

pinMode(inPin, INPUT);

Is there a pullup resistor on pin "inPin"? If not, you can enable the internal one:

pinMode(inPin, INPUT_PULLUP);

Ahhh yes I forgot to add the internal pullup. Now when the switch is on I see 0 and 1 when it’s off.

I removed the commented lines for the analog and voltage values but those don’t change in the monitor when I turn the pot knob? It used to work. I must have stepped on something getting the on/off switch to display in the monitor.

/*
 * Created by ArduinoGetStarted.com
 *
 * This example code is in the public domain
 *
 * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-potentiometer
 */

float floatMap(float x, float in_min, float in_max, float out_min, float out_max) {
  return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}

//int ledPin = 13;  // LED connected to digital pin 13
int inPin = 7;    // pushbutton connected to digital pin 7
int val = 0;

// the setup routine runs once when you press reset:
void setup() {
  pinMode(inPin, INPUT);    // sets the digital pin 7 as input  
  pinMode(inPin, INPUT_PULLUP);
  Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop() {
  // read the input on analog pin A0:
  int analogValue = analogRead(A0);
  // Rescale to potentiometer's voltage (from 0V to 5V):
  float voltage = floatMap(analogValue, 0, 1023, 0, 5);

  val = digitalRead(inPin);   // read the input pin
  Serial.print("Switch: ");
  Serial.println(val);
  
  // print out the value you read:
  Serial.print("Analog: ");
  Serial.print(analogValue);
  Serial.print(", Voltage: ");
  Serial.println(voltage);
  delay(1000);
}

I don’t see a problem in your code. Check your wiring?

I commented out the on/off switch code and it is back to running the analog/voltage prints in the monitor. However, the values seem to change when I don't touch the pot knob.

Here is a snippet when I turned the shaft full on and then released the knob:

Analog: 2, Voltage: 0.01
Analog: 0, Voltage: 0.00
Analog: 8, Voltage: 0.04
Analog: 6, Voltage: 0.03
Analog: 10, Voltage: 0.05
Analog: 2, Voltage: 0.01
Analog: 3, Voltage: 0.01
Analog: 0, Voltage: 0.00
Analog: 4, Voltage: 0.02

Shouldn't the values remain the same once I release the shaft knob?

float floatMap(float x, float in_min, float in_max, float out_min, float out_max) {
  return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}

//int inPin = 7;    // pushbutton connected to digital pin 7
//int val = 0;
int analogValue = 0;
int prevAnalogValue = 0;
float voltage = 0.0;

void setup() {
  //pinMode(inPin, INPUT);    // sets the digital pin 7 as input  
  //pinMode(inPin, INPUT_PULLUP);
  Serial.begin(9600);
}

void loop() {
  analogValue = analogRead(A0);
  if (analogValue == prevAnalogValue) {
      return;
  }
  voltage = floatMap(analogValue, 0, 1023, 0, 5);

  //val = digitalRead(inPin);   // read the input pin
  //Serial.print("Switch: ");
  //Serial.println(val);

  prevAnalogValue = analogValue;
  Serial.print("Analog: ");
  Serial.print(analogValue);
  Serial.print(", Voltage: ");
  Serial.println(voltage);
  delay(1000);
}

Looks like an "open" input at A0. Exactly how is your potmeter connected?

Should be connected like this:

Yes it is connected like that:

dropbox wiring photo

Above link shows how I have it wired.

My mega 2560 is an old board. I think I will order a new board. Maybe my board is not working 100%.

WHen you turn the knob against its stop then release it will spring back a little way. Thats why your readings change a bit.
Do they change as you turn the pot?

Hi,

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom.. :slight_smile:

johnerrington:
WHen you turn the knob against its stop then release it will spring back a little way. Thats why your readings change a bit.
Do they change as you turn the pot?

Yes, the value changes as I turn the pot but there are more 0,0 displays than numbers. But yes, the values do change as I turn.