Analog input, always giving a value ?

Am a bit fuzzy here...

Thinking of analog input between 0 and 1024, now, when uploading following code to my arduino:

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);

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 ?

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.

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.

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.

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.

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 :(

What device? Are grounds common?

MarkT: What device? Are grounds common?

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

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.

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 :frowning:

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!

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.

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