# Potentiometer

## Theory

Electric symbol of potentiometer

Potentiometer is a three terminal resistor, witch has fixed resistance between its two side contacts and variable resistance between side and middle contact. In principle a potentiometer is a attenuator (voltage divider), where resistance is formed between side contacts and middle contact.

A typical potentiometer consists of a resistor with conducting surfaces and of a sliding contact called slider. The closer the slider is to the edge of the resistor, the smaller is the resistance between the slider and the edge and vice versa. A material with high resistivity or coil made of resistance wire can act as a resistor. Some potentiometers have linear or logarithmic relations between the resistance and the slider position. Potentiometers are mainly single turn-potentiometers (example on the picture), but exist also slider potentiometers. A special type of potentiometers are digital potentiometers, where the regulation of the resistance is done inside the micro scheme according to the signals.

## Practice

Single-turn potentiometer

On the module of the HomeLab is a 10 kΩ turn potentiometer. This potentiometer is connected between ground and microcontroller supply potentials and the slider is connected to the analogue-digital converter (ADC). With this connection, the output voltage of the potentiometer can be regulated between 0 V and microcontroller supply. The digital value of the potentiometer output voltage on its entire adjusting range can be measured if the comparison voltage from AVR digital-analogue converter is taken from AVCC pin. For the Homelab III, the maximum voltage of the AVCC pin is 2.7 V but the inputs can deliver 3.3 volts. The following function for AVR ADC are in the library of the HomeLab:

• void adc_init(reference, prescale)
• unsigned short adc_get_value(channel)
• unsigned short adc_get_average_value(channel, num_samples)

The function adc_init must be called out in the beginning of the program, this is used for making the ADC to work. The reference voltage must be chosen from either AREF pin or AVCC pin, or fixed inner voltage must be selected. In addition the clock cycle of the converter must be set by the prescaler (factor of frequency divider), which will be used to divide the controller clock cycle. The conversion is quicker when using higher clock cycle but with this the accuracy may suffer. Function adc_get_value is for measuring, this able to select the channel and it returns 10 bit (11-bit for Homelab III) results. The function for measuring is inter locking, hence it waits for the end of conversion and returns the results only after all measuring is done.

The following functions of the library are provided to support the ATmega2561 ADC converter.

```// Starting the ADC
void adc_init(adc_reference reference, adc_prescale prescale)
{
// Allowing ADC to operate, selecting the frequency divider
ADCSRA = bit_mask(ADEN) | (prescale & 0x07);

// Selecting comparison voltage
ADMUX = (reference & 0x03) << REFS0;
}

// Converting the values of selected channel
unsigned short adc_get_value(unsigned char channel)
{
// Setting the channel
ADMUX = (ADMUX & 0xF0) | (channel & 0x0F);

// Starting the conversion
bit_set(ADCSRA, ADSC);

// Waiting the end of the conversion
while (bit_is_set(ADCSRA, ADSC))
{
asm volatile ("nop");
}

// Returning the results
return ADCW;
}```

In previously explained example program analogue-digital converter and 7 segment number indicator library are used. The value of analogue-digital converter is multiplied by 10 and divided by 2048 to get the value between 0 and 9. The value 10 is impossible to reach because while dividing in C-language only integer value is calculated and not rounded result. Function of averaging the result of converter is used to get more accurate result. Derived from this the operating program shows the numbers 0 to 9, which correspond to the position of the potentiometer on the indicator.

It should be noticed where is connected to the analog potentiometer in the particular hardware. For example, it is connected to an analog channel 3 in case of the Homelab Sensor Module II and analog channel 15th in case of the Home Labor III Interface module.

```// Example program of potentiometer on the Sensor module
// The position of the potentiometer is displayed on the 7-segment indicator
#include <homelab/adc.h>
#include <homelab/module/segment_display.h>

// Robotic HomeLab II
//#define ADC_CHANNEL 3

// Robotic HomeLab III
#define ADC_CHANNEL 15

// Main program
int main(void)
{
int value;

// Adjusting 7-segment indicator
segment_display_init();

// Adjusting ADC
adc_init(ADC_REF_AVCC, ADC_PRESCALE_16);

// Endless loop
while (1)
{
// Reading 4 times rounded values of the channel
value = adc_get_average_value(ADC_CHANNEL, 4);

// Displaying the hundreds of the indicated value
// Robotic HomeLab II
//segment_display_write(value * 10 / 1024);
// Robotic HomeLab III
segment_display_write(value * 10 / 2048);
}
}```
en/examples/sensor/potentiometer.txt · Last modified: 2020/07/20 14:26 by 127.0.0.1