Analog read varies from 0 to 1023 even though it should be a fixed value

Hello,

I am using analog read to get the values from a tilt sensor. The specs say that the sensor outputs from 0.5 to 4.5 volts depending on the angle. I checked the output with a multimeter and got the expected results. Then I fixed the tilt sensor so that it should output ~2.5V and checked it again with the multimeter. I connected it to the arduino, but the values from analog read start at 0 go up all the way to 1023, go back down to 0 and the cycle repeats. The strangest thing for me is that the reading varies even though the sensor is fixed at the same angle.

Not sure if this is relevant, but the sensor is connected to a 12V battery since it needs >9V.

Any suggestions on where the problem might be?

Thanks!

Post your code.

Post your circuit.

Here's the code:

// Pin
int analogPin = 7;
// Define value
int val = 0;

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

void loop() {
  val = analogRead(analogPin);      
  SerialUSB.println(val);
  delay(1000);
}

and here's the circuit sketch (hope it's good enough, I don't really know how to draw a circuit diagram)

mr-blue:
SerialUSB.println(val);

Hi,
I've never seen such instruction. Is new?
Regards.

P.S. The datasheet of the sensor would be needed.

mr-blue's picture:

I think you'll need to show us how the Arduino board is powered. I believe your problem could be related to a lack of a common ground between the Arduino board and the sensor.

It's better to post pictures directly to the forum. Some forum members won't bother to follow external links so hosting the picture outside the forum can mean you'll have a lesser chance of getting help. The way to host pictures on the Arduino forum and embed them in your post is described here:
http://forum.arduino.cc/index.php?topic=364156

@pert

The board is powered by USB. You're right there is no common ground, thanks for the suggestion! Is there a simple way to get them a common ground? I cannot connect the Arduino directly to the battery because the voltage is too big, right?

Also thanks for the tip on how to insert images directly!

@vffgaston

the board is a Sodaq Sara N211 . They also have their own arduino library, maybe it has something to do with that. I'm not sure though since it's the only board I've used (and, as you can probably tell, I haven't been using it for too long :slight_smile: )

mr-blue:
@vffgaston

the board is a Sodaq Sara N211 . They also have their own arduino library, maybe it has something to do with that. I'm not sure though since it's the only board I've used (and, as you can probably tell, I haven't been using it for too long :slight_smile: )

Thanks.

Is there a simple way to get them a common ground?

Yes. All grounds MUST be connected together with wire.

What is the battery and what is the sensor? Post links.

@pert & @jremington

Thank you! I connected the battery min to the arduino ground and now I am getting the expected values.

I'm glad to hear it's working. Enjoy!
Per