TransWikia.com

AnalogRead always 1023 on Arduino Due?

Arduino Asked by Littlegator on January 25, 2021

Just using the Basic Example AnalogReadSerial.

// the setup routine runs once when you press reset:
  void setup() 
{
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop() 
{
  // read the input on analog pin 0:
  int sensorValue = analogRead(A0);
  // print out the value you read:
  Serial.println(sensorValue);
  delay(1);        // delay in between reads for stability
}

I have a basic voltage divider set up between two 10K resistors from 5V to GND. I have verified with a DMM that the DC voltage at pin A0 is 2.5V. Grounding the A0 pin has no effect.

It also seems that enabling either ADC channel 7 (Arduino pin AD0) or enabling every channel, putting the ADC in free-running mode, and deliberately setting it to not sleep has no effect:

ADC->ADC_CHDR = 0;
ADC->ADC_CHER |= 0xFF;
ADC->ADC_MR |= 0b10000000;
ADC->ADC_MR &= 0b11011111;

Also, by running the sketch DigitalInputPullup, I can get the LED on pin 13 to toggle and serial communication to work as I tie the pin to ground, demonstrating that the board is being programmed properly.

I guess I’m just stumped. Should be the simplest thing in the world and it’s just not working.

EDIT:
This is my attempt at manually controlling the ADC:

volatile unsigned long value;

void setup() 
{
  //ADC setup
  ADC->ADC_WPMR &= 0xFFFE; //disable write protect
  ADC->ADC_CHER = 0b100; //Enable AD2     |
  ADC->ADC_MR &= 0b11111111000000001111111100000000;
  ADC->ADC_MR |= 0b00000000000100100000000000000000; //software trigger, hi res, no sleep, not free running
  ADC->ADC_IER = 0b100; //enable interrupt
  ADC->ADC_IMR = 0b100; //enable interrupt in mask

  ADC->ADC_CR |= 0b10; //start first conversion
  Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop() 
{
  ADC->ADC_CR |= 0b10; //start conversion
  while (!(ADC->ADC_ISR & 0b100)); //wait for conversion to end 

  for (int i = 0; i <= 15; i++)
 { 
    value = ADC->ADC_CDR[i]; //read conversion data for all channels
    Serial.println(value);    
 }

 Serial.println();
 Serial.println();
 Serial.println();

 delay(3000);
}

Just doesn’t seem to be doing anything. ADC_CHSR (the ADC channel status register) shows channel 2 and only channel 2 enabled and conversions are starting and completing, yet now the conversion data is always 2048. Here’s the serial output of all 15 channels, now in 12 bit mode:

0
0
4095
0
0
0
0
0
0
0
0
0
0
0
0
0



0
0
4095
0
0
0
0
0
0
0
0
0
0
0
0
0

6 Answers

I've abandoned all hope and decided that the ADC is just bad.

Correct answer by Littlegator on January 25, 2021

Answer to a 6 years old thread. Hope this will help others :)

Looks like you have been using an external power source and only connected the positive core to the pin. If you had an external power source, did you connect the external power source GND to Arduino GND?

Answered by Sherjeel Ton on January 25, 2021

Try adding this line in your void setup(), also disconnect AREF pin from any kind of bias,

analogReference(INTERNAL1V1); 

Answered by S Dube on January 25, 2021

Try to force sensorValue at the beginning of the void loop to zero, put short delay before reading the analogue pin. I tried this and works for me. Unless your prgram will misbehave at zero sensor value at every loop start. The will No need of initializing A0 as input because they are input by default.

Answered by Vaso on January 25, 2021

If you have AREF pin pulled out onto the shield board not connected to anything, and you don't declare analogReference in your code (thus keeping it as the default internal 5V), it could still cause you to have the 1023 symptom you are describing. My suggestion: cut the AREF connecting pin, or put a constant 5v onto the AREF line.

Answered by Andrew Wang on January 25, 2021

You need to add this to your setup():

pinMode(A0, INPUT);

...to set the pin for reading.

Since you're reading an input that's already pulled up (in the middle of a voltage divider), that should be all. However, you can turn on the internal pull-up with:

digitalWrite(A0, HIGH);

...while the pin is in input mode.

More details here:

http://arduino.cc/en/Reference/PinMode

http://arduino.cc/en/Tutorial/AnalogInputPins

Answered by sburlappp on January 25, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP