Pages: [1]   Go Down
Author Topic: IR sensor and photoresistor problems  (Read 1888 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 29
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Code:
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.
Code:
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?
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 180
Posts: 8103
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

0
Offline Offline
Newbie
*
Karma: 0
Posts: 29
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for the reply.  I haven't changed the IR things yet but I am messing with the photoresistor.  I changed the code to
Code:
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?
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 29
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 180
Posts: 8103
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

0
Offline Offline
Newbie
*
Karma: 0
Posts: 29
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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   smiley-cool.  Thanks for the responses and help
Logged

Pages: [1]   Go Up
Jump to: