I am going to use a couple of toggle switches and a rotary switch (11 positions), and I would like to ask some questions about how do I use them, for my purpose. I am making a keyboard device, so the signals should be interpreted as key press.
I did check tutorials about how to use a momentary switch; but with the toggle switch; I can't use the same logic, since the button goes off when you release pressure on it; while a toggle stay on until you turn it off again. My objective is to toggle the switch on, and have the signal being registered once only; instead of being always on. Then when I turn off the toggle switch, the signal get processed again. Basically I want to use the toggle switch as a momentary switch.
Same goes for the rotary switch: it has 11 positions, so I was thinking if it was possible to use it to trigger button press. To give an idea; I would like to have the rotary switch to behave like a throttle wheel that you may find on a boat; so at 0 (position 6 for example, which is the center of the rotary switch), if I turn it right one position; I press a button to increase speed of the boat; if I go one more on the right; the "increase speed" is pressed; but if I go back one position, the speed decrease.
I thought of keeping a variable that increase and decrease, based on the current position and previous position (so I know that if previous position was 6 and new position is 7, I need to press the speed increase, while if the position goes down, I need to press the speed decrease). Is this correct?
Thanks in advance for any suggestion you may have. This is my first attempt at using switches beyond the standard momentary switch with Arduino.
If you want help with the toggle switches, you need to provide more information (manufacturer and model number, or SPST/SPDT/DPST/DPDT, two positions/three positions, spring return, center off, other features).
Not sure about your rotary switch.
You need to find and understand the State Change Detection example. This will help with knowing when a switch has changed rather than just knowing that a switch is on or off.
My objective is to toggle the switch on, and have the signal being registered once only; instead of being always on.
That sort of thing is known as a state change.
There is an example in the Arduino's IDE under the menu:-
File -> Examples -> 02.Digital -> StateChangeDetection
Thanks for your replies; now I know what to look for ("state change").
Regarding the rotary switch; I did get it from a company called Uxcell; the description says "2 pole 11 positions rotary switch". Can't find anything else on their website, beside this brief description
Can't find anything else on their website, beside this brief description
What else do you need to know?
2 pole 11 positions rotary switch
2 pole - there are two "sets" of switches
11 position - the switch can be in 11 places.
Are you sure of this because that would imply that the switch has 24 connections on it. Their is one common and 11 switched contacts at each position. At each switch position common is connected to one of the 11 position pins. You can use this in a few ways, the simplest is to connect the common to ground and each position to a digital input with the pull up resistors enabled.
Another way is to wire the common to an analogue input and the positions to taps in a resistor chain. Then use the analogue read to get a number which will be different for each switch position.
You can work out what pins connect to what with a DVM set on resistance.
It might be nice to know whether the rotary switch is "Make Before Break" or "Break Before Make". This will determine what is going to be read when the switch is being changed.
A lot of the rotary switches I've played with were all break before make so there's a bit of gap between each position. If one switched a bit slowly, Arduino may end up in between state and give confusing result. Maybe sample once every 1/2 second and if the 3 reading are the same, assume the switch is not being rotated and pass it. If it changes, assume someone is trying to turn the switch and do not pass new value, keep using old value a while longer.
A quick and easy 1-wire solution would be to use bunch of resistors and one analog pin, wire from switch common to Arduino analog pin. Have one position connect to ground (for reading of 0), have a resistor from position 0 to 1, another resistor from 1 to 2, etc so as the switch rotates, there's increasing resistance from ground. On position 11, connect to Vcc so it'd have high reading. Each of the 9 position not at GND or Vcc level would represent 1/10th of a voltage (10 resistor total) if my mayh isn't screwed up. I'd draw a schematic but on laptop atm and it's a pain in the butt to draw something on a laptop that doesn't look like a 3 year old kid drew it with crayon.
Grumpy_Mike:
What else do you need to know?
2 pole - there are two "sets" of switches
11 position - the switch can be in 11 places.
Are you sure of this because that would imply that the switch has 24 connections on it. Their is one common and 11 switched contacts at each position. At each switch position common is connected to one of the 11 position pins. You can use this in a few ways, the simplest is to connect the common to ground and each position to a digital input with the pull up resistors enabled.
Another way is to wire the common to an analogue input and the positions to taps in a resistor chain. Then use the analogue read to get a number which will be different for each switch position.
You can work out what pins connect to what with a DVM set on resistance.
I did not put the quote from vaj4088; he did ask the type and description of the rotary switch.
It does have 2 sets of switches; I need just one, but that's what I found online. From what I can see, there is a common on the same "ring" of the other connection, so in total it has 12 connections on 2 different rings, so 24 connections in total.
I think I like your suggestion regarding the resistors and use the analog input; that will give me always the same value, right? I can use that to store the current value and know if I am going up or down, based on the value of the resistor. I will give it a try. Do I need the same resistors for each contact? I am not sure I understand how the connection is done.
vaj4088:
It might be nice to know whether the rotary switch is "Make Before Break" or "Break Before Make". This will determine what is going to be read when the switch is being changed.
How can I find out which is which? Is there a way to check this with the multimeter?
You can do this visually, or you could use an ohmmeter (the ohms function of a multimeter) or a continuity checker.
Choose two pins, say positions five and six. Connect the continuity checker (or ohmmeter or...) to those two pins.
Put the switch at position five. Your continuity checker should show no continuity. Your ohmmeter should show infinity.
S-l-o-w-l-y move the switch from position five to position six.
If, during the move, you get continuity or a reading close to zero ohms, the switch is Make-Before-Break.
If, during the move, you still get no continuity or a reading of infinity, either the switch is Break-Before-Make, or you moved too fast.
As wilykat implied, most rotary switches are Break-Before-Make.
You do not need equal resistors, but this makes the math a bit easier.
You will not always get the same value, due to "noise". You should allow some error around the expected value. Using thresholds between the expected values is even better because it allows for a larger error.
Do I need the same resistors for each contact? I am not sure I understand how the connection is done.
You do not need the same resistor value but it helps make the calculations easier.
Consider the 11 switch contacts and call them 1 to 11. Wire 1 to 5V and 11 to ground. Then connect a resistor between 1 and 2, another between 2 & 3..... and so on until you get to 10 & 11.
Connect the common to an analogue input. Then do a test that just prints out the value you read from the input port. I would use 1K resistors.
Typically rotary switches like this are a single design that can be configured for 12 way, 11 way, 6 way dual pole, 4 way 3-pole and 3-way 4-pole.
Internally a different selector disc is needed for 4, 3, 2 or single pole, some little pegs are used to limit the
rotation angle, and the pole contacts are populated as needed for the number of poles. 11 way is 12 way with
a single peg to block continuous rotation, thus removing one of the positions too.
Make before break and break before make are (or perhaps were) both made.
Such switches are mainly legacy now, typically with digital interfaces a rotary encoder is much simpler to interface to. However they still have niche uses in analog circuitry - expect them to become unobtainium
at some point.