Using Potentiometers to Replace Rotary Switches

Publisher:pi26Latest update time:2014-08-26 Source: 互联网 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 a rotary switch when rotary switches are unavailable or too expensive (Figure 1).

  Although it only takes a few instructions to digitize a potentiometer setting and make it behave like a switch, one immediate problem is that the switching threshold between one value and the next can become unstable due to electrical or mechanical noise. The solution to this problem is to introduce an upper and lower hysteresis threshold 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 one. In this way, the hysteresis effect allows for a clean transition between states.

  

 

  Figure 1: Replacement for 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 is that detent feel is lost. Another feature of the set point is that it can be set to any position, for example to compensate for nonlinear changes in the response of a potentiometer.

  The hysteresis is usually slightly higher than any noise that would cause unwanted switching. It is recommended to place a capacitor between the potentiometer contacts and ground to filter out contact noise (Figure 1).

  Figure 2 shows the algorithm. Once the potentiometer setting is digitized by the ADC, it is compared to the lower threshold and 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 this 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 a threshold or are at the same value from different states.

  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 that a single-turn potentiometer knob rotates one full circle 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 value is not continuously changed at a faster rate, subsequent samples will correct the switching state.

  Let's create a threshold list for seven switch states. Assume there is an 8-bit ADC. First, the 256-step range of the ADC is divided into seven switch states. The width of each switch state is the ADC range divided by the number of states, which is: 256/7=36.6. Rounding it off, the width of each state is 36, 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 switch 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 switch states. The thresholds are determined based on the hysteresis values ​​that increase or decrease to the boundaries. 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 threshold is obtained by adding 4 to the upper boundary and the lower threshold is obtained by subtracting 4 from the lower boundary, as shown in Table 1. From this example, it can be seen that switching from state 2 to state 1 requires the potentiometer count to drop to 4 less than the switching point value of 74, so the lower threshold is 70. Conversely, switching from state 1 to state 2 requires the potentiometer count to rise to 4 more than the switching 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 Threshold

  

 

  The code examples (see below) support the Silicon Labs C8051F310 (8051 architecture), but can be easily adapted for other microcontrollers.

  ;POT2SW 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 VALUE 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 < lwrval, so decrement switch position value

  ;check if switch position is < zero

  MOV A, POSMAX;load maximum switch position value

  CLR C

  SUBB A, SWPOS;max switch value - switch position

  JNC P2S2

  MOV SWPOS, #00H;reset switch position value to zero since underflow

  SJMP P2S2

  P2S1: ;check if pot setting is above upper theshold

  CLR C

  MOV A, UPRVAL

  SUBB A, B ;uprval - potval

  JNC P2S2 ;no carry means potval <= uprval

  INC 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

  I have 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

  RIGHT

  HYSTBL:;TABLE OF LOWER & UPPER THRESHOLDS FOR SEVEN POSITION SWITCH

  DB00D,41D;Switch state 0

  DB34D,77D;Switch state 1

  DB70D,113D;Switch state 2

  DB106D,149D;Switch state 3

  DB142D,185D;Switch state 4

  DB178D,221D;Switch state 5

  DB214D,255D;Switch state 6

Reference address:Using Potentiometers to Replace Rotary Switches

Previous article:Discussion on the drying method and issues to be aware of for transformers
Next article:Implementation of a four-quadrant DC/DC switching regulator

Latest Power Management Articles
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号