You are on page 1of 6

Line Follower

Car

Youssef Mohamed FAWZY Agour 19106323


Moamen Ayman Samir 19106607
Michael Sameh 19106480
Mohamed Ismail 19104391
Mohab Maged 19106167
Introduction:
The line tracking car is a popular project in the field of robotics. It involves a car-like robot that can
follow a line on the ground using sensors. In this project, we have used three IR sensors to detect the
line and an H-Bridge to control the motors. The H-Bridge is controlled by an STM32 blue pill
microcontroller, which also receives power from the battery packs.

Components Used:
- H-Bridge: An H-Bridge is a circuit that allows the motors to be controlled in both directions. In this
project, we have used an H-Bridge to control the motors of the car.
- STM32 blue pill: This is a microcontroller board based on the STM32F103C8T6 microcontroller. It
is used to control the H-Bridge and receive input from the sensors.
- IR Sensors: These sensors are used to detect the line on the ground. In this project, we have used
three IR sensors.
- Battery packs: Two battery packs are used in this project. One is connected to the H-Bridge to power
up the motors, and the other is connected to the STM32 blue pill to power it up.

Chassis:
The chassis used in this project is a 4-wheel chassis. The left two motors are connected to one output
of the H-Bridge, and the same goes for the right two motors. This setup allows the car to move in both
directions.

Code:
The code for this project is written in C language. It uses the HAL library for controlling the GPIO
pins of the STM32 blue pill. The code reads the input from the three IR sensors and determines the
position of the line. Based on the position of the line, it controls the motors using the H-Bridge. The
code runs in an infinite loop and continuously reads the input from the sensors and controls the
motors accordingly.

REPORT TITLE 2
#include "main.h"
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
while (1)
{
int sensor1 = HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0);
int sensor2 = HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_1);
int sensor3 = HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_2);

if (sensor1 == 0 && sensor2 == 1 && sensor3 == 0) // All sensors are on the line
{

HAL_GPIO_WritePin(GPIOA, GPIO_PIN_6, 1);


HAL_GPIO_WritePin(GPIOA, GPIO_PIN_7, 0);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_10, 1);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_11, 0);
}
else if (sensor1 == 0 && sensor2 == 0 && sensor3 == 1) // Line is on the left
{
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_6, 0);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_7, 0);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_10, 1);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_11, 0);
}
else if (sensor1 == 0 && sensor2 == 1 && sensor3 == 1) // Line is in the center
{
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_6, 0);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_7, 0);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_10, 1);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_11, 0);
}
else if (sensor1 == 1 && sensor2 == 0 && sensor3 == 0) // Line is on the right
{
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_6, 1);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_7, 0);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_10, 0);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_11, 0);
}
else if (sensor1 == 1 && sensor2 == 1 && sensor3 == 0) // Line is on the right
{
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_6, 1);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_7, 0);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_10, 0);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_11, 0);
}
else if (sensor1 == 0 && sensor2 == 0 && sensor3 == 0) // Line is on the right

REPORT TITLE 3
{
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_6, 0);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_7, 1);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_10, 0);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_11, 1);
}
else // No line detected
{
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_6, 0);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_7, 0);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_10, 0);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_11, 0);
}

// HAL_Delay(250);

// HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, 1);


// HAL_Delay(25);
//HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, 0);
//HAL_Delay(25);
}
}

void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

/** Initializes the RCC Oscillators according to the specified parameters


* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}

/** Initializes the CPU, AHB and APB buses clocks


*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

REPORT TITLE 4
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
{
Error_Handler();
}
}

static void MX_GPIO_Init(void)


{
GPIO_InitTypeDef GPIO_InitStruct = {0};
/* USER CODE BEGIN MX_GPIO_Init_1 */
/* USER CODE END MX_GPIO_Init_1 */

/* GPIO Ports Clock Enable */


__HAL_RCC_GPIOD_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();

/*Configure GPIO pin Output Level */


HAL_GPIO_WritePin(GPIOA,
MOTOR1_PIN1_Pin|GPIO_PIN_7|MOTOR1_PIN2_Pin|MOTOR2_PIN2_Pin, GPIO_PIN_RESET);

/*Configure GPIO pins : SENSOR1_PIN_Pin SENSOR2_PIN_Pin SENSOR3_PIN_Pin */


GPIO_InitStruct.Pin = SENSOR1_PIN_Pin|SENSOR2_PIN_Pin|SENSOR3_PIN_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

/*Configure GPIO pins : MOTOR1_PIN1_Pin PA7 MOTOR1_PIN2_Pin MOTOR2_PIN2_Pin */


GPIO_InitStruct.Pin =
MOTOR1_PIN1_Pin|GPIO_PIN_7|MOTOR1_PIN2_Pin|MOTOR2_PIN2_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOD_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();

/*Configure GPIO pin Output Level */


//HAL_GPIO_WritePin(led_GPIO_Port, led_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_RESET);

/*Configure GPIO pin : led_Pin */


GPIO_InitStruct.Pin = GPIO_PIN_13;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);

REPORT TITLE 5
void Error_Handler(void)
{

__disable_irq();
while (1)
{
}
}

#ifdef USE_FULL_ASSERT
void assert_failed(uint8_t *file, uint32_t line)
{
}
#endif

Conclusion:
The line tracking car is an interesting project that involves several components and programming
concepts. The use of the H-Bridge and the STM32 blue pill microcontroller makes this project more
advanced and versatile. This project can be further expanded by adding more sensors and
implementing more complex algorithms for line detection and motor control.

REPORT TITLE 6

You might also like