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

Line Follow Logic 17

Combine sensor thresholds and motor decisions for line following robots.

Open in compiler →
/*
  Roboda verified Arduino sample 0425
  Feature: Line Follow Logic
  Generated for online build, classroom practice, and hardware upload testing.
*/

const byte leftSensor = A0;
const byte rightSensor = A1;
const byte leftMotor = 5;
const byte rightMotor = 6;
const int lineThreshold = 260;

void setup() {
  pinMode(leftMotor, OUTPUT);
  pinMode(rightMotor, OUTPUT);
}

void loop() {
  int left = analogRead(leftSensor);
  int right = analogRead(rightSensor);
  analogWrite(leftMotor, left > lineThreshold ? 120 : 210);
  analogWrite(rightMotor, right > lineThreshold ? 120 : 210);
  delay(20);
}

How it works

Use two analog sensors to make basic line-following motor decisions.

  1. Wire left and right sensors to A0 and A1.
  2. Wire motor driver speed inputs to D5 and D6.
  3. Tune lineThreshold for the track and sensor height.
  4. Reduce one side speed when that sensor sees the line.

Connections

PinConnect toNote
A0, A1IR reflectance sensor outputsTune lineThreshold for your surface.
D5, D6Motor driver speed inputsUse separate motor power and common GND.

Good ideas start here.

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