Simple line follower Robot
magic robo

Simple line follower Robot

Components Required


What You Need

  •  Magicbit
  • Magicbot
  • Black color Tape
  • White surface/floor

How IR sensor work

IR reflectance sensor consists of IR emitter and an IR receiver. IR transmits infrared lights. When infrared rays fall on a white surface, it’s reflected back on to the phototransistor which generates a change of currents, which can be turned into a change of voltage using a fixed resistor in series. When IR light falls on a black surface, light is absorbed by the black surface and minimal amount of light is reflected back to the photodiode. This difference in reflected light amount changes the voltage at the output, which can be used to identify whether the sensor is on a black surface or a white surface.


Setting up the arena

This robot finds its way by using color changes on the surface. so its suitable colors are black and white. Therefore Make a 2cm wide black color line on the white surface. You can use some black color tape for that.

Better to make a closed loop line. This allows the robot to work continuously.

line follower map

Hardware Setup

MagicBot – Just connect the magicbit board with a magicbot.  Then, download the Arduino code and upload the code onto your magicbit board.

Note- magicbot has 5 IR sensors, but in this project, we will only use IR1 and IR5.

power up it. (make sure to insert batteries). place the robot on the black line.


//Line Follower Robot

#include <ESP32Servo.h>
#include <Wire.h>
#include <Adafruit_SSD1306.h>

#define ir1 13  //IR sensor left
#define ir2 32  //IR sensor Right

#define M1A 17  //Motor diver pin -Left motor
#define M1B 16
#define M2A 18  //Motor diver pin -Right motor
#define M2B 27

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

void setup() {
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);

pinMode(ir1, INPUT);
pinMode(ir2, INPUT);
pinMode(M1A, OUTPUT);
pinMode(M1B, OUTPUT);
pinMode(M2A, OUTPUT);
pinMode(M2B, OUTPUT);


void loop() {
 int Ls =analogRead(ir1); //Read IR data
 int Rs =analogRead(ir2);

    display.println("Left IR");
    display.println("Right IR");

 if(Ls < 1000 && Rs < 1000) {
  }else if(Ls > 1000){
  }else if(Rs >1000){

 void forward(int x) {  
    analogWrite(M1A, x);
    analogWrite(M1B, 0);
    analogWrite(M2A, x);
    analogWrite(M2B, 0); 


void Left(int x) {  
    analogWrite(M1A, 0);
    analogWrite(M1B, 0);
    analogWrite(M2A, x);
    analogWrite(M2B, 0);

   display.println("Turn Left");

void Right(int x) {  
    analogWrite(M1A, x);
    analogWrite(M1B, 0);
    analogWrite(M2A, 0);
    analogWrite(M2B, 0);

   display.println("Turn Right");


Related Posts
Leave a Reply