0

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
  • Have you tried the same on another analog input pin? – jfpoilpret Jun 5 '14 at 5:03
  • Yup, I've tried all of them. – Littlegator Jun 5 '14 at 12:42
2

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

  • Could've sworn I needed something like this, but it wasn't in the example or the code on the website. However, it's still outputting only 1023. – Littlegator Jun 4 '14 at 19:59
  • Try connecting that pin to ground. If you still get 1023, you might have a damaged chip. – sburlappp Jun 6 '14 at 16:13
  • The value remains the same no matter what it is connected to. – Littlegator Jun 6 '14 at 16:30
  • It seems like a broken chip. – Handoko Jun 6 '14 at 23:57
  • You could have a short somewhere. – sburlappp Jun 7 '14 at 12:44
1

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.

0

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

0

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.

0

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

analogReference(INTERNAL1V1); 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for?Browse other questions tagged or ask your own question.