IR sensor and photoresistor problems

Hi guys,

I am still a relatively new Arduino user and have did a lot with LED's and things but now I am working toward sensors and applications. I am having a weird problem/issue. I bought the IR emitter and detector from spark fun and I am not getting anything like I thought I would. I have them wired out correctly according to the forum on the sparkfun side. I have them probably about an inch apart facing right at each other. I block them to begin with and then i move the object to try and get a signal and print it to the serial monitor. I have tried analog inputs and pulsein and I am getting the same signal if they are blocked or not. Something seems to be floating but I can't figure out why. The same thing happens with the photoresistor.

Here is the IR test code. The emitter is always on and the receiver is plugged into pin 12.

int irpin = 12;
unsigned long irstatus;

void setup(){
  pinMode(irpin, INPUT);
  Serial.begin(9600);
}

void loop(){
  irstatus = pulseIn(irpin, LOW);
 if(irstatus != 0){
   Serial.println(irstatus);
 }
}

The output is usually close to 0 then close to max, like 5, 986.

Here is the code for the photoresistor (also bought from spark fun).
I have 5V going to the resistor and a 10k resistor to limit the current. Measuring where the resistor and photoresistor starts.

int resistorpin = 1;

int resistval;
void setup(){
  pinMode(resistorpin, INPUT);
  Serial.begin(9600);
}

void loop(){
  resistval = analogRead(resistorpin);
  Serial.println(resistval);
  delay(50);
  
}

The serial output for usually goes 0, 0, 0, 234, 687, 1023 for the most part. This doesn't vary no matter what I do.

Any ides or suggestions to help me out?

In your second sketch, you don't set the pinmode() for analog input pins.

You are changing the pinmode of digital pin 1 which is one of the serial pins.

pulseIn measures pulses in microseconds. Perhaps you have a noisy connection. Does it go wild when you giggle the wires?

Thanks for the reply. I haven't changed the IR things yet but I am messing with the photoresistor. I changed the code to

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

void loop(){
  Serial.println(analogRead(A0));
  delay(500);
  
}

And the serial out put generally is:

0
0
0
0
0
791
1023
32
0
0
0
0
714
1023
35

With me not doing anything. Ideas?

Well I used the 5V and GND from the Duemilanove board and the signal works fine. Why didnt it work from an external 5V source? Ground refernce issue?

Is the 'external source' regulated and filtered? If not it may just be half-wave rectified (60 Hz pulses).

Did you remember to connect the ground lines between the external supply and the Arduino ground? The analog inputs measure relative to Arduino ground.

The external source is a PC power supply thats modified. I wasn't running through my regulator. When i powered the board off of it everything worked fine. Connecting ground makes perfect sense, that was me overlooking the simple things 8). Thanks for the responses and help