This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| en:examples:sensor:potentiometer [2014/08/12 11:58] – rellermaa | en:examples:sensor:potentiometer [2020/07/20 09:00] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ~~PB~~ | + | < |
| - | ====== | + | ====== |
| - | //Necessary knowledge : [HW] [[en: | + | //Necessary knowledge: |
| + | [HW] [[en: | ||
| + | [ELC] [[en: | ||
| + | [AVR] [[en: | ||
| + | [LIB] [[en: | ||
| ===== Theory ===== | ===== Theory ===== | ||
| Line 9: | Line 13: | ||
| 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. | 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, | 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 ===== | ===== Practice ===== | ||
| + | [{{ : | ||
| - | On the module of the HomeLab is a 10 kΩ turn potentiometer. This potentiometer is connected between ground and +5 V potentials and the slider is connected to the channel 3 of the analogue-digital converter. With this connection, the output voltage of the potentiometer can be regulated between 0 V and 5 V. 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. The following function for AVR ADC are in the library of the HomeLab: | + | On the module of the HomeLab is a 10 kΩ turn potentiometer. This potentiometer is connected between ground and microcontroller supply |
| - | ~~CL~~ | + | * void adc_init(reference, |
| + | * unsigned short adc_get_value(channel) | ||
| + | * unsigned short adc_get_average_value(channel, | ||
| - | <code sql> | + | The function |
| - | // | + | |
| - | // Data types for adjustment | + | |
| - | // | + | |
| - | typedef enum | + | |
| - | { | + | |
| - | ADC_REF_AREF = 0x00, | + | |
| - | ADC_REF_AVCC = 0x01, | + | |
| - | ADC_REF_2V56 = 0x03 | + | |
| - | } | + | |
| - | adc_reference; | + | |
| - | typedef enum | + | The following functions of the library are provided to support the ATmega2561 ADC converter. |
| - | { | + | |
| - | ADC_PRESCALE_2 | + | |
| - | ADC_PRESCALE_4 | + | |
| - | ADC_PRESCALE_8 | + | |
| - | ADC_PRESCALE_16 | + | |
| - | ADC_PRESCALE_32 | + | |
| - | ADC_PRESCALE_64 | + | |
| - | ADC_PRESCALE_128 = 0x07 | + | |
| - | } | + | |
| - | adc_prescale; | + | |
| - | // | + | ~~CL~~ |
| + | |||
| + | <code c> | ||
| // Starting the ADC | // Starting the ADC | ||
| - | // | ||
| void adc_init(adc_reference reference, adc_prescale prescale) | void adc_init(adc_reference reference, adc_prescale prescale) | ||
| { | { | ||
| Line 56: | Line 42: | ||
| } | } | ||
| - | // | ||
| // Converting the values of selected channel | // Converting the values of selected channel | ||
| - | // | ||
| unsigned short adc_get_value(unsigned char channel) | unsigned short adc_get_value(unsigned char channel) | ||
| { | { | ||
| Line 77: | Line 61: | ||
| } | } | ||
| </ | </ | ||
| - | The function // | ||
| - | In previously explained example program analogue-digital converter and 7 segment number indicator library are used. The 10 bit value of analogue-digital converter is multiplied by 10 and divided by 1024 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. | + | 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> | <code c> | ||
| - | // | ||
| // Example program of potentiometer on the Sensor module | // Example program of potentiometer on the Sensor module | ||
| // The position of the potentiometer is displayed on the 7-segment indicator | // The position of the potentiometer is displayed on the 7-segment indicator | ||
| - | // | ||
| #include < | #include < | ||
| #include < | #include < | ||
| - | // | + | // Robotic HomeLab II |
| - | // Selecting the channel | + | //#define ADC_CHANNEL 3 |
| - | // | + | |
| - | // 1 = photoresistor | + | // Robotic HomeLab III |
| - | // 2 = thermistor | + | #define ADC_CHANNEL |
| - | // 3 = potentiometer | + | |
| - | // | + | |
| - | #define ADC_CHANNEL | + | |
| - | // | ||
| // Main program | // Main program | ||
| - | // | ||
| int main(void) | int main(void) | ||
| { | { | ||
| Line 110: | Line 87: | ||
| // Adjusting ADC | // Adjusting ADC | ||
| - | adc_init(ADC_REF_AVCC, | + | adc_init(ADC_REF_AVCC, |
| // Endless loop | // Endless loop | ||
| - | while (true) | + | while (1) |
| { | { | ||
| // Reading 4 times rounded values of the channel | // Reading 4 times rounded values of the channel | ||
| Line 119: | Line 96: | ||
| // Displaying the hundreds of the indicated value | // Displaying the hundreds of the indicated value | ||
| - | segment_display_write(value * 10 / 1024); | + | // Robotic HomeLab II |
| + | //segment_display_write(value * 10 / 1024); | ||
| + | // Robotic HomeLab III | ||
| + | segment_display_write(value * 10 / 2048); | ||
| } | } | ||
| } | } | ||
| </ | </ | ||