Line Following Robot
Background Knowledge:
How to read a Schematic
Resistors, Variable Resistors, Transistors, and LEDs in a Circuit
How to use an Arduino
How to read and use a Multimeter
What you'll need:
1 - 9 Volt Battery
1 - 9 Volt Battery Harness
1 - SPST Switch
1 - Breadboard
1 - Arduino UNO
1 - LM298N motor controller
2 - IR proximity sensors
2 - DC motors with wheels
Wheels and Chassis Kit (Make this)
The Line Following Robot project, utilizing an Arduino, LM298N motor controller, and IR sensors, is designed to create an autonomous robotic system capable of tracking and following a predetermined path or line on a surface. The IR sensors, positioned underneath the robot, continuously monitor the surface, generating signals based on the reflected infrared light. The Arduino processes these inputs, implementing a control algorithm that adjusts the LM298N motor controller to regulate the power supplied to the DC motors. This dynamic motor control allows the robot to make precise movements, maintaining alignment with the line. The project serves as an educational tool, offering hands-on experience in sensor integration, motor control, and programming with Arduino. Additionally, it can be extended to include features like obstacle avoidance, providing a foundational platform for understanding and experimenting with robotics concepts in various educational settings.
Parts for Consideration
LM298N
The L298N is a dual H-bridge motor driver integrated circuit (IC) widely used for controlling and driving DC motors and stepper motors. Developed by STMicroelectronics, the L298N is designed to handle high currents and voltages, making it suitable for a variety of robotic and motor control applications.
Key features of the L298N motor controller include:
Dual H-Bridge Configuration: The L298N can control two motors independently with its dual H-bridge configuration, allowing for forward, backward, and braking control.
High Current and Voltage Capability: It can handle relatively high current (up to 2A per channel) and voltage (up to 46V), making it suitable for a wide range of motor types and sizes.
Built-in Protection Diodes: The IC includes built-in flyback diodes, which help protect the circuit from voltage spikes generated by the motors during deceleration.
Logic Compatibility: The L298N is compatible with both TTL (Transistor-Transistor Logic) and CMOS (Complementary Metal-Oxide-Semiconductor) control signals, making it easy to interface with microcontrollers and other digital control circuits.
Heat Sink: The L298N may require a heat sink to dissipate heat generated during operation, especially when driving high currents or operating for extended periods.
In summary, the L298N motor controller is a versatile and robust solution for driving and controlling DC motors and stepper motors in various electronic projects and applications. Its dual H-bridge design, high current/voltage capabilities, and compatibility with different control systems make it a popular choice among hobbyists and engineers for motor control projects.
LM298N Pinout
The L298N motor controller has a 15-pin configuration, and each pin serves a specific function in controlling and driving motors. Here's an explanation of the pinout:
Vs (Supply Voltage): Connect this pin to the external power supply voltage (usually the motor voltage). It can handle 6-12V.
GND (Ground): Ground reference for the motor controller.
Enable A (ENA): This pin enables the motor connected to Output A. When a logic high signal is applied, it activates the corresponding motor.
Enable B (ENB): This pin enables the motor connected to Output B. When a logic high signal is applied, it activates the corresponding motor.
Input 1 (IN1): This pin controls the direction of rotation for Motor A. By providing different logic levels to IN1 and IN2, you can control the motor's rotation (forward, reverse, or brake).
Input 2 (IN2): This pin, in conjunction with IN1, determines the direction of rotation for Motor A.
Input 3 (IN3): This pin controls the direction of rotation for Motor B.
Input 4 (IN4): This pin, in conjunction with IN3, determines the direction of rotation for Motor B.
Output 1 (OUT1): Connects to one terminal of Motor A.
Output 2 (OUT2): Connects to the other terminal of Motor A.
Output 3 (OUT3): Connects to one terminal of Motor B.
Output 4 (OUT4): Connects to the other terminal of Motor B.
Vss (Logic Supply Voltage): You can use this pin to the power of the logic circuitry (microcontroller, etc.) This pin outputs at 5V.
It's essential to provide appropriate external power supplies to the Vs and Vss pins, and the logic control signals (ENA, IN1, IN2, ENB, IN3, IN4) are typically controlled by a microcontroller or other digital control circuits. Careful attention to the logic levels and power requirements is crucial for proper operation of the L298N motor controller.
IR Proximity Sensor
An IR Proximity Sensor designed for line-following applications is a key component in robotics and automation. This sensor is specifically tailored to detect infrared light, allowing it to distinguish between contrasting surfaces, such as a dark line on a lighter background. Here's a summary of its features and functionality:
Infrared Sensing Technology: The IR Proximity Sensor utilizes infrared light to detect variations in reflectivity on the surface beneath it. This makes it particularly useful for tracking lines on surfaces, like a contrasting track on the ground.
Emitter and Receiver Pair: The sensor typically consists of an infrared emitter and a photodiode receiver. The emitter sends out infrared light, and the receiver measures the intensity of the reflected light. The difference in reflectivity helps the sensor determine the position of the line.
Analog Output: These sensors often provide analog output, delivering a voltage signal proportional to the intensity of the reflected infrared light. The analog signal can be processed by a microcontroller to make decisions about the robot's movement based on the detected line.
Adjustable Sensitivity: Many IR Proximity Sensors for line followers come with adjustable sensitivity settings. This allows users to fine-tune the sensor's response to varying lighting conditions or different surface types.
The pinout of an IR Proximity Sensor can vary between different models and manufacturers, but here is a general overview of the typical pin configuration for such sensors:
VCC (Power Supply): This pin is used to connect the sensor to the positive power supply. It usually requires a voltage within a specified range (commonly 3.3V or 5V), as indicated by the manufacturer.
GND (Ground): Connect this pin to the ground (0V) of the power supply. It establishes the reference potential for the sensor.
Signal Output: The sensor provides an output signal that corresponds to the intensity of the reflected infrared light. This pin carries the analog or digital signal, depending on the specific sensor model. In some sensors, there might be both analog and digital output pins.
Various Notes
Adjustment (if available): Some IR Proximity Sensors come with a sensitivity adjustment potentiometer. If present, this pin allows you to adjust the sensor's sensitivity to better suit the environmental conditions or the type of surface being detected.
Emitter (LED): In sensors with a separate emitter and receiver, this pin is connected to the infrared LED (Light Emitting Diode) that emits the infrared light used for detection.
Receiver (Photodiode): In sensors with a separate emitter and receiver, this pin is connected to the photodiode that receives the reflected infrared light. The intensity of the received light is used to determine the surface characteristics.
It's crucial to refer to the datasheet or documentation provided by the specific manufacturer for accurate pinout information, as variations can exist. Additionally, when integrating the sensor into a circuit, ensure proper voltage levels, and take note of any polarity considerations.
Building the Project
Using the schematic to the right, create the Arduino RGB circuit.
Connect 9V power to the Motor Controller. This should be directly connected so that the motor controller pulls current from the battery and not the Arduino.
Use 5V from the LM298N to power the Arduino.
Connect the Enable A, Enable B, Input 1, Input 2, Input 3, and Input 4 to their corresponding Arduino Pins. You can use any PWM pins. In this case, we used pins ~11, ~10, ~9, ~6 ~5, and ~3 in that order.
Connect your IR Sensors to 5V power and ground from the Arduino.
Connect thier Signal pins to the corresponding Analog outputs on the Arduino. In this case, we used A0 and A1.
Connect the motors to Output 1, 2, 3, and 4 of the Motor A and B terminal Blocks. Polarity will matter but this can be adjusted later.
2. Write the Arduino Sketch
// Made by MakerLessons
int ENA = 11; //ENA connected to digital pin 11
int ENB = 3; //ENB connected to digital pin 3
int MOTOR_A1 = 10; // MOTOR_A1 connected to digital pin 10
int MOTOR_A2 = 9; // MOTOR_A2 connected to digital pin 9
int MOTOR_B1 = 6; // MOTOR_B1 connected to digital pin 6
int MOTOR_B2 = 5; // MOTOR_B2 connected to digital pin 5
int RIGHT = A0; // RIGHT IR sensor connected to analog pin A0
int LEFT = A1; // LEFT IR sensor connected to analog pin A1
// the setup function runs once when you press reset or power the board
void setup() {
pinMode(ENA, OUTPUT); // initialize ENA pin as an output
pinMode(ENB, OUTPUT); // initialize ENB pin as an output
pinMode(MOTOR_A1, OUTPUT); // initialize MOTOR_A1 pin as an output
pinMode(MOTOR_A2, OUTPUT); // initialize MOTOR_A2 pin as an output
pinMode(MOTOR_B1, OUTPUT); // initialize MOTOR_B1 pin as an output
pinMode(MOTOR_B2, OUTPUT); // initialize MOTOR_B2 pin as an output
pinMode(RIGHT, INPUT); // initialize RIGHT IR pin as an input
pinMode(LEFT, INPUT); // initialize LEFT IR pin as an input
}
// the loop function runs over and over again forever
void loop() {
if (analogRead(RIGHT)<=35 && analogRead(LEFT)<=35) //compare the both sensor to decide the direction
{
//MOVE FORWARD//
analogWrite(ENA, 100); // set right motors speed
analogWrite(ENB, 100); // set left motors speed
//run right motors clockwise
digitalWrite(MOTOR_A1, LOW);
digitalWrite(MOTOR_A2, HIGH);
//run left motors clockwise
digitalWrite(MOTOR_B1, HIGH);
digitalWrite(MOTOR_B2, LOW);
}
else if (analogRead(RIGHT)<=35 && !analogRead(LEFT)<=35) //compare the both sensor to decide the direction
{
//MOVE RIGHT//
analogWrite(ENA, 255); //set right motors speed
analogWrite(ENB, 255); //set left motors speed
//run right motors clockwise
digitalWrite(MOTOR_A1, LOW);
digitalWrite(MOTOR_A2, HIGH);
//run left motors anti-clockwise
digitalWrite(MOTOR_B1, LOW);
digitalWrite(MOTOR_B2, HIGH);
}
else if (!analogRead(RIGHT)<=35 && analogRead(LEFT)<=35) //compare the both sensor to decide the direction
{
//MOVE-LEFT//
analogWrite(ENA, 255); //set right motors speed
analogWrite(ENB, 255); //set left motors speed
//run right motors anti-clockwise
digitalWrite(MOTOR_A1, HIGH);
digitalWrite(MOTOR_A2, LOW);
//run left motors clockwise
digitalWrite(MOTOR_B1, HIGH);
digitalWrite(MOTOR_B2, LOW);
}
else if (!analogRead(RIGHT)<=35 && !analogRead(LEFT)<=35) //compare the both sensor to decide the direction
{
//STOP//
analogWrite(ENA, 0); //set right motors speed
analogWrite(ENB, 0); //set left motors speed
//stop right motors
digitalWrite(MOTOR_A1, LOW);
digitalWrite(MOTOR_A2, LOW);
//stop left motors
digitalWrite(MOTOR_B1, LOW);
digitalWrite(MOTOR_B2, LOW);
}
}
3. Understand the Code
int ENB = 11; //ENB connected to digital pin 11
int MOTOR_A1 = 10; // MOTOR_A1 connected to digital pin 10
This section of code at the beginning assigns the pins to their chosen name as variables that will be used later in the code.
pinMode(ENB, OUTPUT); // initialize ENB pin as an output
pinMode(MOTOR_A1, OUTPUT); // initialize MOTOR_A1 pin as an output
pinMode(RIGHT, INPUT); // initialize RIGHT IR pin as an input
The lines set the previously assigned variables to inputs or outputs depending on what will be plugged into them.
void loop() {
Loops set all the code within them to repeat over and over to continue running your program.
if (analogRead(RIGHT)<=35 && analogRead(LEFT)<=35) //compare the both sensor to decide the direction
{
//MOVE FORWARD//
analogWrite(ENA, 100); // set right motors speed
analogWrite(ENB, 100); // set left motors speed
//run right motors clockwise
digitalWrite(MOTOR_A1, LOW);
digitalWrite(MOTOR_A2, HIGH);
//run left motors clockwise
digitalWrite(MOTOR_B1, HIGH);
digitalWrite(MOTOR_B2, LOW);
}
This section of code is one of four conditions set for the direction of the vehicle using the logic statements IF, and ELSEIF. They ask the logical question of IF a condition is met with the IR Sensors, then go forward. This requires setting both motors in the same direction and speed. There are three other logic conditions in this code that set left, right, and stop.
4. Upload and Monitor
Upload the sketch to the Arduino board. You could add code to Print your IR Sensor values to the Serial Monitor to check that your sensors are working and outputting the right values. This would like like: Serial.println("RIGHT"); for example.
5. Experiment and Observe
Build a simple chassis and install the motors, Arduino, LM298M, IR Sensors, and wheels to create the Line Following Robot. This can be out of foam-core, cardboard, or if you're bold enough and have access, laser cut or 3D print one! Test that it works. A good black line to follow can be to use Black Vinyl Electrical Tape.
Considerations:
Power Supply: Ensure that the power supply can adequately power both the motors and the Arduino. It's common to use separate power supplies for the motors and the logic components. This prevents any voltage drops from the motors from interfering with the microcontroller.
Sensor Placement: Properly position the IR proximity sensors to effectively detect the line. Adjust the sensor angles and heights for optimal performance.
Motor Calibration: Fine-tune the motor control parameters in the code to achieve smooth and precise line following.
Environmental Factors: Consider variations in lighting conditions, track design, and surface color, and account for these factors in your code.
Documentation: Keep track of pin connections, code versions, and any modifications made during the development process for future reference.
Questions:
How do the IR Sensors work?
Why do you need to use the LM298N instead of directly connecting the motors to the Arduino?