# Potentiometer

## Theory

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

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:

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
{
// Allowing ADC to operate, selecting the frequency divider

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

// Converting the values of selected channel
{
// Setting the channel

// Starting the conversion

// Waiting the end of the conversion
{
asm volatile ("nop");
}

// Returning the results
}```

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/module/segment_display.h>

// Robotic HomeLab II

// Robotic HomeLab III

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

segment_display_init();

// Endless loop
while (1)
{
// Reading 4 times rounded values of the channel 