Rotary Switch Odd Input

Hello,

I recently purchased the Rotary Switch Potentiometer kit with board and 10 position switch. I followed instructions here: https://learn.sparkfun.com/tutorials/ro … okup-guide. I used 9 total 10k resistors.

I simplified the provided code to just output which slot is selected to the serial monitor. My code is below. However, I am getting some odd results. When I turn the knob to each position, I’m getting inconsistent results:

Position 1: val = 0

Position 2: val = 1

Position 3: val = 3

Position 4: val = 4

Position 5: val = 6

Position 6: val = 8

Position 7: val = 9

Position 8: val = 9

Position 9: val = 9

Position 10: val = 9

I don’t see any shorts. Any guesses what’s happening?

/* Test rotary switch
// modified from https://github.com/sparkfun/Rotary_Switch_Potentiometer

Demonstrates using the Rotary Switch Potentiometer breakout board 
with a microcontroller, to build a 10 position selector switch.

The Rotary Switch Potentiometer is a breakout board that adds 9 resistors to a
10 position rotary switch, to make a custom-taper, stepped potentiometer.  This
example uses 9 10KOhm resistors, and connects the rotary switch potentiometer
to an analog input. 

The Rotary Switch Potentiometer board was populated with 10K resistors in 
every position.  It was connected to the RedBoard as follows:

RedBoard pin : Rotary Switch Potentiometer Pin
----------------------------------------------
GND          : CCW
A3 aka 17    : W
5V           : CW

*/

#define BAUDRATE 115200
#define ROTARY_PIN 3 //A3, analog 3, aka pin 17 on Teensy 4.0

void setup() {
  Serial.begin(BAUDRATE);
  Serial.println("Begin Test Rotary Switch");
}

void loop() {
  uint16_t input;
  uint16_t val;

  // read the ADC
  input = analogRead(ROTARY_PIN);

  // Translate ADC value from 0-1023 to 0-9.
  // This implements the proportion
  // input/1023 = val/9.
  // One "step" of the pot is about 113 ADC counts.
  // We're adding 65 (1/2 of 113) to the input value, so that the 
  // input is in the middle of the window, rather than right at the edge, so values 
  // are stable and solid.
  val = (input+56)*9/1023;

  Serial.println(val);
}/code]

I think the code simply counts the resistance to determine what position the potentiometer is in.

Have you tried measuring the resistance of each position? Do they go up by 10K per notch as they should? If so, I would imagine there must be an issue with the code.

If I put the multimeter from the CW (ground) to W (positive), I get the following:

Step 1: 0

Step 2: 9.94

Step 3: 19.82

Step 4: 29.77

Step 5: 39.61

Step 6: 49.4

Step 7: 59.2

Step 8: 69.5

Step 9: 79.3

Step 10: 89.3

Looks like the wiring is good on the switch.

For my circuit, I’m using a Teensy 4.0. Ground to CCW, A3 (aka 17) to W, 5v to CW. Is that wiring correct?

Hmm. New twist. It works fine on an Arduino Uno using A3. But on a Teensy 4.0 using A3 I get my weird results. Any guess why?

Teensy is a 3.3 volt only board, you’re feeding it 5 volts. I think that’s your issue.

I cross posed on the Teensy forum. https://forum.pjrc.com/threads/70163-An … h?p=305258

Looks like the Teensy uses 3.3v but this board is 5v, yes? I’m using an OctoWS2811 Adaptor which does provide 5v power. Is this my issue? Any way to fix this with something else in the circuitry?

Connect the potentiometer to GND, 3.3V and your analog pin. Provided you haven’t damaged the Teensy with 5 volts, it should work.