analogRead returns varying values when connected to GND/3.3V/5V

Hi,

I'm not quite sure this is the right place to post, please feel free to move if there's a better forum for it.

I'm trying to read an analogue voltages with my ArduinoMega but I can't get it right and I'm not sure where the problem lies. I've found many threads on similar topics but couldn't find one what would address my issue.

No matter how I connect (or not) the analogue input pins, my Arduino always returns input values in the range of 280 to 320 (more or less) and I don't understand why. Does anyone have any ideas?

Any help is appreciated.

My setup looks like this (just in case it's a setup problem):

For now I've connected

pin3 to GND
pin6 to 5V
pin10 to 3.3V

My expectation is that I'd read the following values:

pin2: random (pin is floating)
pin2: 0
pin6: 1023
pin10: 675 (+/- a few)

A sample output looks like this:

pin2: 319 ; pin3: 320 ; pin6: 321 ; pin10: 316 ; 
pin2: 324 ; pin3: 324 ; pin6: 325 ; pin10: 315 ; 
pin2: 319 ; pin3: 318 ; pin6: 316 ; pin10: 305 ; 
pin2: 309 ; pin3: 307 ; pin6: 303 ; pin10: 289 ; 
pin2: 294 ; pin3: 292 ; pin6: 288 ; pin10: 276 ; 
pin2: 284 ; pin3: 282 ; pin6: 279 ; pin10: 270 ; 
pin2: 282 ; pin3: 281 ; pin6: 280 ; pin10: 274 ; 
pin2: 286 ; pin3: 285 ; pin6: 285 ; pin10: 282 ; 
pin2: 295 ; pin3: 296 ; pin6: 298 ; pin10: 297 ; 
pin2: 311 ; pin3: 312 ; pin6: 315 ; pin10: 312 ; 
pin2: 320 ; pin3: 321 ; pin6: 323 ; pin10: 317 ; 
pin2: 323 ; pin3: 323 ; pin6: 324 ; pin10: 315 ; 
pin2: 320 ; pin3: 319 ; pin6: 318 ; pin10: 306 ; 
pin2: 311 ; pin3: 310 ; pin6: 306 ; pin10: 292 ; 
pin2: 298 ; pin3: 295 ; pin6: 291 ; pin10: 278 ; 
pin2: 286 ; pin3: 284 ; pin6: 281 ; pin10: 271 ; 
pin2: 282 ; pin3: 280 ; pin6: 278 ; pin10: 271 ;

I'm using the code below to read the voltage at the specific pins.

// Analogue input pins
const int Sense2 = 2;
const int Sense3 = 3;
const int Sense6 = 6;
const int Sense10 = 10;

void setup() {
  // put your setup code here, to run once:

  //define pinMode for analogue input
  pinMode(Sense2, INPUT);
  pinMode(Sense3, INPUT);
  pinMode(Sense6, INPUT);

  Serial.begin(9600);
  Serial.print("Read values ->");

  delay(1000);
}

void loop() {
  // put your main code here, to run repeatedly:

  String printstring1 = "";
  String printstring2 = "";

  printstring1 = "pin2: ";
  printstring2 = printstring1 + analogRead(Sense2) + " ; ";
  Serial.print(printstring2);

  printstring1 = "pin3: ";
  printstring2 = printstring1 + analogRead(Sense3) + " ; ";
  Serial.print(printstring2);

  printstring1 = "pin6: ";
  printstring2 = printstring1 + analogRead(Sense6) + " ; ";
  Serial.print(printstring2);

  printstring1 = "pin10: ";
  printstring2 = printstring1 + analogRead(Sense10) + " ; ";
  Serial.println(printstring2);

  delay(500);
}

When you say pin 2, 3, etc., are you referring to digital pin 2 or analog pin 2?

It looks like you are wiring the digital pins and then reading the analog input pins.

Note: You don't need to use pinMode() when using analogRead().

Hi All,

Weeeeeeeeeeeeeeeeee!!!!!!!!!!! It works :slight_smile:

John, you were right, I was using the digital pins instead of the analogue pins. Now I get the values I'm expecting :slight_smile:

I was aware that pinMode() is not required for analogRead() but I was getting desperate and had put it in just to make sure.

Thanks a million :slight_smile:

Cheers,
Tom