Docsity

Line Follower Robot with Computer Vision and CNN, High school final essays of Electronics

Partial preview of the text

Download Line Follower Robot with Computer Vision and CNN and more High school final essays Electronics in PDF only on Docsity!

ELECTRONIC ENGINEERING.

SMART SYSTEMS

PROJECT: LINE FOLLOWER ROBOT WITH COMPUTER VISION AND

CONVOLUTIONAL NEURONAL NETWORK

Students:

Alan Cromo Cogerrubias

Christian Bojorsex Hernansex

Ángel Manuel Gåmez Oltehuevo

H. Nogales, Sonora, June 2, 2025

Content

SUMMARY ........................................................................................................................... 3

1. BACKGROUND ............................................................................................................

1.1. PROBLEM STATEMENT..................................................................................... 6

1.2. OBJECTIVES

1.2.1. General Objectives:

1.2.2. Particular Objectives:

1.3. Scope

1.4. Limitations

2. THEORETICAL FRAMEWORK ..................................................................................

2.1. Line following robots and their characteristics ......................................................

  • Mechanical Structure: .............................................................................................
  • Motors: ...................................................................................................................
  • Wheels: ...................................................................................................................
  • Power Supply: ......................................................................................................
  • Processing Unit: ....................................................................................................
  • Camera: .................................................................................................................
  • H-Bridge L293D Driver: ......................................................................................

2.2. Control algorithm .................................................................................................

2.2.1. Computer vision ...........................................................................................

2.2.2. Image processing ..........................................................................................

4.2.6. Building the dataset.

4.2.7. Coding and Training CNN. ..............................................................................

4.2.7.1 Preparing the dataset .........................................................................................

4.2.7.2 Network topology .............................................................................................

4.2.7.3 Setting hyperparameters and callbacks.............................................................

4.7.2.4 Balance of weights ...........................................................................................

7.4.2.5 Training the model............................................................................................ 38

4.2.8. Loading the model and code for the motors and Socket communication. .......

4.3. Testing and Validation ..............................................................................................

5. RESULTS .........................................................................................................................

5.1. Track#

5.1.1. Track#1 Outdoor test

5.1.2. Track#1 Indoors test (only artificial lighting)

5.1.3. Track#1 Indoor test (Natural Lighting)

5.2. Track#

5.2.1. Track#2 Outdoor Test

5.2.2. Track#2 Indoors test (Artificial lightning)

5.2.3. Track#2 Indoors test (Natural lighting)

5.3. Analysis result

5.3.1. Analysis for time in seconds.

5.3.2. Analysis of average confidence

5.3.3. Analysis Corrections/Steps ...........................................................................

5.3.4. Analysis of lost line

5.4. Comparison of results obtained against objectives:

5.5. LIMITATIONS THROUGH IMPLEMENTATION

6. Conclusion ........................................................................................................................

6.1. Summary of achievements.

6.2. Impact of the project on the defined context.

6.3. Evaluation through the achievement of objectives.

7. BIBLIOGRAPHY ............................................................................................................

1. BACKGROUND

In the early phase of our project, we took time to review past work on line-following robots. This helped us understand the main approaches used in both hardware and software. Out of all the material we found, two papers stood out and really helped shape our prototype. One was focused on traditional sensor-based robots, while the other explored computer vision in challenging lighting conditions. Design and Implementation of Conventional Line-Following Robots The first paper we relied on was “A Line Follower Robot from Design to Implementation” by Pakdaman, Sanaatiyan, and Rezaei (2010). It discusses a robot named TABAR, designed for university competitions in Iran. The system used infrared sensors, an ATmega16 microcontroller, analog-to-digital converters, and an L298 motor driver. One of the key takeaways was how important sensor placement is to detect black lines on white surfaces. The sensors read the reflectance difference, and the microcontroller uses this data to control the motors using PWM signals. They also describe how the robot navigates curves and intersections. This paper helped us understand how a typical robot is built in modules. Sensors serve as inputs, the microcontroller processes data, and the H-bridge drives the motors. It also made us aware of the limits of using only hardware logic in complex environments. Improving Vision Systems Under Variable Lighting Conditions The second paper, “Vision Enhancement of Line Following Robot in Variable Lighting Environments” by AbouElela, Hafez, and Elewa (2014), focused on using computer vision to handle inconsistent lighting. This is a common problem indoors, where lighting can change depending on the source. They used image processing techniques like color correction (Gray-World, Max-RGB, Gray-Edge), converting to HSI color space, contrast enhancement, noise filtering, and morphological operations to fix line breaks. Their setup used a webcam and a laptop running MATLAB, which sent control signals to an Arduino board. This approach was especially useful for our project since we also use a camera and real-time image processing. The paper showed us how to improve line segmentation and keep the system stable under sudden lighting changes. Integration of Approaches Even though the two papers used different methods, both were important in shaping our final design. From the first, we used the classic robot structure with motors, a microcontroller, and PWM control. From the second, we adopted computer vision as the main input for navigation. By combining these ideas, we created a hybrid system. Our robot includes gear motors, a PWM controller, and a Raspberry Pi Zero 2W. It processes camera input in real time, cleans up images,

