IR emitter and receiver problem

So here goes my 1st post :slight_smile:
I thought of building a line following robot using Arduino Uno Rev 3 (on a windows 7 laptop, incase its necessary) by following a tutorial.So I gathered all the required parts and theres this IR emitter/detector (which both of them are on a single pack) which gives me the problem.
I thought of testing everything on a breadboard before actualy beginning to build it. So I connected the IR sensor as instructed in the tutorial, the long leads to +5 of the arduino and the GND to the GND of the Arduino through a resistor. Then a wire from inbetween the GND of the detector and the resistor to an analog pin of the Arduino.

UPDATE : It just gives me 0 or 1 and when i cover it from my finger, it increases upto 25. I tried some other IR sensors of the same type, but nothing changes. Its just the same.
Note - I can use only the analog pins since I have to use the motorshield.

Heres the code : (Very sorry, I am putting the whole code)

// Linus the Line-bot
// Follows a Black line on a White surface (poster-board and electrical tape).
// Code by JDW 2010 – feel free to modify.
#include <AFMotor.h> // this includes the Afmotor library for the motor-controller
AF_DCMotor motor_left(1); // attach motor_left to the Adafruit motorshield M1
AF_DCMotor motor_right(3); // attach motor_right to the Adafruit motorshield M3
// Create variables for sensor readings
int sensor1 = 0;
int sensor2 = 0;
int sensor3 = 0;
int sensor4 = 0;
int sensor5 = 0;
// Create variables for adjusted readings
int adj_1 = 0;
int adj_2 = 0;
int adj_3 = 0;
int adj_4 = 0;
int adj_5 = 0;
// You can change the min/max values below to fine tune each sensor on your bot
int s1_min = 200;
int s1_max = 950;
int s2_min = 200;
int s2_max = 950;
int s3_min = 200;
int s3_max = 950;
int s4_min = 200;
int s4_max = 950;
int s5_min = 200;
int s5_max = 950;

// this threshold defines when the sensor is reading the black line
int lower_threshold = 20;
// value to define a middle threshold (half of the total 255 value range)
int threshold = 128;
// this threshold defines when the sensor is reading the white poster board
int upper_threshold = 230;
// this value sets the maximum speed of linus (255 = max).
// using a speed potentiometer will over-ride this setting.
int speed_value = 255;
// end of changeable variables
void setup()
{
Serial.begin(9600); // start serial monitor to see sensor readings
// declare left motor
motor_left.setSpeed(255);
motor_left.run(RELEASE);
// declare right motor
motor_right.setSpeed(255);
motor_right.run(RELEASE);
}
void update_sensors(){
// this will read sensor 1
sensor1 = analogRead(A0);
adj_1 = map(sensor1, s1_min, s1_max, 0, 255);
adj_1 = constrain(adj_1, 0, 255);
// this will read sensor 2
sensor2 = analogRead(A1); // sensor 2 = left-center
adj_2 = map(sensor2, s2_min, s2_max, 0, 255);
adj_2 = constrain(adj_2, 0, 255);
// this will read sensor 3
sensor3 = analogRead(A2); // sensor 3 = center
adj_3 = map(sensor3, s3_min, s3_max, 0, 255);
adj_3 = constrain(adj_3, 0, 255);
// this will read sensor 4
sensor4 = analogRead(A3); // sensor 4 = right-center
adj_4 = map(sensor4, s4_min, s4_max, 0, 255);
adj_4 = constrain(adj_4, 0, 255);

// this will read sensor 5
sensor5 = analogRead(A4); // sensor 5 = right
adj_5 = map(sensor5, s5_min, s5_max, 0, 255);
adj_5 = constrain(adj_5, 0, 255);
// check value for speed potentiometer if present (to read the pot, uncomment line below)
//speed_pot = analogRead(5) / 4;
}
void loop(){
update_sensors(); // update sensors
//speed_value = speed_pot; // Leave commented out, unless using potentiometer
// first, check the value of the center sensor
if (adj_3 < lower_threshold){
// if center sensor value is below threshold, check surrounding sensors
if (adj_2 > threshold && adj_4 > threshold){
// if all sensors check out, drive forward
motor_left.run(FORWARD);
motor_left.setSpeed(speed_value);
motor_right.run(FORWARD);
motor_right.setSpeed(speed_value);
}
// you want the bot to stop when it reaches the black box.
else if (adj_1 < 1){
if (adj_2 < 1){
if (adj_3 < 1){
if (adj_4 < 1){
if (adj_5 < 1){
// if all sensors are reading black, stop Linus.
motor_left.run(RELEASE);
motor_right.run(RELEASE);
}
}
}
}
}
}
// otherwise, the center sensor is above the threshold
// so we need to check what sensor is above the black line
else {
// first check sensors 1
if (adj_1 < upper_threshold && adj_5 > upper_threshold){
motor_left.run(RELEASE);
motor_left.setSpeed(0);
motor_right.run(FORWARD);
motor_right.setSpeed(speed_value);
}
// then check sensor 5
else if (adj_1 > upper_threshold && adj_5 < upper_threshold){
motor_left.run(FORWARD);
motor_left.setSpeed(speed_value);
motor_right.run(RELEASE);
motor_right.setSpeed(0);
}
// if not sensor 1 or 5, then check sensor 2
else if (adj_2 < upper_threshold && adj_4 > upper_threshold){
motor_left.run(RELEASE);
motor_left.setSpeed(0);
motor_right.run(FORWARD);
motor_right.setSpeed(speed_value);
}
// if not sensor 2, then check sensor 4
else if (adj_2 > upper_threshold && adj_4 < upper_threshold){
motor_left.run(FORWARD);
motor_left.setSpeed(speed_value);
motor_right.run(RELEASE);
motor_right.setSpeed(0);
}
}
///// Print values for each sensor
/////sensor 1 values
Serial.print("sensor 1: ");
Serial.print(sensor1);
Serial.print(" - ");
Serial.print("Adj 1: ");
Serial.print(adj_1);
Serial.print(" - ");
/////sensor 2 values
Serial.print("sensor 2: ");
Serial.print(sensor2);
Serial.print(" - ");
Serial.print("Adj 2: ");
Serial.print(adj_2);
Serial.print(" - ");
/////sensor 3 values
Serial.print("sensor 3: ");
Serial.print(sensor3);
Serial.print(" - ");
Serial.print("Adj 3: ");
Serial.print(adj_3);
Serial.print(" - ");
/////sensor 4 values
Serial.print("sensor 4: ");
Serial.print(sensor4);
Serial.print(" - ");
Serial.print("Adj 4: ");
Serial.print(adj_4);
Serial.print(" - ");
/////sensor 5 values
Serial.print("sensor 5: ");
Serial.print(sensor5);
Serial.print(" - ");
Serial.print("Adj 5: ");
Serial.print(adj_5);
Serial.print(" ");
Serial.print("speed: ");
//Serial.print(speed_pot);
Serial.println(" ");
}
// end of code

