# How to Use a Variable Resistor as a Switch

Written by jesse randall
• Share
• Tweet
• Share
• Pin
• Email

A variable resistor or rheostat is usually a tunable shaft connected to a knob, which can be turned by hand. The function of a variable resistor is to adjust the resistance across the terminals of the device, depending on the angle of the turn. Turning the knob clockwise may result in a higher resistance, for example, while turning it counter clockwise may lower it. To use a variable resistor as a switch, allowing for definite on or off states, requires a bit of logic design in order to measure the analogue resistance through the rheostat and then convert it into a desired state. A user may decide to use a single variable resistor as a three state switch, allowing, for example, the control of a light bulb array to turn on the first bulb if the resistance is between 0 and 3.33K ohms, a second light bulb at a resistance between 3.33 and 6.66K ohms, and the final bulb at 6.66K ohms and higher.

Skill level:
Easy

### Things you need

• Variable resistor (0 ohms to 10K ohms range)
• Microcontroller (PIC18F2525)
• Variable power supply
• Jumper wire (pre-stripped on ends)
• Alligator jumper cords
• 3 LEDs (red)
• 3 resistors (220 ohm)
• PIC18F2525 datasheet (available at www.microchip.com)

## Instructions

1. 1

Configure the workspace. Clear the bread board and plan the placement of all of the components to be installed. Connect the power supply, using alligator jumper cords, from the power supply's output to the power inputs on the bread board. Adjust the voltage of the power supply to 3.3 volts and turn it off for now.

2. 2

Insert the microcontroller into the breadboard and wire it up for normal operation. To do this connect its power supply pins to the 3.3 volt supply on the bread board and connect its reset pin to positive.

3. 3

Connect the variable resistor's two outside terminals to ground and positive of the 3.3 volt power supply. The middle terminal on the variable resistor will adjust, from 0 volts to 3.3 volts, depending on how far the knob is turned. Connect the middle terminal to one of the ADC (analogue to digital converters) on the PIC18F2525 microcontroller, such as AN0.

4. 4

Program the microcontroller with software to control the ADC pin. To do this, the pin must be configured as an analogue input with output latches disabled. In other words, the TRIS register must be set to "1" for the appropriate pin intended to act as the ADC.

5. 5

Program a control loop in the main function of the microcontroller software. This loop can be implemented with a "while(1)" loop, which keeps running indefinitely. Inside the while loop, design a test routine which periodically polls the value detected on the ADC pin. Since the ADC pin is connected to the potentiometer, the digital value, between 0 and 255, that the microcontroller reads in will depend on the exact rotation of the variable resistor.

6. 6

Wire three LEDs into the bread board to signify the state of the variable resistor. These are analogous to the light bulbs discussed earlier. Connect the negative side of the LED to the ground strip on the bread board and the positive side to RB0 on the microcontroller. RB1 on the microcontroller should connected to LED2 and RB2 should connect to LED3. All three LEDs must be connected through the 220 ohm resistor to prevent excessive current draw by the LED, which will permanently damage the microcontroller pins.

7. 7

Add to the program a set of if and then statements which look for specific ranges coming from the ADC. Since the variable resistor can adjust from 0 to 3.3 volts and the resolution of the ADC is 8 bits (0-255) a change of one digital value signifies a physical voltage change of 0.0129 volts (3.3-0)/256. Breaking up the voltage range to provide three separate modes, one for each LED, and an off mode, requires splitting the 3.3 volts into 4 equal sections. So from 0 to 0.825 volts, no LEDs will be on. This means when the ADC registers a digital value of between 0 and 64, the LEDs are off. However, as soon as 65 is reached (voltage exceeds 0.825 volts), LED1 will turn on. The same spacing is used to control LEDs 2 and 3 also.

### Don't Miss

#### References

• All types
• Articles
• Slideshows
• Videos
##### Sort:
• Most relevant
• Most popular
• Most recent