and runs a TensorFlow Lite model for inference. This setup helped us deal with real-world challenges like lighting shifts and partially visible lines in an efficient and modern way.

1.1. PROBLEM STATEMENT

The line follower robot is a mobile machine that can detect and follow a line drawn on the floor. Usually, the path is already defined and can be clearly visible, like a black line on a white surface with strong color contrast, or it can be invisible, such as a magnetic field (Pakdaman, 2010). One method to detect the line is to use different colors with high contrast for the line and the background so the robot can recognize which is which. However, this has a lot of variation depending on the color and brightness of the surrounding light, and it may change during movement, therefore the sensors cannot detect the line as it should. Shadows, reflections, and objects nearby can also change the way light is reflected. Another variation the type of light, for example a place with fluorescent lighting might not perform the same in an area that uses incandescent lighting, since these light types create different effects on how colors are seen by the sensors (AbouElela, 2014). To solve this problem, it is important to create a stronger and reliable system that improves the robot's ability to understand its environment. A good option is to use computer vision and artificial intelligence. These technologies will help the robot to process visual data in a more flexible and correct way. This project focuses on building a mobile robot that can follow a black line by itself in different lighting conditions. It uses artificial vision with machine learning methods. The robot will take pictures in real time, change them into binary images to get the main details of the line, and use a convolutional neural network (CNN) to decide the correct movement. Then, it will send PWM signals to move the motors. The performance of the robot will be tested in many types of environments to see how strong and flexible it is. Some changes in the AI model and control settings will also be done to make the robot work better.

1.2. OBJECTIVES

1.2.1. General Objectives:

  • Develop a mobile robot capable of following a black line on a white background in three different lighting environments.
  • Implement artificial vision through OpenCV to binarize images, thus converting an image to only black and white colors.
  • Image processing is performed on limited computational hardware such as the Raspberry Pi zero 2w which may restrict real-time response speed.
  • The robot speed is limited, so the robot will move slowly to decrease the chances of losing track of the line.
  • The category system for line detection doesn’t allow any linear regression, so the PWM output signals will be fixed, moving the robot at a certain speed.

2. THEORETICAL FRAMEWORK

2.1. Line following robots and their characteristics

Line-following robots are simple robots. These robots are generally designed to perform a single task: follow a line marked on the ground. Usually, this line is black on a white background to provide a high contrast. To achieve this task, line-following robots use sensors that interact with the processing unit. The components implemented in the following line of this project are the ones shown below.

  • Mechanical Structure: This is the part where the motors, sensors, batteries, and the necessary electronics are mounted to make the robot function.
  • Motors: For movement and control of the line-following robot, TT-type DC motors with plastic gearboxes are used. These motors are widely employed in educational robotics and mobile prototypes due to their ease of use, low cost, and good compatibility with common control systems (Mechatronics, s.f.). Technical Specifications o Operating Voltage: Works between 3V and 6V; 5V is the typical value, ideal for efficient and stable performance o Current Consumption: o No load: Approximately 100mA o Under load: Can reach up to 250mA, depending on the effort required by the robot. o Gearbox: Includes a plastic gearbox with a 1:48 reduction ratio. o Increased Torque: This reduction increases torque, which is essential for precise movements at low speed. o Output Speed: At 6V, it reaches approximately 200 RPM. o Torque: At 5V, it provides an estimated torque of 0.8 kg¡cm, sufficient to move the robot’s structure and overcome small resistances. o Output Shaft: "D"-shaped (flat on one side), with a diameter of 5–6 mm, ideal for attaching standard wheels like the common yellow wheels used in these types of projects.

