.. index:: quadrature rotary switch, rotary switch .. _qrms: Quadrature Rotary Switch ======================== Not that long ago, 'rotary switches' consisted of banks of wafers and contacts, they could be small with only one wafer and a couple of contacts or huge, long, gold plated and expensive. Of course they are still available today and have their specialised uses. 23 Position Wafer Rotary Switch ------------------------------- This rotary switch has 23 positions and two wafers. Each wafer has one 'wiper' that makes contact with a terminal at each position. .. image:: projects/quadrature-rotary-switch/rotary-wafer-switch-23pole.jpg Hi Quality Rotary Switch ------------------------ We won't see many of these again, they are definitely a thing of the past, with chrome or silver plated brass body, gold plated high current contacts etc. .. image:: projects/quadrature-rotary-switch/quality-rotary-switch.jpg Rotary Encoder -------------- .. image:: projects/quadrature-rotary-switch/rotary-encoder.jpg This is a high quality optical quadrature rotary position encoder, it's designed for CNC positioning, usually with a servo motor and not discussed in this article. Quadrature Rotary Switch ------------------------ .. image:: projects/quadrature-rotary-switch/quadrature-mechanical-rotary-switch.jpg .. image:: projects/quadrature-rotary-switch/quadrature-mechanical-rotary-switch-2.jpg This is the article subject, the 'Quadrature Rotary Switch'. These can be very small, cheap and simple and yet just as useful or even more useful as the old "wafer type' when deployed in embedded designs. They can replace the wafers and wires with their attendant losses and interference while decrease manufacturing costs enormously. They can drive IGBJT's from a MCU and control hundreds of amps, or high speed FET switches capable of GHz frequencies. The majority are a mechanical only design where the high end ones are optical and last forever, with no switch 'bounce', these cost $25 and up. These switches can be as cheap as AUD $0.20 each, search for 'rotary switch quadrature' at https://www.arrow.com/ my favorite supplier. The pictured units above are the mechanical type and the exact unit used in this article which also features a N/O PB switch as well as the encoder. To activate the switch, the shaft of the encoder is pressed. This shows the two pins for the pb switch and three pins for the encoder. This unit has twenty 'detents' which hold the shaft in positions 1 - 20, and these can be felt as the switch is rotated. Complexity ---------- There is a price to pay for all the Quadrature Rotary Switch advantages above, the switch can't just be wired up like a Wafer Rotary Switch and used right away because the switch positions have to be 'decoded' first by a specialised chip or a microprocessor and that's what I'm doing next using a :ref:`STMicro STM32F0 Discovery board` and 'quadrature decoding'. The aim of this article is to demonstrate how this can be done using a STM32F051 MCU and :ref:`Mecrisp-Stellaris Forth`. What is 'QUADRATURE DECODING' ? ------------------------------- There are a million articles online, here is a summary. Single Channel Encoder ^^^^^^^^^^^^^^^^^^^^^^ This is a single channel (non quadrature) encoder and can measure the speed of a shaft but not the absolute position or direction of rotation. .. image:: projects/quadrature-rotary-switch/gear-sensor.jpg Dual Channel Encoder -------------------- Quadrature Type Using two code tracks with sectors positioned 90 degree out of phase, the two output channels of the quadrature encoder indicate both position and direction of rotation. For example, if A leads B, the disk is rotating in a clockwise direction. If B leads A, the disk is rotating in a counter-clockwise direction. Therefore, by monitoring both the number of pulses and the relative phase of signals A and B, the microcontroller can track both the position and direction of rotation. .. image:: projects/quadrature-rotary-switch/interior-qrs-pic.jpg Mechanical Rotary Switch Type; cheap and nasty, but the design of Grey Code decoding makes for a surprisingly reliable switch. Grey Code --------- Grey Code is used in Quadrature Encoders because of its ability to handle noise, this link has a good explanation: https://www.tutorialspoint.com/what-is-gray-code STmicro Example ^^^^^^^^^^^^^^^ Technical Manual RM0091, Figure 141. Example of counter operation in encoder interface mode. Page 424/1004. The manual gives extensive information on quadrature decoding along with example C code. .. image:: projects/quadrature-rotary-switch/fig141.jpg Schematic --------- Contact Bounce ^^^^^^^^^^^^^^ .. image:: projects/quadrature-rotary-switch/contact-bounce.jpg Because mechanical encoders have a lot of contact bounce I find 'conditioning' the signal channels as below reduces false counts. Omitting these parts and just using the MCU internal 30K GPIO pullups can be problematic if you run long unshielded wires to the encoder because they can easily pick up noise, mains hum etc. This 'conditioning' gives a nice clean signal right ? ... not so much, see :ref:`'conditioner waveforms'` below. .. image:: projects/quadrature-rotary-switch/f0-quadrot-sw2.png Switch Waveforms ---------------- Here is a picture of the (conditioned) switch phases as the switch is slowly rotated between detents. .. image:: projects/quadrature-rotary-switch/quadrature-switch-delayed-pic-2.png .. image:: projects/quadrature-rotary-switch/quadrature-switch-delayed-pic-2.settings.jpg Oscilloscope channel-2 shows one contact (phase), and oscilloscope channel-1 the other. test-switch ^^^^^^^^^^^ A "test-switch" Word included in the source visually shows the switch contacts state in operation using the STM32F0 Discovery Board leds. The position of the switch and the state of the leds is the same as the 'Switch Waveform'. 1. run "test-switch" 2. While the switch is located at a detent the Green and BLUE leds will turn on. 3. Turn Switch *slowly* clockwise until the BLUE led turns off 4. Continue turning until the GREEN led turns off. Both leds are now off. 5. Continue until the BLUE led turns back on 6. Continue until the GREEN led turns back on. Both leds are now on and the switch has reached the next detent. This picture shows the detent positions and idealised voltages as the switch is turned clockwise. .. image:: projects/quadrature-rotary-switch/qms-geda.jpg .. _conditioner-waveforms: Conditioner Waveforms ^^^^^^^^^^^^^^^^^^^^^ The analog 'conditioner' forms a 'low pass filter' which attenuates all higher frequency signals including switch debounce spikes and fast shaft rotation signals. The faster the shaft is turned the smaller the signals until they're unusable. By design of the RC filter, this only occurs at speeds above normal use. Taken at PB4 and PB5 (Phase B and Phase A) as I flicked the knob as fast as I possibly could. You can see the initial acceleration causing loss of voltage excursion, which recovers as it slows down. This is the worst case signal scenario which occurs at higher than normal rotational speeds. This waveform is contrived because it is much slower in real world use. .. image:: projects/quadrature-rotary-switch/quadrature-mech-enc-switch-4.png Decoding -------- What is incredible is that the switch decoding was perfect, no missed steps. :: d 0 1 CW 2 CW 3 CW 4 CW 5 CW 6 CW 7 CW 8 CW .. _qrms_source: How does it work ? ------------------ STM32Fxx timers 1,2 and 3 have a 'quadrature encoder' mode (amongst many others). Timer3 in this mode is used to decode the switch phases A and B via Ports PB4 and PB5. A main loop checks the position of the switch and if it changes, the new position is printed. No interrupts or DMA are used. As mentioned previously, there is also a test program to visually show the switch contacts (via the F0 Disco Board leds) in relation to the detents as the switch shaft is rotated. Sourcecode ---------- This source was made using my (still Alpha level) SVD2FORTH-V6. If you haven't read my :ref:`Bitfields` article before I recommend reading it before proceeding. .. literalinclude:: projects/quadrature-rotary-switch/f0-quadrot-sw2.project.fs Summary ------- A real world test of a cheap (AUD $1.50 ea IIRC) Chinese Rotary Quadrature switch being read with a STM32F0 Disco Board (STM32F051 MCU). It's incredibly reliable even with a terrible signal as you can see. I didn't even have to feed the switch outputs into the dual comparator to clean them up further. I like this switch and will use it in further projects. A LCD display can print out the selection choices and the push button the switch can allow nested menues, only your imagination and flash will be your limitation. Of course this will work on any STM32Xxx with minor variations, perhaps pinouts (AF mode) etc.