This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| en:examples:sensor:potentiometer [2015/11/09 15:47] – heikopikner | en:examples:sensor:potentiometer [2020/07/20 12:00] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | < | ||
| + | ====== Potentiometer ====== | ||
| + | //Necessary knowledge: | ||
| + | [HW] [[en: | ||
| + | [ELC] [[en: | ||
| + | [AVR] [[en: | ||
| + | [LIB] [[en: | ||
| + | |||
| + | ===== 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, | ||
| + | |||
| + | ===== 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: | ||
| + | |||
| + | * void adc_init(reference, | ||
| + | * unsigned short adc_get_value(channel) | ||
| + | * unsigned short adc_get_average_value(channel, | ||
| + | |||
| + | The function // | ||
| + | |||
| + | The following functions of the library are provided to support the ATmega2561 ADC converter. | ||
| + | |||
| + | ~~CL~~ | ||
| + | |||
| + | <code c> | ||
| + | // 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, | ||
| + | |||
| + | // Waiting the end of the conversion | ||
| + | while (bit_is_set(ADCSRA, | ||
| + | { | ||
| + | asm volatile (" | ||
| + | } | ||
| + | |||
| + | // 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. | ||
| + | |||
| + | <code c> | ||
| + | // Example program of potentiometer on the Sensor module | ||
| + | // The position of the potentiometer is displayed on the 7-segment indicator | ||
| + | #include < | ||
| + | #include < | ||
| + | |||
| + | // 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, | ||
| + | |||
| + | // Endless loop | ||
| + | while (1) | ||
| + | { | ||
| + | // Reading 4 times rounded values of the channel | ||
| + | value = adc_get_average_value(ADC_CHANNEL, | ||
| + | |||
| + | // Displaying the hundreds of the indicated value | ||
| + | // Robotic HomeLab II | ||
| + | // | ||
| + | // Robotic HomeLab III | ||
| + | segment_display_write(value * 10 / 2048); | ||
| + | } | ||
| + | } | ||
| + | </ | ||