A little curiosity. A whole lot of making.Robotics, CNC & connected ideas
robodatech.

Configuration 3: IR Line Follower

The line follower uses two IR sensors under the front of the robot. When the robot drifts away from the black line, Arduino Nano corrects the motors.

What students learn: Feedback control, thresholds, calibration, and simple automation.
Read this before wiring: The schematic shows only the modules required for this configuration. Compare it with the Arduino program pins before connecting the battery.

Mode-specific circuit schematic

Line circuit schematic Mode-specific schematic: only the modules needed for this build are shown. +5V regulated logic rail 7.4V motor rail from 2 x 3.7V Li-ion through switch Common GND rail Arduino Nano D5 L293D ENA PWMD6 L293D ENB PWMD8-D11 L293D inputsA0 Left IRA1 Right IR L293DPin 1 EN1,2 from D5Pin 2 IN1 from D8Pin 7 IN2 from D9Pin 9 EN3,4 from D6Pin 10 IN3 from D10Pin 15 IN4 from D11Pin 8 VCC2 from 7.4VPin 16 VCC1 from +5V LeftRight IR PairLeft OUT to Nano A0Right OUT to Nano A1 Blue = Arduino signal, red = power, gray = ground. Arduino GND, L293D GND, battery negative, and module GND must join.

Pin and power rules

Power rule

Use the Li-ion pack for motor power through L293D VCC2, and regulated 5V for Arduino Nano logic and modules.

Ground rule

All grounds must be common, otherwise the Arduino signal has no reliable reference.

Pin rule

The Arduino programs on this page match this pin plan: D5/D6 PWM, D8-D11 motor direction, D2-D3 Bluetooth, D4/D7 ultrasonic, D12 servo, A0-A5 sensors/RF.

Step-by-step build

  1. Place black tape on a light floor.
  2. Mount left and right IR sensors 8 to 15 mm above the floor.
  3. Connect left IR OUT to A0 and right IR OUT to A1.
  4. Tune sensor screws until black and white surfaces read differently.
  5. Run slowly first and adjust speed.

Arduino Nano program

Arduino Nano program
Download HEX
// Arduino Nano Two-IR Line Follower
const int leftIR = A0;
const int rightIR = A1;
const int ENA = 5, IN1 = 8, IN2 = 9, ENB = 6, IN3 = 10, IN4 = 11;

void setup() {
  pinMode(leftIR, INPUT); pinMode(rightIR, INPUT);
  pinMode(ENA, OUTPUT); pinMode(IN1, OUTPUT); pinMode(IN2, OUTPUT);
  pinMode(ENB, OUTPUT); pinMode(IN3, OUTPUT); pinMode(IN4, OUTPUT);
}

void loop() {
  int leftValue = digitalRead(leftIR);
  int rightValue = digitalRead(rightIR);

  // Many IR modules output LOW on black. If yours is opposite, swap LOW/HIGH logic.
  bool leftOnLine = (leftValue == LOW);
  bool rightOnLine = (rightValue == LOW);

  if (leftOnLine && rightOnLine) forward(135);
  else if (leftOnLine && !rightOnLine) left(120);
  else if (!leftOnLine && rightOnLine) right(120);
  else stopMotors();
}

void forward(int s){ drive(HIGH,LOW,HIGH,LOW,s); }
void left(int s){ drive(LOW,HIGH,HIGH,LOW,s); }
void right(int s){ drive(HIGH,LOW,LOW,HIGH,s); }
void stopMotors(){ analogWrite(ENA,0); analogWrite(ENB,0); }
void drive(int a1,int a2,int b1,int b2,int s){
  digitalWrite(IN1,a1); digitalWrite(IN2,a2);
  digitalWrite(IN3,b1); digitalWrite(IN4,b2);
  analogWrite(ENA,s); analogWrite(ENB,s);
}
Use Build here to verify this program with the Roboda Arduino compiler.

Do and don't

Do
  • Use Arduino Nano as the controller.
  • Keep battery negative and all module GND pins connected.
  • Test motors with wheels lifted first.
  • Use a voltage divider for HC-05 RX if Arduino TX is 5 V.
  • Power servo from a stable 5 V source.
Don't
  • Do not short Li-ion cells.
  • Do not connect motors directly to Arduino Nano pins.
  • Do not mix loose wires without labels.
  • Do not run high motor speed during first tests.
  • Do not assume every RF module has the same pinout.

Good ideas start here.

Robotics lessons, build notes and useful updates. Subscribe by entering your email.