Go Down

Topic: weird stuff happening with sensor program o_0 (Read 1 time) previous topic - next topic


I'm really confused. I have a program that's supposed to take sensor readings (it's a Sharp long-distance IR sensor). It's supposed to print them out on the serial monitor in values ranging from 0 to 1023. It worked just fine before, and then it suddenly started acting strangely; now the values seldom go any higher than 200, and they are totally inconsistent. I'll place my hand directly in front of the sensor and start to move it away, and the numbers start to grow larger, which is what it's supposed to do‚Ķbut then they fluctuate  like crazy, even when my hand is barely moving! What have I done? Here is my code:

int sensorvar = 0;
int led = 9;
int outputValue = 0;

void setup(){
  pinMode(led, OUTPUT);

void loop(){

  sensorvar = analogRead(1);
  outputValue = map(sensorvar, 0, 1023, 0, 255);
  analogWrite(led, outputValue);
  Serial.print("sensor value:");





Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)


I don't think it's the wiring; I have the signal wire connected to analog pin 1, and the power and ground wires are connected to the power and ground rails on the breadboard. Also, I doubt that it's dirt, because I tried using my other Sharp sensor in case this one is defective, and that one produced the same results.


steps to test (I understood that it partly works and the problems are fluctuations, but nevertheless):

  • remove for a moment the analogWrite(led, outputValue); and see if it becomes stable

  • Connect analog 1 to gnd, does it read 0

  • Connect analog 1 to VCC or also to 3.3, does it read 1023 (or for 3.3V around 675 give and take a bit)

  • Connected something odd to AREF?

  • Tried another pin?

  • if you put the sensor and a free wire to analog 1, does it fluctuate when you touch the wire?

  • is it analog 1 or the 1st analog in?

  • recheck the wiring!


Go Up