Build Robotic Car to follow path
Building your own line-following car that can track and follow a line drawn on the floor is a great DIY robotics project. Below is a step-by-step guide that outlines the process of creating a basic line-following robot using readily available components such as an Arduino or Raspberry Pi, motors, sensors, and a chassis.
Materials You’ll Need:
Chassis: A simple car chassis kit with space for motors, sensors, and a microcontroller.
Example: 2-wheel or 4-wheel car chassis.
Microcontroller: You can choose between Arduino or Raspberry Pi.
Example: Arduino Uno (for beginners) or Raspberry Pi.
Motors: Two DC motors for the wheels.
Example: 2 x DC motors (with motor driver module, like L298N).
Line Sensor Module: Infrared (IR) sensors to detect the line.
Example: 2 or more IR sensors for detecting black lines on a white surface.
Motor Driver Module: To control the DC motors from the microcontroller.
Example: L298N motor driver module (for Arduino) or H-bridge motor driver.
Battery: Power supply for the car and microcontroller.
Example: 9V battery or a rechargeable Li-ion battery pack.
Wheels: 2 wheels for the DC motors and a caster wheel or ball wheel for balance.
Example: Plastic or rubber wheels suitable for the chassis.
Miscellaneous: Jumper wires, breadboard (optional), screws, and nuts.
Step-by-Step Instructions:
Step 1: Assemble the Car Chassis
Start by assembling the car chassis kit. Attach the DC motors to the motor slots on the chassis. Mount the wheels onto the motors. If your chassis kit includes a caster wheel or balance ball, attach it to the front or rear to keep the car stable.
Ensure there’s enough space on the chassis to mount the microcontroller (Arduino or Raspberry Pi), motor driver, and sensors.
Step 2: Connect the Motors to the Motor Driver
The motor driver module (L298N) will control the speed and direction of the motors. The motor driver needs to be connected to the Arduino or Raspberry Pi and the motors as follows:
Connect the motors to the motor output terminals on the motor driver module (OUT1, OUT2 for Motor 1; OUT3, OUT4 for Motor 2).
Connect the power supply: Connect the battery's positive terminal to the motor driver's VCC pin, and the negative terminal to the GND pin.
Connect the motor driver's input pins (IN1, IN2, IN3, IN4) to the appropriate pins on the microcontroller (Arduino pins 9, 10, 11, 12, for example).
Enable Pins: Ensure the motor driver’s enable pins (EN1, EN2) are connected to the microcontroller to allow control over motor speed.
Step 3: Install the Line-Following Sensors (IR Sensors)
Attach two or more IR sensors (also called line-tracking sensors) at the front of the car, facing the floor. These sensors will detect the difference between the black line and the white floor.
Sensor Positioning: Place the sensors slightly apart, ensuring they can both detect the line and adjust the car’s direction.
Sensor Connections:
Connect the VCC of each sensor to the 5V pin on the Arduino.
Connect the GND of each sensor to the GND pin.
Connect the OUT pin of each sensor to different digital input pins on the Arduino (e.g., pins 2 and 3).
Step 4: Write the Code
Here is an example code to run the robot using Arduino that will make the car follow the black line on a white surface. The logic is simple: when the left sensor detects black and the right sensor detects white, the car turns right, and vice versa.
// Pin Definitions int leftSensor = 2; // Left IR sensor int rightSensor = 3; // Right IR sensor int leftMotorForward = 9; // Left motor forward int leftMotorBackward = 10; // Left motor backward int rightMotorForward = 11; // Right motor forward int rightMotorBackward = 12; // Right motor backward void setup() { // Initialize sensor pins as input pinMode(leftSensor, INPUT); pinMode(rightSensor, INPUT); // Initialize motor pins as output pinMode(leftMotorForward, OUTPUT); pinMode(leftMotorBackward, OUTPUT); pinMode(rightMotorForward, OUTPUT); pinMode(rightMotorBackward, OUTPUT); } void loop() { int leftState = digitalRead(leftSensor); // Read left sensor int rightState = digitalRead(rightSensor); // Read right sensor // Move forward if both sensors are on white (0 = white, 1 = black) if (leftState == 0 && rightState == 0) { moveForward(); } // Turn right if left sensor is on black (1) else if (leftState == 1 && rightState == 0) { turnRight(); } // Turn left if right sensor is on black (1) else if (leftState == 0 && rightState == 1) { turnLeft(); } // Stop if both sensors are on black else if (leftState == 1 && rightState == 1) { stopCar(); } } void moveForward() { digitalWrite(leftMotorForward, HIGH); digitalWrite(leftMotorBackward, LOW); digitalWrite(rightMotorForward, HIGH); digitalWrite(rightMotorBackward, LOW); } void turnRight() { digitalWrite(leftMotorForward, HIGH); digitalWrite(leftMotorBackward, LOW); digitalWrite(rightMotorForward, LOW); digitalWrite(rightMotorBackward, HIGH); } void turnLeft() { digitalWrite(leftMotorForward, LOW); digitalWrite(leftMotorBackward, HIGH); digitalWrite(rightMotorForward, HIGH); digitalWrite(rightMotorBackward, LOW); } void stopCar() { digitalWrite(leftMotorForward, LOW); digitalWrite(leftMotorBackward, LOW); digitalWrite(rightMotorForward, LOW); digitalWrite(rightMotorBackward, LOW); }
Step 5: Power the Robot
Connect the power supply (9V or a Li-ion battery pack) to the Arduino and the motor driver module. Ensure the motors and sensors have enough power to function properly.
Step 6: Draw the Line and Test the Robot
On the floor, use black electrical tape or a black marker to draw a line for the robot to follow. Make the line a continuous loop or a path with curves to test the car's ability to follow different directions.
Place the car on the line and power it on. The IR sensors should detect the black line, and the car should adjust its direction based on the sensor readings.
Step 7: Debugging and Fine-Tuning
If the car does not follow the line correctly, adjust the distance between the IR sensors, tweak the motor speed, or modify the code to improve the performance.
You can also add additional sensors to improve line detection accuracy and fine-tune the thresholds for detecting black vs. white surfaces.
Step 8: Optional Enhancements
Speed Control: Add a potentiometer or use Pulse Width Modulation (PWM) to control motor speed.
Obstacle Detection: Add ultrasonic sensors to detect obstacles and program the car to stop or navigate around them.
Wireless Control: Implement Bluetooth or Wi-Fi to remotely control the car or adjust settings in real-time.
Conclusion
By following these steps, you can build a line-following robot that detects and follows a black line on a white surface. This project introduces key robotics concepts like motor control, sensor integration, and algorithm development, providing a hands-on learning experience in robotics and electronics.