Go Down

Topic: Problem reading ADC channels (Read 85 times) previous topic - next topic

cois

Hi There
I am currently using the Arduino UNO to read three ADC channels.
I am making use of the Arduino IDE but not the analogRead() function.

The code works well, but my problem is, the order in which the data is being printed to the serial monitor differs from the order of the code, e.g. instead of displaying 0-1-2 it displays 2-0-1.  The last value is always being printed first, irrespective of the order

Any advice please.
Francois

void loop() {

 
  ADMUX = 0b11100000;  // select channel 0
  delay(10);      //delay 1 second
  ADCSRA |= (1 << ADSC);         //start single conversion
  analogData = ADCH;             //store data in analogData variable
  float volt = (analogData/255.0)*1.1;
  Serial.print(volt);
  Serial.print("    ");
 
      ADMUX = 0b11100001;  // select channel 1
      delay(10);      //delay 1 second
      ADCSRA |= (1 << ADSC);         //start single conversion
      analogData = ADCH;             //store data in analogData variable
      float volt1 = (analogData/255.0)*1.1;
      Serial.print(volt1);
      Serial.print("    ");
           
           ADMUX = 0b11100010;  //select channel 2
            delay(10);      //delay 1 second
            ADCSRA |= (1 << ADSC);         //start single conversion
            analogData = ADCH;             //store data in analogData variable
            float volt2 = (analogData/255.0)*1.1;
            Serial.println(volt2);
           
        delay(1000);      //delay 1 second   
}

UKHeliBob

Quote
Any advice please.
First post all of your program after Auto formatting it in the IDE and use code tags when you post it.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

cois

Full code.

Code: [Select]

//8 bit adc, 3 channel

#include <avr/io.h>   //allows for register commands to be understood
int analogData;       //declare analogData variable

void setup() {
  DDRC &= ~(1 << DDC0);
  DDRC &= ~(1 << DDC1);
  DDRC &= ~(1 << DDC2);
  // ADMUX |= (1 << REFS0);   //sets reference voltage to internal ref
  // ADMUX |= (1 << REFS1);   //sets reference voltage to internal ref
  //ADMUX |= (1 << ADLAR);   //left adjusts for 8-bit resolution

  //binary format only allowed in setup because of
  //once off setup
  ADMUX = 0b11100000;   //sets 1.1V, sets ADC0 as input channel,
  //and left adjusts


  //binary format only allowed in setup because of
  //once off setup
  ADCSRA = 0b10000111;  //turn on the ADC, keep ADC single conversion mode
  //set division factor-8 for 125kHz ADC clock
  Serial.begin(9600);   //start Serial Interface
  DIDR0 = 0x07;  // disable digital input buffers on ch0 to ch2

}

void loop() {


  ADMUX = 0b11100000;  // select channel 0
  delay(10);      //delay 10 milli second
  ADCSRA |= (1 << ADSC);         //start single conversion
  analogData = ADCH;             //store 8 bit data in analogData variable
  float volt = (analogData / 255.0) * 1.1;
  Serial.print(volt);
  Serial.print("    ");

  ADMUX = 0b11100001;  // select channel 1
  delay(10);      //delay 10 milli second
  ADCSRA |= (1 << ADSC);         //start single conversion
  analogData = ADCH;             //store 8 bit data in analogData variable
  float volt1 = (analogData / 255.0) * 1.1;
  Serial.print(volt1);
  Serial.print("    ");

  ADMUX = 0b11100010;  //select channel 2
  delay(10);      //delay 10 milli second
  ADCSRA |= (1 << ADSC);         //start single conversion
  analogData = ADCH;             //store 8 bit data in analogData variable
  float volt2 = (analogData / 255.0) * 1.1;
  Serial.println(volt2);

  delay(1000);      //delay 1 second
}




outsider

#3
Feb 14, 2019, 03:39 pm Last Edit: Feb 14, 2019, 03:41 pm by outsider
Try this:
Code: [Select]

void loop() {

  ADMUX = 0b11100000;  // select channel 0
  delay(10);      //delay 10 milli second
  ADCSRA |= (1 << ADSC);         //start single conversion
  analogData = ADCH;             //store 8 bit data in analogData variable
  float volt = (analogData / 255.0) * 1.1;
  Serial.print("V  ");
  Serial.print(volt);
  Serial.print("    ");

  ADMUX = 0b11100001;  // select channel 1
  delay(10);      //delay 10 milli second
  ADCSRA |= (1 << ADSC);         //start single conversion
  analogData = ADCH;             //store 8 bit data in analogData variable
  float volt1 = (analogData / 255.0) * 1.1;
  Serial.print("V1  ");
  Serial.print(volt1);
  Serial.print("    ");

  ADMUX = 0b11100010;  //select channel 2
  delay(10);      //delay 10 milli second
  ADCSRA |= (1 << ADSC);         //start single conversion
  analogData = ADCH;             //store 8 bit data in analogData variable
  float volt2 = (analogData / 255.0) * 1.1;
  Serial.print("V2  ");
  Serial.println(volt2);

  delay(1000);      //delay 1 second
}

MarkT

You must wait for the conversion to complete, it will not be available until the ADSC bit goes
low in the ADCSRA register:
Code: [Select]

  ADCSRA |= (1 << ADSC);         //start single conversion
  while (ADCSRA & (1<<ADSC)) {}  // wait for completion
  analogData = ADCH;             //store 8 bit data in analogData variable


You can look to see how this is done in the source code for analogRead for instance (you have the
sources, its an open-source project)...
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

cois

Mark Thanks

Problem Solved

Cois

Go Up