Author Options:

ADC conversion error in code ATMEGA328p? Answered

I am trying to read values from ADC0 on the ATMEGA328p. The values expected are between 0-5v. This is due to ADC0 being connected to a potentiometer connected to the 5v output of a Xplained mini. I am getting either 0v or 5v usually. With no variation when the potentiometer is changed.

Here is the date sheet of atmega328p

I have looked at multiple ADC examples and tutorials online but cant find the error in my code.

void adc_initialise (){
    //set vref to AVcc, channel selection is initially ADC0 as wanted
    ADMUX |= (1<<6);
    //set ADC enable, Set adc clock prescalar 64
    ADCSRA |= (1<<7)|(1<<2)|(1<<1);

uint16_t adc_read (){
    ADCSRA |= (1<<6); // start conversion
    while( ADCSRA & (1<<ADSC) ); //wait until conversion is complete
    return ADCW;

float adc_calculation(uint16_t adcValue){
    float stepSize = (5.0/1024.0);
    float voltageIn = adcValue*stepSize;
    return voltageIn;  

then in my main i have

    uint16_t adcValue = adc_read();
    float voltageIn = adc_calculation(adcValue);
    adcConverterToUART(voltageIn);//I know that this part of the code is working as I have hardcoded many test values and all have transmitted correctly.

And as mentioned above I know the error is not in my UART code but somewhere in the above ADC code. Cheers in advance for any help.