o Material: Both the casing and the gears are made of ABS plastic, which reduces weight and cost without compromising functionality. Figure 1. DC motor.

  • Wheels: The selected wheels play a key role in the mobility of the line-following robot. Their design and the materials used have been chosen to ensure good traction and grip on various types of surfaces. The rim is made of yellow ABS plastic and features a five-spoke design. At its center, it has a hole specifically adapted to fit D-shaped shafts, such as those used by the TT DC motors implemented in this project. Surrounding the rim is a black rubber tire with a grooved pattern that enhances grip. This design allows for smooth and controlled movement over surfaces such as wood, cardboard, or plastic. These wheels are standard in terms of dimensions and materials for small-scale robots. The total diameter is approximately 65 mm, and the tire width is around 26 mm. The internal shaft diameter is compatible with 6 mm D-shaped axles. The rim is made of ABS plastic, selected for its strength and low weight, while the tire is made of soft rubber (TPE or synthetic rubber), chosen for its excellent traction and adhesion capabilities (uElectronics, s.f.).

Figure 4. Raspberry Pi Zero 2 W. Technical Specifications: o Processor: Broadcom BCM2710A1, a quad-core ARM Cortex-A53 SoC running at 1 GHz, similar to the one used in the Raspberry Pi 3. This architecture allows for multitasking such as image processing and system logic control. o RAM: 512 MB LPDDR2, sufficient for basic vision and control operations in a linefollowing robot. o Wireless Connectivity: Includes Wi-Fi 802.11 b/g/n and Bluetooth 4.2 BLE, useful for remote debugging, data transfer, or external control. o Available Ports: o Mini HDMI port for direct monitor connection during testing or visualization. o Micro USB (OTG) for both power supply and peripheral connection via adapter. o CSI-2 port for connecting a compatible camera with high video transmission efficiency. o 40 - pin GPIO header for controlling motors, line sensors, and other components. o Storage: Uses a microSD card to store the operating system and project files. o Power Consumption: While relatively moderate, the Raspberry Pi’s power demand is high enough in this project to require a dedicated 5V power source. The shared battery system cannot reliably power both the Raspberry Pi and the motors simultaneously without compromising performance.

  • Camera: The robot’s vision system is based on the Raspberry Pi Camera Module 3, the official camera optimized for integration with Raspberry Pi boards. Its performance, compact size, and image quality make it well-suited for computer vision applications (Foundation, 2023). Technical Specifications: o Sensor: Sony IMX708 with a resolution of 12 megapixels, allowing for detailed image capture, essential for visual analysis of the environment. o Image Format: Compatible with output formats such as JPEG and RAW, useful for different processing approaches. o Autofocus: Features motorized autofocus, allowing the camera to adapt to different distances without manual intervention. o Field of View (FOV): Available in standard and wide-angle versions. The variant used offers a wide-angle view, ideal for capturing more environmental information. o Connectivity: Connects via CSI- 2 interface, ensuring fast and efficient data transmission to the Raspberry Pi Zero 2 W.

o Compatibility: Fully compatible with the libcamera library and other development environments used in computer vision. o Lighting Conditions: Available in IR-filtered (standard) and NoIR (no infrared filter) versions, allowing appropriate selection based on the operating environment's lighting conditions. o Compact Size: Its lightweight and compact design makes physical integration into the robot chassis easy without adding significant weight. Figure 5. Raspberry Pi Camera Module 3

  • H-Bridge L293D Driver: To control the motors without damaging the processor (Raspberry), a driver must be used to provide a stable current and voltage supply, the velocity of the motors is controlled through a PWM signal that is the input of the IN pins of the L293D driver. Further details of the driver are shown below (Microelectronics., 1996): o 600ma output current capability per channel o 1.2a peak output current (non repetitive) per channel o Enable facility o Overtemperature protection o Logical “0” input voltage up to 1.5 v (high noise immunity) o Internal clamp diodes A schematic of the driver is shown below:

