Measure Resistane Using a Rotary Swich

I'm trying to use a rotary switch to turn on 7 individual leds based on connecting 7 different resistance values. This is the switch. https://www.mouser.com/datasheet/2/626/Grayhill_07272021_7db2d6_2bce8318cc2c400da5c3930fd-2509274.pdf
The individual tabs of the switch will have a different resistor with all 7 leads connected to A0.
I'm unsure were the center terminal o]f the switch will be connected to - I tried 5v and GND - in both cases got garbage on serial monitor.
The attached sketch is only trying to turn on the onboard led just for testing.

Is this doable and if so what am I doing wrong
Thanks

const int POT_PIN = A0;
int swcnt = 0;
#define led 13

void setup() {
  Serial.begin(9600);
  pinMode(led, OUTPUT);
}
void loop() {
   // Read potentiometer value
   int val = analogRead(POT_PIN);
    Serial.print (val);
    Serial.println (" "); 
    delay(200);
    
    if (val = 10) {
      swcnt = 0;
    }
    if (val = 47) {
      swcnt = 1;
    }
    if (val = 100) {
      swcnt = 2;
    }
    if (val = 180) {
      swcnt = 3;
    }
    if (val = 220) {
      swcnt = 4;
    }
    if (val = 470) {
      swcnt = 5;
      if (val = 510) {
        swcnt = 6;
      }
    }
    if (swcnt = 0) {
      digitalWrite (led, HIGH);
    }
  }

Schematics please.... Your words doesn't make the set up clear.

How come? There's no print to serial monitor in the code.

1 Like

For starters you have to look for a range of values. You're rarely going to get any exact value from an analog input. Plus, there is usually some noise/instability and there is always the possibility of being on the hairy-edge between two digital values, even if everything is "perfect".

If you have a known and unknown resistance in a voltage divider, you can calculate the unknown resistance.

It's a bit tricky and will require some algebra to derive a C+ expression. I don't have it off the top of my head, but the voltage is non-linear with resistance change because it's a ratio.

Be careful with low resistance. For example, with 5V applied 10 Ohms total, that's half an Amp (Ohm's Law) and the voltage might drop if the power supply can't supply it, or you could burn-up a small resistor.

What kind of "garbage"? Do you have a multimeter to measure the voltage and does the reading correspond to the voltage?

Thank you heading me in the right direction re voltage divider - I'm not familiar with them so I'll doing some reading up and get back to this post.

OK.. So I read up on the voltage divider and here's my revised drawing.


When the dial is rotated to the corresponding contact the voltage divider sends the corresponding voltage to the Uno via A0.
Here's the sketch

const int POT_PIN = A0;
int swcnt = 0;
#define led 13
#define tolerance 10

void setup() {
  Serial.begin(9600);
  pinMode(led, OUTPUT);
}
void loop() {
  // Read potentiometer value
  int val = analogRead(POT_PIN);
  Serial.print (val);
  Serial.println (" ");
  delay(200);

  if (val >= (512 - tolerance) && val <= (512 + tolerance)) {
    swcnt = 0;
  }
  if (val >= (585 - tolerance) && val <= (585 + tolerance)) {
    swcnt = 1;
  }
  if (val >= (678 - tolerance) && val <= (678 + tolerance)) {
    swcnt = 2;
  }
  if (val >= (831 - tolerance) && val <= (831 + tolerance)) {
    swcnt = 3;
  }
  if (val >= (875 - tolerance) && val <= (875 + tolerance)) {
    swcnt = 4;
  }
  if (val >= (924 - tolerance) && val <= (924 + tolerance)) {
    swcnt = 5;
  }
  if (val >= (953 - tolerance) && val <= (953 + tolerance)) {
    swcnt = 6;
  }

  if (swcnt = 0) {
    digitalWrite (led, HIGH);
  }
Before I go and by the switch I just want to make sure I'm on the right track

Isn't R1 = 300K and R2 = the seven other values?

Yes, the other 7 i.e., vary based on switch position.

Are you ordering the switch with shorting between switch positions, or open circuit between switch positions?

The switch is non-shorting.

Certainly you probably want == (test for equality) not = (assignment) here. Your first post was full pf the same type expression.

Which is not an error, but unusual. Go to the IDE and turn up all warnings and verbosity, this kind of mistake not an error will be brought to,your attention so you can decide if it is what you really meant or not.

a7

thank you

Then be aware that the analog pin will see the full voltage between positions - however a 300k resistor is stout protection so its probably not an issue - in future consider using the multi-way switch on the top resistor in the divider, not the bottom one...

I know that it is not the question of the post, but have you considered a rotary encoder instead?
You could just count the clicks to the right or to left by software to control the leds. You would need just 2 arduino pins and you would get rid of the resistors complexity.

Hi,

You seem to have your calculations mixed up.

A0 will be measuring the voltage across each of the lower switched resistors, the 300K being in circuit all the time.

For example ;
Position 1, A0 is measuring across the 51K resistor.
So the voltage across the 51K is;
5 * ( 51 / ( 300 + 51 )) = 0.73V

Position 2, A0 is measuring across the 68K resistor.
So the voltage across the 51K is;
5 * (68 / ( 300 + 68 )) = 0..92V

So;

Tom... :grinning: :+1: :coffee: :australia:

Then the values in your table are wrong

I know that it is not the question of the post, but have you considered using a diode encoder matrix?

You'd need three digital input pins and a bit of code to give you the switch position.

No messing with resistor values.

a7

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.