Go Down

Topic: strange analog input? Wierd... (Read 3 times) previous topic - next topic

zero-one

Hi everyone.

I'm still learning, so please be patient :P

I dunno why I get strange reading from analogs ports EVEN if there is NOTHING attached to.

Code: [Select]


int aval=0;  
int bval=0;    

void setup()
{
 Serial.begin(9600);
 pinMode( 1, INPUT );
 pinMode( 0, INPUT );
 pinMode( 10, INPUT );
}

void loop()
{


aval=analogRead(10);
bval=analogRead(1);

//aval=map(aval,0,1023,0,255);
//bval=map(bval,0,1023,0,255);

Serial.print("sx= ");
Serial.print(aval);
Serial.print(" dx= ");
Serial.println(bval);

 delay(100);                           // waits
}


In the serial monitor i get something like

sx= 234 dx=236
sx= 235 dx=236
sx= 234 dx=237
sx= 236 dx=236
sx= 234 dx=239

And nothing is attached to my arduino.except for USB of course.
is that an EM interference?i'm using a LCD and (as far as i know) triggering those values would require more than a cellphone on the desk or a nearby pc...
Can you help me or clarify why this happens?
some time i get aval=0 bval=14 (or similar values).
I tried adding and removing the Pnmode instruction, but that's not the problem (the ports 1 and 10 are inputs by default anyway!)
this happens with all the analog ports.

I forgot i've an arduino Mega.
Thanks in advance for any help!



Groove

