Microcontroller programm for reading analog inputs

microcontroller programm for reading analog inputs

Hello newbieeee

Welcome to worldbest Arduino forum.

What is your question?

how can i programm to read out analog inputs on a microcontroller

Hello newbieeee

Take here a view to gain the knowledge.

Download the Arduino IDE and look under examples. :wink:

1 Like

Which Arduino you are using -- UNO, NANO or MEGA?

i have a UNO

1. This (Fig-1) is the internal details for the ADC of ATmega328P Microcontroller of Arduino UNO Board.


Figure-1:

2. Programming of ADC Module by acquiring and displaying 3.3 V of UNO Board
(1) Let us use a jumper and connect 3.3V pin with A4-pin of UNO Board and then upload the following Arduino Codes based sketch into UNO. Observe that the OutputBox of the Serial Monitor shows a value close to 3.3V. In this program, the EOC (End-of-Conversion) status is being polled implicitly which means that EOC is monitored during the execution of analogRead() function.

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

void loop() 
{
  unsigned int y = analogRead(A4); 	//y = 16-bit numerical vale with upper six 0s
  float testVolt = (5.0/1023)*y; 	//for input of 5V, ADC value = 1023 (all 1s)
  Serial.println(testVolt, 1);  		//show 1-digit after decimal point
  delay(1000);  							//test interval
}

(2) Let us use a jumper and connect 3.3V pin with A4-pin of UNO and then upload the following Mixed Codes (Register Level Codes + Arduino Codes) based sketch. Observe that the OutputBox of the Serial Monitor shows a value close to 3.3V. In this program, the EOC status is being polled (check again and again) explicitly. Consult Fig-2 for registers’ bits.

void setup()
{
  Serial.begin(9600);
  ADMUX = 0;    //reset
  ADCSRA = 0;   //reset
  ADMUX |= (1 << REFS0) | (1 << MUX2);         				//Vref = 5V, Ch-4 select
  ADCSRA |= (1 << ADEN);                       				//ADC is enabled
  ADCSRA |= (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0); 	//clkADC = 125 kHz
}

void loop()
{
  ADCSRA |= (1 << ADSC);           //start ADC
  while (bitRead(ADCSRA, ADSC) != LOW)
  {
    ;     //wait until conversion is done
  }
  unsigned y = ADCW;
  float testVolt = (5.0 / 1023) * y; //for input of 5V, ADC value = 1023 (all 1s)
  Serial.println(testVolt, 1);       //show 1-digit after decimal point
  delay(1000);                       //test interval
}


Figure-2:

(3) Let us use a jumper and connect 3.3V pin with A4-pin of UNO and then upload the following Mixed Codes (Register Level Codes + Arduino Codes) based sketch into UNO. Observe that the OutputBox of the Serial Monitor shows a value close to 3.3V. In this program, the EOC status is being sensed by interrupt. Consult Fig-2 for registers’ bits.


volatile bool flag = false;

void setup()
{
  Serial.begin(9600);
  ADMUX = 0;    //reset
  ADCSRA = 0;   //reset
  ADMUX |= (1 << REFS0) | (1 << MUX2);         //Vref = 5V, Ch-4 select
  ADCSRA |= (1 << ADEN);                       //ADC is enabled
  ADCSRA |= (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0); //clkADC = 125 khz
  ADCSRA |= (1 << ADIE); //ADC interrupt logic is enabled
  interrupts();   //enable global interrupt logic
  ADCSRA |= (1 << ADSC); //start ADC
}

void loop()
{
  if (flag == true)
  {
    noInterrupts(); //protecting critical sectionby disabling interrupt
    unsigned y = ADCW;
    float testVolt = (5.0 / 1023) * y; //for input of 5V, ADC value = 1023 (all 1s)
    Serial.println(testVolt, 1);       //show 1-digit after decimal point
    flag = false;
    interrupts();   						//interrupt is enabled
    delay(1000);                       //test interval
  }
}

ISR(ADC_vect)
{
  flag = true;
  ADCSRA |= (1 << ADSC); //start ADC again
}
1 Like

Just like this:-

3 Likes

...that are deliberately encapsulated by the Arduino interface, so that a beginner does not need to know at all.

2 Likes

Arduino Forum is a Microcontroller Learning Place; where, an instruction/code activates a piece of hardware; therefore, presentation of architecture of the relevant hardware is a logical decision. Some of the readers might find it useful if he looks for the mechanism of switching to different voltage levels of VREF point or for the source of 125 kHz (default) conversion clock frequency of the ADC. Exposure to the hardware helps better understanding of the system and hence effective trouble shooting and debugging.

@GolamMostafa

Well I found your examples interesting and informative.

However if you read this (link below) you will see why this line is wrong

feel free to message me if you dont understand.

1 Like

I agree generally, however there is such a wide gap between what you posted, and what the OP understands, that it will likely be ignored by them. Other readers might benefit from a lesson on Atmel ADC internals, but it's unlikely that they would encounter this post at the exact stage of learning where it is useful to them. More likely, such a person is researching it independently and could only benefit from it, by starting or joining a thread that has some direct connection with the full understanding of the underlying hardware.

My constructive suggestion to you, is to re-write it in the form of a tutorial, and post it in the tutorials sub forum.

1 Like

The line is not wrong in the context of 1023; rather, it could be incorrect or debated.

This is an constructive criticism. There could be logical mistakes which I will look into; however, the sketch runs well and shows the expected output.

If you multiply a float number with an integer number, what would you expect -- certainly a float number?

In the following diagram (extracted from your referred link) which I studied 47 years ago in 2nd year class in a standard Text Book, do you agree that the comparator has a 1-bit uncertainty? If yes, then 1023 or 1024 is a long debated issue.

That's why the veteran posters are here to bridge the gap through constructive evaluation of the post.

There are three different sketches (in post #8) for the same tasks in which the first one certainly does not require knowing the architecture of the ADC as the analogRead() function has insulted almost everything; but, the remaining two register based sketches required the presentation of the ADC hardware to satisfy the curiosities of >50% pupil of 150 population.

It is not long debated, if you read the data sheet of the processor, it says it should be 1024.

1 Like

The following is an excerpt from a standard Text Book which is at the side of 1023.

Actually, zero could be the correct value being read by the AD converter. The text is referring to INTERVALS, ignoring the possibility that zero is valid.

1 Like

I have a simple intuitive reasoning as follows:

When analog input to a 10-bit ADC is:

0V, the ADC produces:                     00 0000 0000 = 0 decimal
5V (Full Scale = Vref), the ADC produces: 11 1111 1111 = 1023 decimal
Vin (unknown voltage < Full Scale), the ADC produces: (1023/5)*Vin = analoRead(AX)

Which is wrong.

The data sheet for the processor says:-

So sorry but the Data sheet is the authoritative last word on the topic.

No matter what you may think is correct.
No matter what you think is intuitive.
No matter what hoops you turn back somersaults through.

The data sheet for a part is always how that part should be treated, end of.

1 Like