Sahar Soleymaniniaz faghih
Published © GPL3+

Self guided robot with IR sensors

An autonomous robot designed to follow a predefined path using infrared sensors, ideal for logistics and automation.

IntermediateProtip3 days139
Self guided robot with IR sensors

Things used in this project

Hardware components

Arduino Nano R3
Arduino Nano R3
×1
Rechargeable Battery, Lithium Ion
Rechargeable Battery, Lithium Ion
×2
Rocker Switch, Non Illuminated
Rocker Switch, Non Illuminated
×1
Dual H-Bridge motor drivers L298
SparkFun Dual H-Bridge motor drivers L298
×1
DC Motor, 12 V
DC Motor, 12 V
We used 8V power for driver in motors
×2
Jumper wires (generic)
Jumper wires (generic)
×1
IR Range Sensor
Digilent IR Range Sensor
×5
Machine Screw, M3
Machine Screw, M3
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Code

Robot code

Arduino
This code is for an Arduino based line following robot that uses 5 digital sensors to detect a line and control 2 motors for movement
const int sensorPins[5] = {2, 3, 4, 5, 6};
 

const int IN1 = 7;   
const int IN2 = 8;  
const int IN3 = 12; 
const int IN4 = 13; 
const int ENA = 9;   
const int ENB = 10;  
 

int motorSpeedF = 150;
int motorSpeedB = 120;
void setup() { 
  for (int i = 0; i < 5; i++) { 
    pinMode(sensorPins[i], INPUT); 
  } 
  pinMode(IN1, OUTPUT); 
  pinMode(IN2, OUTPUT); 
  pinMode(IN3, OUTPUT); 
  pinMode(IN4, OUTPUT); 
  pinMode(ENA, OUTPUT); 
  pinMode(ENB, OUTPUT); 
} 
 
void loop() { 
  
  bool sensors[5]; 
  for (int i = 0; i < 5; i++) { 
    sensors[i] = digitalRead(sensorPins[i]); 
  } 
  if (!sensors[0] || !sensors[1]) {       
    turnLeft();                         
  }  
  else if (!sensors[3] || !sensors[4]) { 
    turnRight();                         
  }  
  else if (!sensors[2]) {                 
    moveForward();                       
  }                           
} 
  
void moveForward() { 
  digitalWrite(IN1, HIGH); 
  digitalWrite(IN2, LOW); 
  analogWrite(ENA, motorSpeedF); 
 
  digitalWrite(IN3, HIGH); 
  digitalWrite(IN4, LOW); 
  analogWrite(ENB, motorSpeedF);  
} 
 
void turnLeft() { 
  digitalWrite(IN1, LOW); 
  digitalWrite(IN2, HIGH); 
  analogWrite(ENA, motorSpeedB); 
 
  digitalWrite(IN3, HIGH); 
  digitalWrite(IN4, LOW); 
  analogWrite(ENB, motorSpeedF); 
} 
 
void turnRight() { 
  digitalWrite(IN1, HIGH); 
  digitalWrite(IN2, LOW); 
  analogWrite(ENA, motorSpeedF); 
 
  digitalWrite(IN3, LOW); 
  digitalWrite(IN4, HIGH); 
  analogWrite(ENB, motorSpeedB);
} 
 

Credits

Sahar Soleymani
3 projects • 0 followers
niaz faghih
3 projects • 0 followers

Comments