IR sensors somehow detects distance instead of color? help

Hi. For some reason our IR sensor in our line tracing robot registers black and white the same. I have no idea whats wrong since the circuit is right and I only used a simple code. When we raise it up by about 3 cm it moves but when we put it down it doesn't, doesn't matter if its in a black line or a white line. Also when I check the serial monitor, it detects fine???????????????? i have no idea.

//MOTOR DRIVER
const int AIA = 9;
const int AIB = 6;
const int BIA = 5;
const int BIB = 3;
//line tracking sensors
const int middleL = 4;
const int middleR = 8;
const int innerR = 12;
const int innerL = 2;
const int outerL = 7;
const int outerR = 13;
//misc
const int White = 0;
const int Black = 1;
const int SPEED = 100;
const int STRAIGHT = 150;
const int SLOW = 90;


// void functions (is that even the right term?)
void forward() {
  analogWrite(AIA, 0);
  analogWrite(AIB, SPEED);
  analogWrite(BIA, SPEED);
  analogWrite(BIB, 0);
}
void backward() {
  analogWrite(AIA, SPEED);
  analogWrite(AIB, 0);
  analogWrite(BIA, 0);
  analogWrite(BIB, SPEED);
}
void turnRight() {
  analogWrite(AIA, 0);
  analogWrite(AIB, SPEED);
  analogWrite(BIA, 0);
  analogWrite(BIB, SPEED);
}
void turnLeft() {
  analogWrite(AIA, SPEED);
  analogWrite(AIB, 0);
  analogWrite(BIA, SPEED);
  analogWrite(BIB, 0);
}
void stop() {
  analogWrite(AIA, 0);
  analogWrite(AIB, 0);
  analogWrite(BIA, 0);
  analogWrite(BIB, 0);
}
void straightOn() {
  analogWrite(AIA, STRAIGHT);
  analogWrite(AIB, 0);
  analogWrite(BIA, 0);
  analogWrite(BIB, STRAIGHT);
}

void slowDown() {
  analogWrite(AIA, SLOW);
  analogWrite(AIB, 0);
  analogWrite(BIA, 0);
  analogWrite(BIB, SLOW);
}

void setup() {
  pinMode(AIA, OUTPUT);
  pinMode(AIB, OUTPUT);
  pinMode(BIA, OUTPUT);
  pinMode(BIB, OUTPUT);
  pinMode(13, OUTPUT);
  pinMode(middleL, INPUT);  //looks out for curves
  pinMode(middleR, INPUT);  //looks out for curves
  pinMode(outerL, INPUT);   //border for left
  pinMode(outerR, INPUT);   //border for right
  pinMode(innerL, INPUT);   //monitors the line
  pinMode(innerR, INPUT);   //monitors the line
  Serial.begin(9600);
}

void loop() {
  Serial.print("| middle left sensor: ");
  Serial.print(digitalRead(middleL));
  delay(1000);
  
  Serial.print("| middle right sensor: ");
  Serial.println(digitalRead(middleR));
  delay(1000);
  
  Serial.print("| inner right sensor: ");
  Serial.print(digitalRead(innerR));
  delay(1000);
  
  Serial.print("| inner left sensor: ");
  Serial.println(digitalRead(innerL));
  delay(1000);
  
  Serial.print("| outer left sensor: ");
  Serial.print(digitalRead(outerL));
  delay(1000);
  
  Serial.println(" | outer right sensor: ");
  Serial.println(digitalRead(outerR));
  delay(1000);
  
  trace();
}

//WHERE CONDITIONAL STATEMENTS LOL
void trace() {

  int MRinLine = digitalRead(middleR) == White;
  int MLinLine = digitalRead(middleL) == White;
  int IRinLine = digitalRead(innerR) == White;
  int ILinLine = digitalRead(innerL) == White;
  int ORinLine = digitalRead(outerR) == White;
  int OLinLine = digitalRead(outerL) == White;

  //inner left sensor
  if (digitalRead(outerR) == Black) {
    turnLeft();
    delay(500);

  }

  //inner right sensor
  else if (digitalRead(outerL) == Black){
    turnRight();
    delay(500);
  }
  //inner sensors in line
 else if (IRinLine && ILinLine && MRinLine && MLinLine) {
    forward();
  }

  else if (digitalRead(middleR)==White && digitalRead(middleL)==White && (MRinLine && MLinLine)){
    slowDown();
  }

  else {
    forward();
  }
}



This the whole code that our teacher gave us. I'm honestly going insane

Welcome to the forum, and thanks for posting your code correctly.

Please post a link to the sensor, and a wiring diagram.

Post what IR sensor are you using. (link or image)

image
this is the IR sensor we're using

The IR module does not care about color, just how much IR is reflected from the surface.

1 Like

so how do we solve the problem? hehehe

or maybe possible causes of our problem

Maybe the IR emitter puts out too much IR light for your application.

but we've already tried adjusting the sensitivity of the sensors and yet it still doesnt work.

That is the only thing I can think of. If you adjusted the pot on the sensor and have not found the module to work acceptably, then perhaps the surface reflects too much IR for it to sense the difference between black line and white space.

Find a physical color that is recognized as being different from the "color" of your background/floor. IR is invisible and does not have a notion of "black" and "white", only of more or less reflecting IR radiation.

Your mistake is using an IR detector to detect a difference in the visual spectrum. If you are using painted lines then black and white may have virtually equal reflactance to IR.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.