Using a potentiometer instead of a rotary switch

Publisher:BlossomBeautyLatest update time:2015-04-08 Source: eechina Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
Sometimes a microcontroller-based product requires the use of a rotary switch. Since many microcontrollers have built-in ADCs, a low-cost potentiometer can be used as an alternative to rotary switches when they are unavailable or too expensive (Figure 1).
Although it only takes a few instructions to digitize the potentiometer setpoint so that it behaves like a switch, one immediate problem is that the value at the switching threshold between one value and the next will be unstable due to electrical or mechanical noise. This problem is solved by introducing upper and lower hysteresis thresholds for each transition, so that the potentiometer needs to cross a threshold before the next switch state is valid. For each updated switch state, a new pair of thresholds replaces the previous thresholds. In this way, the hysteresis effect allows a clean switch between states.


Figure 1: Alternative to a multi-throw switch.

This approach has many advantages: single port pin versus multiple port pins for a rotary switch, low cost, greater availability, and debounced switching. The disadvantage of this approach is that it loses detent feel. Another feature of the setpoint is that it can be set to any position, for example to compensate for nonlinear changes in the potentiometer's response.
The hysteresis is usually slightly higher than any noise that would cause undesired switching. It is recommended to place a capacitor between the potentiometer contacts and ground to filter contact noise (Figure 1).
Figure 2 outlines the algorithm. Once the potentiometer setting is digitized by the ADC, the value is compared to the lower threshold. If it is below the lower threshold, the switch state is gradually reduced and limited to zero. If the potentiometer setting is above the upper threshold, the switch state is gradually increased and limited to the maximum value. If the switch state changes, the upper and lower thresholds are updated and the subroutine is terminated.


Figure 2: Flowchart.

To ensure that the hysteresis algorithm works, the potentiometer setting must be read periodically and compared to the last switch state. This is done to distinguish between potentiometer settings that cross the threshold from different states or are at the same value from the same state.
Here, it is also necessary to calculate the minimum sampling rate, which can be obtained by dividing the maximum potentiometer rotation rate by the number of switch states. For example, assuming a single-turn potentiometer knob that rotates one full turn in 0.25 seconds and assuming there are seven states, the minimum scanning rate is 28Hz. If the potentiometer value sampling period is lower than the minimum value, the calculated switch state may be incorrect even if the switching direction is correct. If the potentiometer setting is not continuously changed at a fast rate, the subsequent sampling will correct the switching state. [page]
    
Create a threshold list for seven switching states. Assume an 8-bit ADC. First, the 256-step range of the ADC is divided into seven switching states. The width of each switching state is the ADC range divided by the number of states, that is: 256/7=36.6. This is rounded to 36 for each state, but the two outer states need to be increased to 38 to make the total width 256.
The next step is to determine the boundaries of each switching state. For state 0, the boundaries are 0 to 37 (inclusive). State 1 starts at 38 and ends at 73, and so on for the remaining switching states. The threshold is determined based on the hysteresis value added or reduced to the boundary. Here, a hysteresis value of "4" is used. The hysteresis amount must not be greater than the width or less than the expected noise. Therefore, the upper boundary plus 4 gives the upper threshold, and the lower boundary minus 4 gives the lower threshold, as shown in Table 1. From this example, it can be seen that switching from state 2 to state 1 requires the potentiometer to fall by 4 less than the switch point value of 74, so the lower threshold is 70. Conversely, switching from state 1 to state 2 requires the potentiometer to rise by 4 more than the switch point value of 73, so the upper threshold is 77. The table used for the program code only needs to indicate the upper and lower thresholds, which only requires 14 bytes in this example.


Table 1, Thresholds.


The code examples (see below) support the Silicon Labs C8051F310 (8051 architecture), but can be easily adapted for other microcontrollers.
OT2SW INITIALIZATION
                MOV UPRVAL, #00H ;set upper value to opposite end to force the code to run
                MOV LWRVAL, #0FFH ;set lower value to opposite end to force the code to run
                MOV SWPOS, #03H ;initialize switch position to middle
                MOV POSMAX, #06H ;set maximum switch position value
;SUBROUTINES
POT2SW: ;CALCULATE SWITCH POSITION VALUE FROM POTENTIOMETER V ALUE IN ACC
                                ;check if pot setting is below lower threshold
                CLR C                
        
                MOV B, A ;save pot setting to register B
                SUBB A, LWRVAL ;potval - lwrval
                JNC P2S1 ;no carry means potval >= lwrval
                DEC SWPOS ;carry means potval uprval, so increment switch position value
                                ;check if switch position is > max
                MOV A, POSMAX ;load maximum xwitch position value
                CLR C
                SUBB A, SWPOS
                JNC P2S2
                MOV SWPOS, POSMAX ;reset curve number to max curve value since overflow

        P2S2: ;read lower and upper thresholds using switch position value
                MOV A, SWPOS ;multiply switch position value by 2
                MOV B, #02H
                MUL AB                
                MOV B, A ;save multiplied value as table offset
                MOV DPTR, #HYSTBL ;load base address of table pointer
                MOVC A, @A+DPTR ;look up table value from base address + offset
                MOV LWRVAL, A ;read lower threshold value
                MOV A, B                                                        
                INC DPTR ;increment base address
                MOVC A, @A+DPTR
                MOV UPRVAL, A ;read upper threshold value
                RET

HYSTBL: ;TABLE OF LOWER & UPPER THRESHOLDS FOR SEVEN POSITION SWITCH
                DB 00D, 41D ;Switch state 0 
                DB 34D, 77D ;Switch state 1
                DB 70D, 113D ;Switch state 2
                DB 106D, 149D;Switch state 3
                DB 142D, 185D;Switch state 4
                DB 178D, 221D;Switch state 5
                DB 214D, 255D;Switch state 6
Reference address:Using a potentiometer instead of a rotary switch

Previous article:Master MCU software design principles to improve DC motor control accuracy
Next article:In-depth study of MMU of bootloader under ADS

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号