Sesnor :

Datasheet : http://www.vishay.com/docs/83760/tcrt5000.pdf

Setup:

It would be really great if someone can help me out here. This is my very first robotic project and I am a total beginner to this field eventhough I have some knowledge on programming stuff :slight_smile:
I searched everywhere on the internet but couldn’t find a solution.
Thanks alot :slight_smile:

need the datasheet for the sensor and a photo of a hand drawn schematic of the breadboarded circuit.

You have the emitter and receiver mixed up. The receiver has the dark/tinted lens and that's where your analog reading should be coming from. Right now you're reading from the emitter, but since that output never varies it acts like a voltage divider and you only get a single value.

raschemmel: need the datasheet for the sensor and a photo of a hand drawn schematic of the breadboarded circuit.

Datasheet : http://www.vishay.com/docs/83760/tcrt5000.pdf

And the schematic is uploaded on the 1st post :) thanx alot for the reply

Chagrin: You have the emitter and receiver mixed up. The receiver has the dark/tinted lens and that's where your analog reading should be coming from. Right now you're reading from the emitter, but since that output never varies it acts like a voltage divider and you only get a single value.

Thanks alot for your reply. I changed the jumper wire to the GND of the dark lens and changed resistors too, but now it just gives me values only inbetween 5 and 15, sometimes goes upto 50. It doesnt show any drastic change when I hold a black object above it and when i hold a white paper above it.

Do you know how to use a multimeter on diode scale ? It’s easy to tell which is the emitter because it will measure like a led
and when you measure the voltage on the anode it will be similar to a led. The transistor collector voltage should be lower with reflection off a white or mirror like surface .

Your Fritzing schematic doesn't match your picture. Your schematic shows 5V -> 10K -> analog input -> collector of IR detector, but your picture shows 5V -> collector of IR detector -> emitter of IR detector -> analog input -> 10K -> GND. The Fritzing schematic is the correct way.

Don't forget that you can verify that the emitter is on by looking at it through a digital camera (like your cell phone). If that's not lit you might have the polarity backwards. If reversing the polarity doesn't work then you might have burnt it out.

The emitter of the receiver is typically on the same side as the cathode of the LED, so if you found a polarity problem with your LED then you probably have the receiver backwards too.

Chagrin: Your Fritzing schematic doesn't match your picture. Your schematic shows 5V -> 10K -> analog input -> collector of IR detector, but your picture shows 5V -> collector of IR detector -> emitter of IR detector -> analog input -> 10K -> GND. The Fritzing schematic is the correct way.

