I am setting up an 11 positions rotary switch; using resistors. Basically I've got a bunch of 20K resistors, and added them between connections, leaving just the common without it (so 11 positions, 10 x 20K resistors).
Now, I am reading values, and I get weird results. I would expect a linear increase/decrease form 0 to 1023 on the analog pin, but instead I get inconsistent readings.
At position 0 I get 0; yay; but then things change, while I head to position 11.
0 -> 0
1 -> between 10 and 32
2 -> between 29 and 47
3 -> between 29 and 78
4 -> between 41 and 83
5 -> between 49 and 99
6 -> between 41 and 102
7 -> between 29 and 106
8 -> between 908 and 956
9 -> between 960 and 1010
10 -> 1023
I was expecting to find a consistent increase in a sort of linear progression, while it seems that it is not like that. What am I doing wrong? These are the straight values coming out of the analogRead of a pin; which is connected to my common on the rotary switch.
The other 2 ends are connected to ground and to 3.3V
I was expecting a linear progression, so the value from 0 to 1023 would be in quantifiable steps, plus minus the error due to fluctuation of values (I was expecting something like 10-20 units variation).
I did not check the voltage; the rotary switch is in a closed box; so I have to open it up to check.
When I switch between positions, the values change immediately; so I can see these min-max values starting to appear as soon as I click to the next position.
Regarding the current, I have no idea if that would be enough to load the AD; how do I figure it out?
Sadly I did solder the resistors to the rotary switch already; I should have one spare to test with 1K resistors and see if that change things. I did pick 2K since it was readily available; the (wrong at this point) assumption was that resistor value was irrelevant, since I was counting by steps, so as long as all of them were the same; it should not matter. But at this point I feel it does matter, due to the lower current used (3.3V).
Thanks for your insight! Checking what you suggested tomorrow; it is past 1 AM now
What type of switch is it exactly, and how did you wire up the resistors? Your numbers could indicate that your switch has some sort of logarithmic resistance in it, so maybe you should measure the resistance in the switch for each of its positions. What is soldered can be de-soldered!
1M2? Brown, red, green + tolerance band or if 5 banded brown, red, black, yellow +tolerance. 10k should be brown, black, orange or brown, black, black, red.
OK, I did open the box and tested the resistors; beside being a bit off due to obvious reasons (I would not get the exact 20K reading), I did notice a weird behavior.
If I put the common in the middle (so equally distant 5 clicks from right or left position), I get the reading going down until I get the selected position, which read 0, then the reading goes up again in the opposite way.
With common on P6
P1: 98.7K
P2: 79.1K
P3: 59.3K
P4: 39.6K
P5: 19.8K
P6: 0
P7: 19.6K
P8: 39.5K
P9: 59.4K
P10: 79.4K
P11: 98.5K
If I switch to P5, The values change again, so P5 is considered the end where the resistor value is 0, and the other values change accordingly, so P1 will be79K -> 59K -> 39K -> 19K and 0 at position 5.
So the behavior of this rotary switch is that it change start and end position, and the resistor division is not a constant linear value, calculated over the total number of positions, but only between either the leftmost position or the rightmost position, and the middle pole.
Although this behavior does not explain the reading I get; right?
Have you got the correct terminal of your switch assembly connected to the 5V, gnd and analog input?
shinyknight:
OK, I did open the box and tested the resistors; beside being a bit off due to obvious reasons (I would not get the exact 20K reading), I did notice a weird behavior.
If I put the common in the middle (so equally distant 5 clicks from right or left position), I get the reading going down until I get the selected position, which read 0, then the reading goes up again in the opposite way.
With common on P6
P1: 98.7K
P2: 79.1K
P3: 59.3K
P4: 39.6K
P5: 19.8K
P6: 0
P7: 19.6K
P8: 39.5K
P9: 59.4K
P10: 79.4K
P11: 98.5K
If I switch to P5, The values change again, so P5 is considered the end where the resistor value is 0, and the other values change accordingly, so P1 will be79K -> 59K -> 39K -> 19K and 0 at position 5.
So the behavior of this rotary switch is that it change start and end position, and the resistor division is not a constant linear value, calculated over the total number of positions, but only between either the leftmost position or the rightmost position, and the middle pole.
Although this behavior does not explain the reading I get; right?
It is a constant resistor division, you have shown that the resistance difference between each position is 20K +/- the resistor tolerance.
Your method is just different to how others would measure the resistor array.
Can you post a picture of your switch and show how you have connected it up.
Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?
Can you please tell us your electronics, programming, Arduino, hardware experience?
Thanks.... Tom...
Yes, the connection should be fine. I have another pot (10K) connected in the same way to a different analog pin and I can read that correctly; so I assume that the connection is fine (they use the same ground pin and VCC pin).
I am using a compatible Arduino board; Both pins used for this rotary switch and the 10K pot are analog pins.
I did attach 2 pictures of the rotary switch (it has 2 rings, I just connected the resistors to the bottom to test it out) and a rough sketch of the connection.
As far as electronics, I am a SW engineer; I write mostly code in Python and C#, but years ago I used to work with ASM and C++. Got dull with higher level programming languages, but still remember some of the old school stuff
Electronic wise, I did some small projects; but I have no formal education; just learned while using components and mostly did work with microcontrollers to connect them to sensors, so I am not exactly an expert in either hardware nor electronics in general
Hi,
Well if your connections are okay.
What voltage readings with a DMM do you get at the analog input as your code is running and you rotate the switch?
I'm thinking that a total of 200K may be your problem due to the input impedance of the analog input.
If you had used 1K or 2K resistors you may get a reading like your 10K pot.
My guess is a dry solder joint, just retouch the solder joints on terminals 7 and 8 of the switch.
A dry solder joint explains both the high resistance seen between 7 and 8 but also the large
variation in values for each pin which would normally be stable within 1LSB (assuming
a clean 5V power rail).
So I did test the voltage and I can't get a reading that is stable enough. The multimeter is giving me a neverending string of values that fluctuate; which means that either the multimeter is bad (it is a 80 USD device, not a 800 one, but I assumed it was good enough for hobby usage). or the connections on the resistors are not good.
Not sure about either one, since the solder used for these connections is the same used all over the project, and everything else works fine, and I have no professional multimeter to compare with my one.
Not sure about the dry solder joint, since I get the same behavior if I turn the knob to another position. I always get 0 at the position in which the knob is set. I did order a set of 1K resistors and 100 OHM resistors, and will try with that. Got some extra info while reading other posts that made a bit clear what could be wrong with my setup.
In the worst case I just use a regular rotary encoder; but now I want to learn what I did that was wrong and why is that.