13 Output 4 Connected to the other side of Motor B. 14 Input 4 Control input for Output 4. 15 Enable 3,4 Enables outputs 3 and 4 (must be HIGH to activate right H-bridge). 16 VCC1 (Logic Supply) Logic power supply (typically 5V for microcontroller compatibility).

2.2. Control algorithm

2.2.1. Computer vision

Computer vision works similarly to human vision, except that humans have the advantage of a lifetime of context to understand how to distinguish objects, their distance, movement, or whether something is wrong in the image. The three main uses of computer vision are:

  • Distinguishing people, shapes, and objects
  • Analyzing variations in light, color, and object motion
  • Orientation and localization in 2D or 3D spaces

2.2.2. Image processing

The goal of image processing is to compensate for deficiencies when capturing an image with a camera. These deficiencies may be related to contrast, brightness, or other visual aspects. To correct them, preprocessing algorithms are applied, performing basic operations such as filtering, level adjustment, normalization, and others. In particular, preprocessing is applied to images with dimensions M×N and 256 grayscale levels per pixel. In this context, a value of 0 represents pure black, while 255 represents pure white. (Chang,

The main goal of preprocessing is to facilitate later stages such as segmentation, feature detection, or pattern recognition.

2.2.3. Thresholding

Thresholding is a fundamental technique in image processing that allows calculating or modifying a threshold value based on the uniqueness of the evaluated environment or context. (Alvear, 2018) This method is key for image segmentation. Through thresholding, a grayscale image can be converted into a binary image composed of only two color levels: black and white.

  • White represents the areas of interest or the regions to be analyzed.
  • Black represents the background or regions considered irrelevant for analysis. (Palomino,

Among the most prominent techniques used in segmentation is:

  • Edge Processing: This technique identifies regions of continuity and discontinuity in the image, allowing optimal paths to be defined that outline objects or regions of interest.

2.2.4. Artificial Intelligence (AI):

AI allows the robot to learn complex patterns from data. In a line-follower robot, a computer vision model can be trained to recognize the line in an image and predict the correct movement (turn, go forward, etc.). Unlike PID or fuzzy logic, AI does not require explicit rules, as it learns from examples (with labeled images and associated actions). This enables more robust and adaptive control, although it requires more processing power and prior training.

2.2.5. Basic principles of AI

  1. Computer Vision: This involves processing images captured by a camera to extract useful information, such as the position of a line or an obstacle. Libraries such as OpenCV are used to apply filters, detect contours, or convert images to grayscale or binary.
  2. Neural Networks and Classification Models: Models like convolutional neural networks (CNNs) can be used to analyze images and predict which action the robot should take (turn left, go straight, turn right). The model is trained using labeled images: each image is paired with a correct action.

2.2.6. Functioning of the Implemented Convolutional Neural Network

A convolutional neural network (CNN) is a type of artificial neural network designed to process data with image-like structure. These networks are especially useful for computer vision tasks, as they can automatically identify visual patterns and features from image data (DataScientest, s.f.). A CNN is composed of an input layer, an output layer, and several intermediate hidden layers. Each of these layers is responsible for extracting different types of features from the images. Initially, the layers detect basic elements such as edges or contrast, and as the information progresses through the network, more complex patterns are recognized, helping to identify shapes or objects. In this project, a CNN was implemented to enable the robot to autonomously follow a black line. The images captured in real time by the camera were first binarized to facilitate processing. The neural network then processed these images through several layers:

  • In the convolutional layer, filters were applied to extract the most relevant features.
  • In the ReLU activation layer, negative values were removed, which helped improve training efficiency.
  • Ecosystem: CUDA includes tools, libraries, and APIs that help build and improve GPUpowered applications.

4. DESIGN

4.1 Hardware design:

The following flow chart shows the core hardware elements of the robot; the lines show the data flow through the elements. First, the camera records the path that is in front of, then the raspberry makes all the processing, so its output is the input to the L293D driver, this component handles the power for the motors which is the element we want to control. Figure 7: Hardware diagram.

4.1.1. Electrical Wiring:

Figure 8: Electrical Wiring

4.1.2 Neural Network structure:

Figure 9: Neuronal Network Flowchart