Don't forget that you can verify that the emitter is on by looking at it through a digital camera (like your cell phone). If that's not lit you might have the polarity backwards. If reversing the polarity doesn't work then you might have burnt it out.

The emitter of the receiver is typically on the same side as the cathode of the LED, so if you found a polarity problem with your LED then you probably have the receiver backwards too.

I didnt get the first part :astonished: thanks alot for the tip to check the emitter and apparently its working. I updated the picture above :) Could you please explain me that once again?? And now all I get is just 0 or 1 as the sensor output and if i cover the sensor from my finger, it just goes upto 25 What am I doing wrong here??

Please excuse for the noob-ity :(

I don’t that sensor is going to work with collector pulled up to GND . Try moving the resistor to +5V rail instead.

raschemmel: I don't that sensor is going to work with collector pulled up to GND . Try moving the resistor to +5V rail instead.

thanx for the reply...but could you please explain it a bit more?? I am using a 150 ohm resistor for the emitter and 10k resistor for the detector and both are on GND side.What should I do?? How would you connect these if it was you?? :)

I don't that sensor is going to work with collector pulled up to GND . Try moving the resistor to +5V rail instead.

What does this say ? What do you see when you look at the photo I posted ? I don't know how you could not understand this. Your pullup resistor for the transistor is NOT connected to +5V ! It is connected to GND ! LOOK AT THE PHOTO I POSTED and read my comment above again

raschemmel:

I don't that sensor is going to work with collector pulled up to GND . Try moving the resistor to +5V rail instead.

What does this say ? What do you see when you look at the photo I posted ? I don't know how you could not understand this. Your pullup resistor for the transistor is NOT connected to +5V ! It is connected to GND ! LOOK AT THE PHOTO I POSTED and read my comment above again

didnt work that way either...Its giving me some 20 - 80 value...is there any other way i should try?? thanx alot for taking your time to help me...I am a noob in this field and has no guidance other than this forum.

Take a photo of the pullup resistor for the sensor showing it connected to the +5V rail and post the photo. Look at the schematic . The pullup resistor is supposed to be connected to +5V, not GND. Are you sure it is the correct value resistor ?

raschemmel: Take a photo of the pullup resistor for the sensor showing it connected to the +5V rail and post the photo. Look at the schematic . The pullup resistor is supposed to be connected to +5V, not GND. Are you sure it is the correct value resistor ?

Yes, thank you very much for your help. Apparently like you told the polarity of the phototransistor is usual backward and the resistor and the analog read should be connected to the longer lead. I read some more in internet and found some other help aswel. The given image is one of that.

But now i have another problem, I cant connect more than 3 sensors to analog inputs, it gives me the "avrdude: stk500_getsync(): not in sync: resp=0x00" and the 4th and 5th sensor does not ready any proper value. I will probably post another thread about it.

Are you sure that schematic is correct ? You have a digital output and an analog input connected to the same device. Which one is the emitter ? (clear one or dark one ?)

Hi all, I am a total noob to this field. I am trying to make a line following robot using my Arduino Uno rev3 and I have made the IR sensor array with 5 IR emitter/detector pairs. The problem is, I can't connect more than 3 sensors at once, it gives me "avrdude: stk500_getsync(): not in sync: resp=0x00" error and if i upload the code anyhow, the 4th and the 5th sensor gives me rubbish values or if i cover the 4th or 5th sensor from my finger, the serial ouput just stops right there and continues when i take my finger away. Its always just only 3 inputs work. I am using my laptops USB port. Please help me out here

Hi,

Help us to help you!

  • Schematic
  • Sketch (use code tags)
  • links to data sheets for your sensors.

Paul

PaulRB: Hi,

Help us to help you!

  • Schematic
  • Sketch (use code tags)
  • links to data sheets for your sensors.

Paul

Thanks for taking time to reply...looks like my analog pins A3-A5 are not working properly. When nothing connected to them, it shows 1023; but if i connect a sensor input, it just stops right there. I checked every analog pin with every sensor and A0-A2 gave normal results but A3-A5 didnt seem like working properly. Is there anything I can do??

raschemmel: Are you sure that schematic is correct ? You have a digital output and an analog input connected to the same device. Which one is the emitter ? (clear one or dark one ?)

No I just used that image as a help to figure out how the wiring should go and now the sensors are working fine just like I want. But seems like my analog pins A3 - A5 are not working. I checked every pin with all 5 sensors and A0-A2 gives normal expected readings but A3-A5 arent giving proper readings. When nothing connected, they show 1023 but if i connect a sensor then it just stops right there and continues when i remove the connection.

I don't have time right now to look at your code. Do this. Comment out everything in your code except the 6 analog reads and the serial prints to read the. Measure the voltage with a meter at the analog inputs. Make sure everything has a common ground. Post your results.