Arduino robot line follower

This article will show how to make a car or a robot line follower by Arduino.

Hardware need:
1. Arduino UNO R3 (1pc)
2. Motor controller H-bridge L298N (1pc)
3. Infrared (IR) sensor (3pcs)
4. DC Motor 9V with wheel (2pcs)
5. Accessories (car frame, cables, battery...)




Step 1. Understand IR sensor
IR sensor has a infrared light source (called transmitter) to emit infrared light, the light traverse to object and reflect to receiver.
If object observes IR light highly, the light reflect to receiver will be low, so voltage at receiver will be low. Object with dark color or high roughness will observe a lot of light
If object observes IR light lowly, the light reflect to receiver will be high, so voltage at receiver will be high. Object with bright color or low roughness (smooth) will observe a little of light. 

In this project, IR sensor is used to detect the line (dark color) in ground (white color)

With IR sensor module, the thing is easier when it can feedback logic 0 & 1 to Arduino, also can adjust detecting point of received light by vari-resistor
Specification of IR module:
   Power source: 3.3-5V
   Out-put: logic 0 1
   Detect distance: 2-30cm (adjust by vari-resistor)

Step 2. Circuit
Note: ENA, and ENB pin are jumped to 5VDC
H-bridge IN1, IN2 will control motor_1. When IN1 = 5VDC; IN2 = 0VDC, motor will run in one direction. When IN1 = 0VDC; IN2 = 5VDC, motor will run in reverse direction. This principle is same for motor_2.




Step 3. Code works
const int IN1=3;
const int IN2=5;
const int IN3=6;
const int IN4=9;
#define sensor1 10
#define sensor2 11
#define sensor3 12

void setup() {
  Serial.begin(9600);
  pinMode(IN1,OUTPUT);
  pinMode(IN2,OUTPUT);
  pinMode(IN3,OUTPUT);
  pinMode(IN4,OUTPUT);
  pinMode(sensor1,INPUT);
  pinMode(sensor2,INPUT);
  pinMode(sensor3,INPUT);
}

void loop() {
  int a=digitalRead(sensor1);
  Serial.println(a);
  int b=digitalRead(sensor2);
  Serial.println(b);
  int c=digitalRead(sensor3);
  Serial.println(c);
if((a==LOW)&&(b==HIGH)&&(c==LOW)) goahead();
if((a==LOW)&&(b==HIGH)&&(c==HIGH)) turnright();
if((a==LOW)&&(b==LOW)&&(c==HIGH)) turnright();
if((a==HIGH)&&(b==HIGH)&&(c==LOW)) turnleft();
if((a==HIGH)&&(b==LOW)&&(c==LOW)) turnleft();
if((a==LOW)&&(b==LOW)&&(c==LOW)){ 
  back();
  delay(800);
  }
if((a==HIGH)&&(b==HIGH)&&(c==HIGH)) turnright();
}

void goahead() {
  analogWrite(IN1,0);
  analogWrite(IN2,50);
  analogWrite(IN3,0);
  analogWrite(IN4,50);
  }
void turnright() {
  analogWrite(IN1,0);
  analogWrite(IN2,90);
  analogWrite(IN3,0);
  analogWrite(IN4,0);
  }
void turnleft() {
  analogWrite(IN1,0);
  analogWrite(IN2,0);
  analogWrite(IN3,0);
  analogWrite(IN4,90);
  }
void stop() {
  analogWrite(IN1,0);
  analogWrite(IN2,0);
  analogWrite(IN3,0);
  analogWrite(IN4,0);
  }
void back() {
  analogWrite(IN1,50);
  analogWrite(IN2,0);
  analogWrite(IN3,50);
  analogWrite(IN4,0);
  }


Result video






2 comments:

  1. Thế nào mà m lại pro E lên cỡ này rồi cơ vậy, đọc ko hiểu gì luôn ._.

    ReplyDelete