Pages: [1]   Go Down
Author Topic: Analog input, always giving a value ?  (Read 1314 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Am a bit fuzzy here...

Thinking of analog input between 0 and 1024, now, when uploading following code to my arduino:
Code:
int analogPin = 0;     // potentiometer wiper (middle terminal) connected to analog pin 3
int lightPin = 9;                       // outside leads to ground and +5V
int val = 0;           // variable to store the value read

void setup()
{
  pinMode( lightPin, OUTPUT);
  Serial.begin(9600);          //  setup serial
}

void loop()
{
  val = analogRead(analogPin);    // read the input pin
  Serial.println(val);             // debug value

  val = constrain( val, 200, 900 );

  int led = map( val, 200,900,0,100 );
 
  analogWrite( lightPin, led);

//  Serial.println(led);
 
  delay(500);
}

im always getting a value (with no pins connected what so ever, only usb cable!), and the longer my uno is on with that code, the more the value increases.
How come ?

Am trying to hook it up with another device which shall give a value as max 1024 (5v) but since arduino itself always get its own values, im not quite sure if the device is giving a value ot arduino make one up for me ?
Logged

Southern California
Offline Offline
God Member
*****
Karma: 5
Posts: 539
I like blinky lights
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
im always getting a value (with no pins connected what so ever, only usb cable!), and the longer my uno is on with that code, the more the value increases.
How come ?

Noise. You don't have the pin connected to anything, so it's picking up noise.
Logged

http://en.wiktionary.org/wiki/magagna <-- My last name.  Pretty apt.

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 537
Posts: 27067
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

The arduino does not know if something is connected to the analog input pin or not.  So you get a conversion of whatever the voltage is on the pin.
With nothing connected to the pin, the pin is "floating" and may drift higher or lower.
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

UK
Offline Offline
God Member
*****
Karma: 17
Posts: 568
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

As said above, with nothing connected to the pin then it's just sitting there in a high impedance state. It needs very little influence for it to look like it has some 'input'. This can just be from the background (e.g. EMF) or noise form the uC generally, adjacent pins or other components.

Once you have something connected to the pin then you'll have a definitive input, as long as whatever you connect never goes open circuit, or too high an impedance.

Logged

Johannesburg. UTC+2
Offline Offline
Faraday Member
**
Karma: 95
Posts: 4316
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Just as a matter of clarity, there are 1024 possible analog values including 0, so the range is from 0 to 1023 not 0 to 1024.
Logged

Roy from ITCrowd: Have you tried turning it off an on again?
I'm on LinkedIn: http://www.linkedin.com/in/jimbrownza

Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

When I connect my other device to analog pin 0 am still getting noise. Some random values when device is not sending anything. But when device does send something I'm having difficulty to read the break point, when I actually get a value. To know which ones were from noise and which ones are actual values.

@tack as you stated is what I expected. Constant values when having connection on that pin but I'm not gettin any, when I have not values.from other device I'm still getting random values. Also when device has values, I'm getting too jumpy values.

Values from my device I measured shoulb be around 1000 since I'm getting round 5v. But since they are too jumpy I'm also getting 2xx also 0 sometimes. Higly unreliable values smiley-sad
Logged

0
Offline Offline
Shannon Member
****
Karma: 206
Posts: 12164
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What device?  Are grounds common?
Logged

[ I won't respond to messages, use the forum please ]

Johannesburg. UTC+2
Offline Offline
Faraday Member
**
Karma: 95
Posts: 4316
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What device?  Are grounds common?

Post a circuit diagram... and / or a photo and / or a Fritzing pic
Logged

Roy from ITCrowd: Have you tried turning it off an on again?
I'm on LinkedIn: http://www.linkedin.com/in/jimbrownza

UK
Offline Offline
God Member
*****
Karma: 17
Posts: 568
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

A better explanation and diagrams would be more useful. It doesn't sound like your device is providing a constant input to the analog pin, since you say 'when device does send'. If it's only providing a signal at intervals then the pin will float all over the place in between. You'd need to put the pin into a know state with a pullup or pull down resistor, so it goes to a known state of 0 or 1023 when no signal is being applied.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Sorry guys for not mentioning what device im working with.

It is a simple smoke detector,


I played a little more after i posted the original post and found new solution, instead of getting 5v (from all pins)
i found only one pin gives around 2 +-.5v.

The pin im working with now is the green marked to most right (seems to be +).
The middle one seems to be ground, so when measuring the middle and right pins on alarm, i get round 2v

So im connecting the right pin to arduino analog pin 0 and reading its values, but when alarm has not triggered the voltage is 0 and arduino should be giving me 0 in output.
But im not getting 0 smiley-sad

Worth to mention is that  when alarm goes off, it is not constant value i get, since the alarm is programmed to peep in delays. ( a peep every second )

no alarm: values between 0-1023 in random order
alarm: random nr - 1023 - random nr - 1023 - random nr -1023

Alarm seems to have a pattern that i can work with
but the problem is while no alarm, there is no pattern, im not able to detect if alarm has been set off or not :S

(Sorry for the bad explanations, electronics is whole new area for me)

EDIT: looking into fritzing now, looks like great tool for diagrams!
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 626
Posts: 34118
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

It sounds like you need a pull up (or pull down) resistor on your analogue input to make sure it is always in a known state. I would try with something like a 47K pull down resistor to start with.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ahh got it, to get rid of floating values, the analog pin needs to be grounded, that solved my problem and now im getting steady 0 values when alarm is off and between 0-65 when alarm is on
Logged

Pages: [1]   Go Up
Jump to: