My Arduino mega 2560 pins are inputing random numbers

Hi, I have an arduino mega 2560. When I trying to take an input from a sensor (for example a voltage sensor) the pins of the arduino are inputing random numbers instead of inputing the output of the sensor. Even when I completely remove the sensor for the arduino it is still inputing random numbers istead of inputing 0. Any ideas on what may cause this?

Welcome to the forum.

When a pin is not connected, then it is "floating". It is picking noise and static electricity from the air. The impedance of a input pin is in the hundreds of GigaOhm.

What makes you think an unconnected input will read as 0?
Have a read of this for an explanation of the reason this is not the case: Buttons and other electro-mechanical inputs (introduction)

As for your sensors, without knowing what they are or how they are wired it's difficult to offer advice that isn't just guess work. Please post a schematic, details of what components you have and your code.

I forgot to mention that I had it working before a week or so... when I was disconnecting the sensor the input that I was reading in the Serial monitor was 0

Hi, I will read the post. Do you recommend some program or a site for me to make the schematic?

That is my code.

#define vpin A1

float vraw;
float vout;

void setup()
{
pinMode(vpin,INPUT);
Serial.begin(9600);
Serial.println("Voltage: ");
}
void loop()
{
vraw=analogRead(vpin);
vout=(vraw/1023)*25;
Serial.println(vout);
delay(1000);
}

That just means you were fortunate, a pin can read anything and a digital pin can only return zero or one ( high or low), so it matters little what it did a week ago. The thing is it is not stable and the input is floating.

If this is from a sensor it is often the case that the common ground connection is not made.

To learn about floating inputs and how to deal with them read this
http://www.thebox.myzen.co.uk/Tutorial/Inputs.html

And to learn why you need a common ground for both inputs and outputs read this
http://www.thebox.myzen.co.uk/Tutorial/Power_Supplies.html

And wave your hand over the Arduino, or just look at it, or the weather changes from humid to dry and it might change from 0 to some random number.

First of all you guys are right about weather and humidity having a effect on the analog input when the pin is in the air. I have great news! I solved the problem! It was a malfuctioning cable. I switched cables and the arduino is reading the sensor just fine now. Thank you everyone for your time and advice!

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.