Microcontroller Interfacing Part 14

Using a Variable Resistor as a Control

  

  

Goals

Push button and other switches are great input devices for some applications. For some things a knob that can be turned is a much better Human Machine Interface (HMI).  The volume control on a radio is a good example.  Situations where the user wants to make a change, but can decide when the proper level is achieve by their normal senses (sight, sound) will tell them when they reached the proper point are good candidates for this type of an input.

This section shows how to hook up a variable resistor, also known as a potentiometer, or pot for short to the Analog/Digital converter (ADC)  input of a microcontroller to create a volume control like input device.

Voltage Dividers

A voltage divider is simply two resistors in series with the free ends connected to power and ground.  Voltage dividers are discussed in more detail in Part 2.  Figure 14-1 shows a simple voltage divider.

The voltage at the resistor junction will depend on the ratio of the values of the two resistors.

  

Vout = Vin* R1/(R1 +R2)

  

As the relative value of R1 increases with respect to R2, Vout will increase.  If we replace R1 and R2 with a  variable resistor we can adjust the value of Vout.  The wiper lead can be connected to the A/D input of the microcontroller, and the current setting can be measured and alter the operation of the program. The diagram for this shown in Figure 14-2.

For now, ignore R2 and C1. As the knob is turned clockwise, Vout will increase until it reaches the stop.  At this point the equivalent circuit in figure 14-1 would have R2 with a value of 0 ohms, and Vout would equal the supply voltage, in this case 5V. Conversely, if the pot is adjusted counter clockwise the voltage Vout will drop, reaching 0 Volts at the stop.

The value of R1 is not critical  but values between 10K and 100K are typically used. R2 and C1 are optional.  They will provide a degree of protection from static discharges (see Part 10). They will also help filter noise.  Inexpensive or worn pots frequently make intermittent contact as they are adjusted and the voltage can jump around a bit.  Adding R2 and C1 can help filter out the spikes which can be important in some control applications.  Typical values for R2 are up to a few hundred ohms and .001 to .1uF for C2.  Larger values with increase the filtering.

  

Figure 14-1

Voltage Divider

What Kind of Variable Resistor to Use

As mentioned earlier, variable resistors are often used as volume controls in audio equipment.  Human hearing is logarithmic. This means that as the volume goes up, a small increase in the signal going to the speaker or head phones will not be very noticeable.  It takes a fairly large increase in the audio power to make much difference in our perception of the volume.  The pots used in these applications have a logarithmic variation in resistance as they are turned up, resulting in a large increase in the audio amplifier’s output.

If you take a pot from an old radio and hook it up as in Figure 14-2, you will find that as you turn it from one end to the other, the A/D output will increase slowly at first. Then near the other end a very slight adjustment will have a large effect on the count.  It will be difficult to control at that point.

You want to use a pot with a linear taper for a control going into an ADC.  If you don’t know what kind you have, it is easy to test.  See Figure 14-3. Hook an ohmmeter to the fixed terminals and measure the resistance.  Turn the shaft so that it is close to half way between the end stops and measure the resistance between the wiper terminal and the end terminals. It should be roughly the same between the wiper and both ends, and about ½ the value of the resistance between the fixed end terminals.  If this is the case you have a linear taper pot. If there is a great difference between the resistance between the wiper and one terminal, and between the wiper and the other end terminal, you have a logarithmic pot.

  

Figure 14-2

Jitter

You may notice that even though you are not touching the pot, the reading jumps around a bit. This will increase for A/D converters with higher numbers of bits. There are several possible causes for this. One is noise in the system. Part 13 discussed noise in A/D converters.  Another possibility is that the voltage reading at this particular setting is right on the edge of transition from one value to the next. Sometimes it will be one value, the next it will be one count higher or lower.

These problems can be reduced a couple of ways. One way is to take several readings and average them.  Larger number of readings in the average will reduce noise but take longer as more reading are needed, and the time for doing the math.

Another way is to just take a reading and divide by 2 or 4. This reduces the resolution and less susceptible to noise or falling on a critical transition level. A simple division will reduce the number of possible values. Essentially you are truncating the lower bits.  If you have an 8 bit ADC you would have 256 possible values.  If you divide by two you only get 128 and get 64 if you divide by 4. This might reduce the number of settings too much for some applications

An Example Program

Listing 14-1 is a program for the Arduino.  Hook up your pot as in Figure 14-2.  The wiper should go to input A0.  The Arduino takes A/D readings and displays the returned values on a Unified Microsystems ATS-1 LCD shield.  There are 3 readings displayed.  One is the raw data, one is 4 readings averaged, and the other is a reading divided by 4, essentially truncating the lower two bits.

You can play with this program and get a feel for how the different ways of handling the data affect the operation. Try it with a linear and a logarithmic pot.  Try it with and without R2 and C1 in Figure 14-2. Try different values for R2 and C1.  Change the number of readings averaged in the program.

  

  

// RCONTROL  

// Version 1.00

// March 2012

//

//This program hooks a variable reistor to the AO input. Adjusting the pot will change the value returned by the AD converter. The results are shown 3 ways:

// RAW: The value of the last A/D conversion. This will probably jump around a bit.

// Trunc: Increased stability by truncating the lower bits of the reading

// Ave: A number of readings avereaged together to increas stability.

//

// The results are shown on a Unified Microsystems ATS-1 Terminal Shield.  www.unifiedmicro.com

///RCONTROL  Program by Gary C. Sutcliffe is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.

//Updates:

  

int analogPin = 0; //The wiper is connected to Analog pin 0

  

#define CLS  0x01  //Character to send to the ATS-1 to clear the LCD and home the cursor on the LCD display

  

// experiment with the following values to see what the results are

#define AVE_SAMPLES 4  //number of samples to collect to for the average

#define TRUNC_VAL  4  //divisor for truncating lower bits. 2 will truncate LSB, 4 lowest 2 bits, 8 lowest 3 bits

  

void setup()

{

Serial.begin(4800);   // opens serial port, sets data rate to 4800 baud to display 

}

  

void loop()

{

int rawAD;  //Raw A/D reading for display

int aveAD;  // Averaged A/D reading

int truncAD; //AD reading with

int count;  // used for keeping track of number of samples taken 

while(1)

{

aveAD = 0;  //Clear the avereaged value

for(count = 0; count < AVE_SAMPLES; count++)  //loop to collect a number of samples for the average

  {

  rawAD = analogRead(analogPin);    // read the input pin

  aveAD = aveAD + rawAD;  //add up all the readings in aveAD

  }

aveAD = aveAD/AVE_SAMPLES;  //aveAD now has the average of the samples taken

truncAD = rawAD/TRUNC_VAL;

Serial.write(CLS);    //clears the LCD display and puts cursor in upper left corner

Serial.print("R: ");  //start printing out the different results R: is the raw value

Serial.print(rawAD);

Serial.print(" A: ");  //A is the averaged value

Serial.println(aveAD);

Serial.print("Truncated: ");

Serial.print(truncAD);  

delay(2000); //put a little delay before we start a new set of readings

} //End of while() statement

  

} // End of loop()  

Listing 14-1

Summary

A pot connected to a microcontroller’s A/D input can be a good user control for many applications.  Properly implemented it provides a natural input device.

Gotcha List

1. Use a linear taper pot for best results.

2. Use averaging or other technique to stabilize the signal

3. If turning the knob one way makes the ADC count go opposite the desired direction,  switch the pot connections to +5V and GND.

  

Figure 14-3.  Arduino with potentiometer running test program.

Arduino I/O Expander

© 2009 - 2022 Gary C. Sutcliffe

  

Created with the QTH.com SiteBuilder.