Issue reading Voltage from ANALOG Input on ARDUINO

It just shows 2.56V Or volts the whole time on the serial port regardless of if I have anything connected or not....

What Im attempting to do: If anyone's wondering basically I have a laser tag gun and there are 3 health bars... When your life goes down to 0 or the last light goes off; I want my Arduino to detect the drop in voltage and do "DO SOMETHING HERE". The Arduino AtMega isn't detecting any voltage changes exactly but instead just shows 2.56Volts the whole time rather something is connected or not. With the code It shouldn't give me a value if nothing is connected to an AO Pin yet it gives me 2.56v regardless.

Physical: I have a jumper spliced in between and going to a pin A8 shouldn't that be enough? Or is it my code (I doubt)

Others have used extremely similar codes and have had proper results.... Even still If I connect an AA battery it shows 1.31V if I connect the hot side then when I connect the ground to the battery it goes right back to 2.56v idk the problem i'm a bit lost.

/*
* Display5vOrless sketch
* prints the voltage on analog pin to the serial port
* Warning - do not connect more than 5 volts directly to an Arduino pin.
*/

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

void loop()
{
  int sensorValue = analogRead(A8);
  float voltage = sensorValue * (5.0 / 1023.0); // calculate the ratio
  Serial.print(voltage); // print the value in volts
  Serial.println ("V");
  delay(500);
  }

The code is fine so it's most likely a wiring error. Photo or schematic? Can't fix what we can't see.

Trust me I hear you on that but if I have nothing but the arduino plugged into my computer it does the same thing.

If you don't have anything connected to an analog pin then that explains why you can't measure anything sensible on that pin.

If you do have something wired up then show us exactly what it is and how it's connected.

Steve

I don't think you're understanding... If I have my Arduino plugged into my computer and NO OTHER WIRES going anywhere else I get 2.56V. I have 1 wire going into the arduino and that is the USB absolutely nothing else meaning because I have no power going to the Anolog pins I should be getting a reading o 0.00V but I do not get that I get 2.56v

A8 ????? Err 0 to what? when did the mega get 9 analog inputs?

Mark

It's a mega 2560 that was a booboo by me but it doesn't matter what pin I assign to be the pin to read voltage

With the code It shouldn't give me a value if nothing is connected to an AO Pin yet it gives me 2.56v regardless.

The inputs are very-high impedance and can "float" to anything between 0 and 5V. ....It's kinda' nice that it's floating at the mid point! :D :D :D.

If you want to read zero with "nothing connected" add a pull-down resistor (a resistor between the analog input and ground). The value of the resistor depends on the impedance of whatever is driving it.

Connect a wire between the 3.3volt pin and A8. Then pin A8 isn't 'floating' anymore, and you should get a reading of about 3.3volt. Leo..

Im not too too worried about the "Floating" also thanks so so much you guys for that it will defo matter later buuuuuuut what about the issue of not getting a reading at all..

For example even if I do receive any type of "floating #" why when I connect a battery to any of the Analog pins on both my Arduino UNO or Arduino 2560 It doesn't detect any changes as all.

I even tried this much simpler code of which again there is still no problem with

int sensePin = 0;

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


void loop() {
  // put your main code here, to run repeatedly:
  Serial.println((analogRead(sensePin) / 238.0)); // print the value in volts


  delay(500);
}

CRAYONS: For example even if I do receive any type of "floating #" why when I connect a battery to any of the Analog pins on both my Arduino UNO or Arduino 2560 It doesn't detect any changes as all.

How do you connect it? Are you connecting the grounds?

Yeap the most simplistic test would be using a battery with only ground and a hot wire such as what's in this video https://www.youtube.com/watch?v=EcUNLAD1CkA With that same test I got no change in Voltage and to make my life harder I actually got a reading of constant voltage when there was nothing connected... same code i'm 100% positive not a single line of code was off I Copy pasted his exact code and even attempted typing it in letter for letter/space for space.

I could record a video for you guys to show you proof that I have nothing connected to either of my boards and when I do connect a working battery (Several) still nothing happens (same results). It'd be pointless tho considering that. Also I'm positive my wires work and much more like it just isn't making sense..

At any point why would I not get the same results of another person if I'm copying exactly as is.

Hi, Can you run this code on your mega.

And; Using Analog input A0.

  • connect A0 to gnd and tell us what your monitor shows.
  • connect A0 to 5V pin and tell us what your monitor shows.
  • connect A0 to 3.3V pin and tell us what your monitor shows.
int analogPinA0 = A0;
int rawA0;
void setup()
{
  analogReference(DEFAULT);
  Serial.begin(9600);
}


void loop() {
  rawA0 = analogRead(analogPinA0);
  // put your main code here, to run repeatedly:
  Serial.print(rawA0);
  Serial.print("  ");
  Serial.println((float)rawA0 / 238.0); // print the value in volts
  delay(500);
}

Thanks... Tom... :)