Troubleshooting ADC coding for Arduino Uno

Discussion in 'Embedded Systems and Microcontrollers' started by Papasquat12, Jan 29, 2012.

  1. Papasquat12

    Thread Starter New Member

    Jan 29, 2012
    3
    0
    I am trying to code the Arduino Uno to run an analog to digital conversion. I have a pot connected to the A0 pin and am simply trying to input the analog value it reads from A0 and the digital output into the serial monitor. I have troubleshooted this quite a bit and am not getting anything on the serial monitor. Sorry I have to do this but here is my code, if you see my problems can you please help. Thanks
    Code ( (Unknown Language)):
    1. [INDENT][LEFT] /*  ADC Test
    2.     For an Atmega 48 88 or 168
    3.     Sends ADC results to the serial port
    4.     Set your terminal to 2400 N 8 1
    5.  
    6.     Atmega168 DIP TX   PD1 (pin3)
    7.     Atmega168 DIP RX   PD0 (pin2)
    8.     Atmega168 DIP ADC2 PC2 (PIN25) */
    9.  
    10. #define F_CPU 1000000UL
    11. #define UBRR_1200 51
    12. #define UBRR_2400 25        // for 1Mhz
    13.  
    14. const int analogInPin = A0; // Analog input pin that the potentiometer is attached to
    15. int sensorValue = 0; // value read from the pot
    16. int inputValue = 0; // value output to the PWM (analog out)
    17. int adc_temp = 0; // value read from the pot
    18.  
    19. // #define UBRR_2400 207    // for 8Mhz with .2% error
    20. // #define UBRR_9600 51     // for 8Mhz with .2% error
    21. // #define UBRR_19200 25    // for 8Mhz with .2% error
    22.  
    23. #include <avr/io.h>
    24. #include <util/delay.h>
    25. #include <stdio.h>
    26.  
    27. void read_adc(void);        // Function Declarations
    28. void adc_init(void);
    29.      void setup() {   // initialize serial communications at 9600 bps:
    30.       Serial.begin(9600);
    31.  
    32.       adc_temp = analogRead(analogInPin);
    33.       inputValue = map(adc_temp, 1 , 1023, 1, 4);
    34.       Serial.print("ADC = " );
    35.       Serial.print(inputValue);
    36.    }
    37.  
    38.  
    39. int main(void)
    40. {
    41.  
    42.    adc_init();             // Initialize the ADC
    43.  
    44.  
    45.    _delay_ms(125);         // wait a bit
    46.  
    47.    while(1)
    48.    {    
    49. [/LEFT]
    50. [/INDENT][INDENT]      read_adc();
    51.  
    52.  
    53.      _delay_ms(125);        // wait a bit
    54.  
    55.    }
    56. }
    57.  
    58. /* INIT ADC */
    59.  
    60. void adc_init(void)
    61. {
    62.  
    63.    /** Setup and enable ADC **/
    64.    ADMUX = (0<<REFS1)|    // Reference Selection Bits
    65.            (1<<REFS0)|    // AVcc - external cap at AREF
    66.            (0<<ADLAR)|    // ADC Left Adjust Result
    67.            (0<<MUX2)|     // Analog Channel Selection Bits
    68.            (1<<MUX1)|     // ADC2 (PC2 PIN25)
    69.            (0<<MUX0);
    70.  
    71.    ADCSRA = (1<<ADEN)|    // ADC ENable
    72.            (0<<ADSC)|     // ADC Start Conversion
    73.            (0<<ADATE)|    // ADC Auto Trigger Enable
    74.            (0<<ADIF)|     // ADC Interrupt Flag
    75.            (0<<ADIE)|     // ADC Interrupt Enable
    76.            (1<<ADPS2)|    // ADC Prescaler Select Bits
    77.            (0<<ADPS1)|
    78.            (1<<ADPS0);
    79.                    // Timer/Counter1 Interrupt Mask Register
    80.     TIMSK1 |= (1<<TOIE1); // enable overflow interrupt
    81.  
    82.  
    83.     TCCR1B |= (1<<CS11)|
    84.            (1<<CS10);     // native clock
    85. }
    86.  
    87. /* READ ADC PINS */
    88.  
    89. void read_adc(void)
    90. {
    91.    unsigned char i = 4;
    92.    adc_temp = 0;
    93.    while (i--)
    94.    {
    95.       ADCSRA |= (1<<ADSC);
    96.       while(ADCSRA & (1<<ADSC));
    97.       adc_temp+= ADC;
    98.       _delay_ms(50);
    99.    }
    100.    adc_temp = adc_temp / 4;  // Average a few samples
    101.  
    102.     Serial.print("conversion = " );
    103.       Serial.print(adc_temp);
    104.       delay(10);
    105.  
    106. }
    107.  
    108. [/INDENT]
     
    Last edited by a moderator: Jan 30, 2012
  2. hgmjr

    Moderator

    Jan 28, 2005
    9,030
    214
    What programming tools are you using to program your arduino uno?

    hgmjr
     
  3. hgmjr

    Moderator

    Jan 28, 2005
    9,030
    214
    It should be as simple as this if you use the bootloader that comes with the arduino uno to program it.

    hgmjr
     
  4. Papasquat12

    Thread Starter New Member

    Jan 29, 2012
    3
    0
    I'm using the software from the arduino home page, arduino 1.0. Just trying to get the digital output to the serial monitor.
     
  5. hgmjr

    Moderator

    Jan 28, 2005
    9,030
    214
    Your code looks like you are not using the built-in boot loader.

    hgmjr
     
  6. Papasquat12

    Thread Starter New Member

    Jan 29, 2012
    3
    0
    I have run a similar code to just make sure the board is reading the analog input, which looks like:

    void setup() {
    Serial.begin(9600);
    }

    void loop() { //read the analog input into a variable
    int analogValue = analogRead(0); //print the result
    Serial.println(analogValue);
    delay(10); // wait 10 ms for ADC
    }

    It is sending this to the serial monitor and I am not using the burn boot loader, so I don't know if I need it.
     
Loading...