AnalogRead() giving random results

Morning, I am facing an issue with analogRead() giving floating results.
I have a steady 2.5V input into Pin A0 set pinMode to INPUT and convert to volts using 5.0 / 1023 but it returns random results. Measuring the input it gives the correct voltage but not displaying the correct voltage from analogRead().

#define voltRead A0
float convert = 5.0 / 1023;

void setup(){
  pinMode(voltRead, INPUT);
}

void loop(){
 float result = analogRead(voltRead) * convert;
 display(result);   // removed code for simplicity on here, display is a seperate function to display on OLED
}

What do you see if you print the raw value ?

Please post your complete sketch rather than leaving us in the dark about what the display() function does, what data type it expects and returns etc

/*   

REMOVED BY HOST

 */

Here's the sketch, hope this helps, Many Thanks

it gives me random raw values if i remove the conversion

the 2.5V source is steady when i continuosly measure the input into A1

I don't know if it makes any difference, but you don't need to set the pinMode() when you use analogRead(). pinMode() is intended to be used with digital pins or analogue pins being used as digital pins. The analogue pins default to being inputs, as do the digital pins

What range of raw values do you get with a steady 2.5 volt input ?

I hope that it goes without saying that you should check your wiring carefully. Are you using a breadboard and if so can you rely on it ? Does it have split power rails or do they go right along the length of the breadboard with no central gap ?

I originaly didn't have pinMode() but added just to see if made a difference....it didn't.

Raw values range from 0 to 1023 and are never the same each time.

It's a printed circuit which has been tried and tested multiple times and i can rely on it be stable everytme :slight_smile:

For these lines

float val = (analogRead(voltRead) * voltConversion) * 100;

Can you make the analogRead a separate action and Serial.println the result, confirm you are getting a good reading before the conversion?

Thanks for the reply, even without the conversion i still get random readings.
I've tried changing the Pin through A0-7 with no change.
I'm using an ATmega2560 which shouldn't really matter but before anyone asks

Any chance you are missing a Gnd connection somewhere?

All my grounds are connected, double checked.

FIXED: Something I probably should have started with. Swapped out the Arduino for another and seems to work now. Possible broken Arduino track or bad joint on the board, but hey, works now :):slight_smile:

Appreciate the help from all :slight_smile: