RoboticsA Car That Avoids Obstacles
A two-motor robot car drives forward, spots a wall from afar with its ultrasonic “eye”, stops, looks left and right and turns where the way is clear.
RoboticsTwo infrared sensors see a black line on the floor and the car follows it on its own, round the bends too. On a wide black strip it stops, as if at a finish line.
Written by the RoboHub team. A question? Write to us.
Choose a variation and the parts list updates.
Lay a closed loop of 2 cm black electrical tape on a light floor or a big sheet of white paper. Keep the bends gentle. For a finish, stick a wide strip of tape across the line.
Wire the chassis, motors, L298N and power the same way as in “A Car That Avoids Obstacles”: ENA 5, IN1 7, IN2 8, ENB 6, IN3 4, IN4 2.
Fix two sensors under the front of the car, 1 cm above the floor and a little further apart than the line is wide. Connect the left sensor's DO pin (OUT on some modules) to A0 and the right one's to A1, VCC to 5V and GND to ground.
Put the sensor over white, then over black, and turn its little screw until the module's LED lights over white and goes out over black.
Place the car so the line runs between the two sensors. If the car runs away from the line, change BLACK = HIGH to BLACK = LOW in the code.
// L298N: take the jumpers off ENA and ENB and wire them to PWM pins
const int ENA = 5, IN1 = 7, IN2 = 8; // left motor
const int ENB = 6, IN3 = 4, IN4 = 2; // right motor
const int LEFT_EYE = A0, RIGHT_EYE = A1; // the sensors' DO outputs
const int BLACK = HIGH; // most modules give HIGH over black, some give LOW
const int SPEED = 140; // on the straight
const int TURN = 170; // in the bends
void drive(int left, int right) { // -255..255 for each side
digitalWrite(IN1, left > 0 ? HIGH : LOW);
digitalWrite(IN2, left < 0 ? HIGH : LOW);
digitalWrite(IN3, right > 0 ? HIGH : LOW);
digitalWrite(IN4, right < 0 ? HIGH : LOW);
analogWrite(ENA, abs(left));
analogWrite(ENB, abs(right));
}
void setup() {
int pins[] = {ENA, IN1, IN2, ENB, IN3, IN4};
for (int pin : pins) pinMode(pin, OUTPUT);
pinMode(LEFT_EYE, INPUT);
pinMode(RIGHT_EYE, INPUT);
delay(2000); // time to put the car on the track
}
void loop() {
bool left = digitalRead(LEFT_EYE) == BLACK;
bool right = digitalRead(RIGHT_EYE) == BLACK;
if (left && right) drive(0, 0); // a wide black strip: the finish
else if (left) drive(-TURN / 2, TURN); // line under the left sensor: turn left
else if (right) drive(TURN, -TURN / 2); // under the right one: turn right
else drive(SPEED, SPEED); // line between the sensors: straight on
}
RoboticsA two-motor robot car drives forward, spots a wall from afar with its ultrasonic “eye”, stops, looks left and right and turns where the way is clear.
RoboticsAn HC-05 Bluetooth module links the car to your phone. Drive it forward and back and turn it left and right with the buttons of an Android app, and change its speed too.
NewRoboticsFive infrared sensors measure how far the robot has drifted off the line, and a PID controller corrects the motors smoothly and fast. Built for competitions: the track, the sensor bar, the code and how to tune it.