# How can I get a linear relationship between a voltage divider and an ADC?

I'm making a small level meter using an ATtiny microcontroller. It uses a linear 5K potentiometer as a voltage divider coupled to an ADC pin. So far it's working except the digital values don't seem to correlate well with the measured voltage.

The schematic: simulate this circuit – Schematic created using CircuitLab

The relevant code:

#define F_CPU 1000000
#include <avr/io.h>
#include <util/delay.h>

{

// Set ADC reference to AVCC

// Prescaler /8, 125kHz ADC sample rate @ 1MHz

// Auto trigger enable

// Use input channel ADC2 (PB4)
ADMUX |= (1 << MUX1) | (0 << MUX0);

// Start converting
}

int main(void)
{
DDRB = 0x07;
PORTB = 0x00;

while(1)
{

// output A
// output B
// output C
else
// output D
}
}


I measured the voltage at PB4, and noted the points at which the output changed:

• Minimum: 196 mV
• 64: ~235 mV
• 128: ~500 mV
• 192: ~770 mV
• Maximum: 2.849 V

I expected approximately 1.5 V to be where a digital value of 128 is obtained, not 500 mV.

If I understand correctly, the ATtiny13A has a 10-bit ADC, the values from which are stored in two registers, ADCH and ADCL. With ADLAR (the left adjust) enabled, the value is shifted into the ADCH register and effectively two bits are discarded.

The datasheet indicates:

If the result is left adjusted and no more than 8-bit precision is required, it is sufficient to read ADCH.

I'm confused about why I am getting a nonlinear conversion on the ADC. Can you explain what I may be overlooking?

You state that at 128, you measure about 500mV. This means at full scale (256) you measure about 1V.

According to the datasheet (14.12) with:

// Set ADC reference to AVCC


You actually do the following:

This bit selects the voltage reference for the ADC, as shown in Table 14-2. If this bit is changed during a conversion, the change will not go in effect until this conversion is complete (ADIF in ADCSRA is set).

Table 14-2.

Voltage Reference Selections for ADC
REFS0   Voltage Reference Selection
0       VCC used as analog reference.
1       Internal Voltage Reference.


Paragraph 14.3 states:

The ADC converts an analog input voltage to a 10-bit digital value through successive approximation. The minimum value represents GND and the maximum value represents the voltage on VCC or an internal 1.1V reference voltage.

So in conclusion, you are setting the ADC reference voltage to 1.1V and with that knowledge your findings are pretty accurate.

• This is exactly it. Thanks for elaborating not only on what setting is wrong, but what it's doing which explains the measurements. Oct 22 '13 at 6:40

The comment doesn't match the code.

// Set ADC reference to AVCC
{"ft_lang_label":"__label__en","ft_lang_prob":0.7540624,"math_prob":0.83644897,"size":1795,"snap":"2021-31-2021-39","text_gpt3_token_len":533,"char_repetition_ratio":0.10999442,"word_repetition_ratio":0.0,"special_character_ratio":0.33593315,"punctuation_ratio":0.125,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9846638,"pos_list":[0,1,2,3,4],"im_url_duplicate_count":[null,3,null,3,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-09-16T16:21:47Z\",\"WARC-Record-ID\":\"<urn:uuid:d97ebc9f-3c6b-451e-a56b-5ea8712c08a3>\",\"Content-Length\":\"178983\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:d01707ad-ee3b-4618-bb36-106422dc6974>\",\"WARC-Concurrent-To\":\"<urn:uuid:974de558-9e5c-450d-9fe4-b6e6ed59ee9b>\",\"WARC-IP-Address\":\"151.101.193.69\",\"WARC-Target-URI\":\"https://electronics.stackexchange.com/questions/86160/how-can-i-get-a-linear-relationship-between-a-voltage-divider-and-an-adc\",\"WARC-Payload-Digest\":\"sha1:WVGFVSIUSTNK2VFFW2QNEJFYDQMBTIRL\",\"WARC-Block-Digest\":\"sha1:TESYWF4YHWTXDH7MDAY2Z3TBT47TKIPA\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-39/CC-MAIN-2021-39_segments_1631780053657.29_warc_CC-MAIN-20210916145123-20210916175123-00612.warc.gz\"}"}