Your inputs are floating.
pinMode (at least in the number range you're using) has no effect on analogue inputs.
Per Arduino ad Astra

zero-one

#2
Oct 27, 2009, 07:06 pm Last Edit: Oct 27, 2009, 07:20 pm by zero-one Reason: 1
yes but i've tried doing a diagnostic, cycling on all  ports from 0 to 15.
these are the DIGITAL reads:

0= 1 , 1= 0 , 2= 1 , 3= 1 , 4= 1 , 5= 1 , 6= 1 , 7= 1 , 8= 0 , 9= 0 , 10= 0 , 11= 0 , 12= 0 , 13= 0 , 14= 1 , 15= 1

with NOTHIN attached to...how can measure 1 if there is not even a wire attached?I cannot do anything,buttons or other inputs...I tried a simple switch it keeps sayin 1,1,1,1....
is my arduino gone crazy?

i tried also enabling pullup resistor but nada...still same problem.

pluggy

#3
Oct 27, 2009, 07:23 pm Last Edit: Oct 27, 2009, 07:27 pm by stephen_t Reason: 1
Like Groove said, your inputs are floating, ie they are undefined, you can make them read one way or the other by using a pull up / pull down resistor.  You can either use a physical resistor (10-100k is nice) between the pin and ground (pulldown) or between the pin and 5v (pullup). Or you can enable an internal resistor in software to make it go one way or the other. for example if you wanted to detect a switch being turned on digital pin 4, wire a switch between pin 4 and ground and use
Code: [Select]

pinMode(4, INPUT);
digitalWrite(4, HIGH); // internal pull up

in the setup part of the sketch.
you can then read the switch using digitalRead, it will be low when the switch is closed and high when its open.
http://pluggy.is-a-geek.com/index.html

zero-one

#4
Oct 27, 2009, 07:30 pm Last Edit: Oct 27, 2009, 07:55 pm by zero-one Reason: 1
i already tried internal pullup.

Code: [Select]

int pin=4;  
int var=0;
void setup()
{
 Serial.begin(9600);
 pinMode( pin, INPUT);
 digitalWrite( pin, HIGH);       // turn on pullup resistors
}

void loop()
{
var=digitalRead(pin);
Serial.println(var);
delay(1000);                          
}



result in:
1
1
1
1
1
1
1
1
1
1
....

With OR without anything attached.

i've no idea what to do...

pluggy

You're connecting the switch between the pin and ground aren't you ? Using a pullup and connecting it between the pin and 5V will mean it reads high all the time.
http://pluggy.is-a-geek.com/index.html

retrolefty

#6
Oct 27, 2009, 08:04 pm Last Edit: Oct 27, 2009, 08:05 pm by retrolefty Reason: 1
I'm a little confused. Your last post shows reading digital inputs with their internal pull-ups enabled, so they should read 1 (HIGH) if you ground the pin it will read 0 (LOW), so your results are as expected.

Your original post was about analog inputs, and floating analog inputs will result is random valves returned if not wired to a source voltage or ground, and the internal pull-up option does not apply to analog input reads.

So what exactly are you trying to understand?

Just to be clear you know that analog input pin 0 is not the same pin as digital pin 0, right?

Lefty

ajb

The arduino, like any digital device, is only capable of reading a logical '1' or a logical '0' from a digital input.  It has no concept of anything else in that context, so it must choose one or the other.  You may think you have nothing connected to the input, but in reallity lots of things are capacitively or inductively or resistively coupled through e.g. the circuit board substrate or the very air surrounding the chip.  Those things are usually negligible and drowned out by the valid signal when you've got something deliberately connected, but if not then those other things can cause the input to drift towards one or zero depending on what's going on around it.  The same goes for the analog inputs.  If you touch your finger to one of the analog inputs with nothing else attached you will cause the readings to change, for example, since your body has measurable resistive and capacitive proeprties.

Think of it like a weather vane; if you have a strong steady wind you will get meaningful readings.  If it's mostly calm but with gentle intermittent breeze, though, it will just drift around rather randomly.  Connecting a pullup or pulldown resistor or a valid TTL signal will provide that strong steady wind.  

zero-one

#8
Oct 27, 2009, 08:44 pm Last Edit: Oct 27, 2009, 08:46 pm by zero-one Reason: 1
well...
i was trying a light sensor and i noticed that i have to map values to have a correct feeback (axample: with no light was 300, full light was 500, so i did map(var,300,500,0,255); )
This was my original problem.
I'm now aware that there is a EM "noise" that should be considered.
My real concern was...it is normal than, in a scale  from 0 to 1024 (AnalogRead values) having nothing connected to the designed probe pin can trigger a feedback of 300-400 or even 500?That is really a huge amount it's like sayin it is picking up almost 50% of the +5v,right?.
Also, i'm aware that any little thing can trigger a logic state 0 or 1, but despite having understood how to fix it for the analog in (with map as i said..at least this is how i do it.) i'm unable to read 0 from a connected pin or a pressed switch/button, even with pullup resistor enabled internally AND externally.
Maybe i'm wrong but the first tests i did with my arduino i did not had this problems.Pin disconnected = analogRead 0.

Right now i'm reading 1 with ANY pin grounded,and the arduino is completely free of other hardware/cables except the USB!

Forgive me if i'm misunderstanding something obvious, i'm following the learning section which is great to start but this problem is just unexpected for me,at least for what i know.

Thanks for your replies anyway!





trialex

Yes you are misunderstanding.

The results you are getting are perfectly normal and mean your arduino is working 100% correctly.

When you enable a "pullup" it very weakly connects the input internally to 5V, so when nothing else is connected, it reads that pin as HIGH, which is equal to digital 1.

Similarly with the analog inputs - when nothing is connected, it can read anything between 0 and 5V.

Remember that there are small voltages everywhere, for example static electricity can read in the thousands of volts, and since there is nothing to overcome those small voltages in the atmosphere, they are what the arduino reads.

Do a bit more reading/googling using the terms people here have given you - specifically "floating", and "pullup".

Also Ladyada's Tutorial #5 - http://www.ladyada.net/learn/arduino/lesson5.html

pluggy

#10
Oct 27, 2009, 11:18 pm Last Edit: Oct 27, 2009, 11:21 pm by stephen_t Reason: 1
Quote
Right now i'm reading 1 with ANY pin grounded,and the arduino is completely free of other hardware/cables except the USB!


Using the code you posted in post #4, it behaves exactly as expected, it prints out '1' repeatedly until you ground pin 4 when it prints out '0'

I did have a degree of trouble, wondering why it didn't work until I caught on I was uploading the sketch and monitoring 1 arduino and trying to ground pin 4 on the other one......   ::)   (Note to self, it makes a lot of sense to only have one plugged in at once.)

You're not doing something similar ?
http://pluggy.is-a-geek.com/index.html

zero-one

#11
Oct 27, 2009, 11:48 pm Last Edit: Oct 27, 2009, 11:54 pm by zero-one Reason: 1
i've pasted the same example in the link provided.

Code: [Select]
/*
* Switch test program
*/

int switchPin = 2;              // Switch connected to digital pin 2

void setup()                    // run once, when the sketch starts
{
 Serial.begin(9600);           // set up Serial library at 9600 bps
 pinMode(switchPin, INPUT);    // sets the digital pin as input to read switch
}


void loop()                     // run over and over again
{
 Serial.print("Read switch input: ");
 Serial.println(digitalRead(switchPin));    // Read the pin and display the value
 delay(100);
}


and as it says

Quote
Now use a wire to alternate between connecting Pin 2 to 5V and Ground through a 100[ch937] resistor, and watch the serial monitor.




well.. my serial monitor keeps saying 1.
Any way i connect port 2,to 5v to ground. always 1.

:(

retrolefty

Quote
well.. my serial monitor keeps saying 1.
Any way i connect port 2,to 5v to ground. always 1.


Well then maybe you have a bad pin. Have you tried the other digital pins?

Lefty


ajb

#13
Oct 28, 2009, 01:22 am Last Edit: Oct 28, 2009, 01:23 am by ajb Reason: 1
In the picture you have the pin connected to 5V via your 100R resistor--in other words, you've added an external pullup resistor--and since the internal resistors are also pullup resistors, of course you're reading 1 all the time.  If you ground the pin you will read 0.

zero-one

#14
Oct 28, 2009, 09:26 am Last Edit: Oct 28, 2009, 10:26 am by zero-one Reason: 1
pin2--->100ohm Res.--->+5v  
with or without internal pullup always report 1

pin2--->100ohm Res.--->Ground
with or without internal pullup always report 1

i've tried even without resistor and with onther 100 ohm resistor.

the image above is not what i'm doing.that's from the tutorial.i've no protoshield.
To be precise this is what i'm doing:



but the same pins actually works as analog inputs...

Go Up