raspberry pi

Autonomous Car using Raspberry Pi, Raspberry Pi Line Follower Car, Autonomous Vehicle

Autonomous car, Overview:

Autonomous Car using Raspberry Pi- In this project we are building a small Autonomous car controlled by the Raspberry Pi, which is able to follow a black line independently. So, this is basically a Line follower robot car, you simply draw black lines and then the Autonomous car follows these lines, you don’t have to give commands to control this car, this makes this car as the self driving car. You can also make the cheapest version of the same project using Arduino Uno or Arduino Nano. Making a self driving car is not that easy, you will need to use Cameras, Lidars, GPS Trackers, etc to make a fully functional autonomous or self driving car. Autonomous car or Self driving car running on roads in real traffic situation should be able to identify humans, animals, small and big vehicles, the location, the distance, etc.

As a beginner you can start with the simpler and cheapest version that is the line follower autonomous or self driving car. In this project we don’t need any camera, Lidar, GPS, and other expensive modules. The autonomous car or self driving car that we are going to build in this project can be used in homes, industries, hospitals, etc for the cleaning purposes, documents transportation, security purposes etc. The autonomous robot starts from the home location and stops at the end of the line and again returns to the same spot from where it had started.

Amazon Purchase Links of the components used in the building of the Autonomous Car:

RC car chassis Dagu Rover 5 Model

L298N motor driver:

IR tracker Sensors

WLAN stick to avoid cables

Raspberry Pi

raspberry pi 4 4gb kit

Wireless Keyboard and Mouse for raspberry pi:

Night vision Camera for Raspberry Pi:

Oled HDMI touch display for raspberry pi:

TOP10 Best Raspberry Pi Kits

Other Tools and Components:

Super Starter kit for Beginners

Digital Oscilloscopes

Variable Supply

Digital Multimeter

Soldering iron kits

PCB small portable drill machines

*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!


The chassis Dagu Rover 5 model

You can use any remote-controlled car chassis as the base of the car. Alternatively, you can find RC car chassis in many online model making shops. We used the Dagu Rover 5 model (see Figure 1). The main advantage of using the Dagu Rover 5 Model is that, We don’t need a separate steering system, we can change the direction of travel by controlling the left or right side motor. Like many others, the chassis contains two motors on each side of the vehicle. The two motors on each side will be connected in parallel, so the two motors will act as the one motor. This will give the feeling as if there are two motors only, one on the left side and the other one on the right side. In addition, four encoders are attached to the motors with which the speed can be read back. For this project we leave the encoders but lie on the left.

Figure 1: The track chassis Dagu Rover 5

The IR trackers, line sensor:

As the eye of the car, we use a line follower (see Figure 2). This is a small sensor that uses infrared light to differentiate between dark and light surfaces. The sensor has three connection pins: VCC, GND and OUT. How it works it’s very easy. The infrared tracking sensor module uses TCRT5000 infrared reflection sensor whose infrared emitting diodes are constantly emitting infrared rays. When object is in detecting range, infrared ray will be reflected back with high intensity, the module outputs high level and indicating diode is on. When the infrared ray emitted is not reflected back or intensity is not high enough, the infrared receiving tube is in the off state and module outputs low level, while indicating diode is off. So for the light surface it outputs high signal and for the dark surface it outputs low signal. These high and low signals can be then used to decided whether the sensors are on the top of the dark surface or light surface.

Autonomous Car
Figure 2: The line follower sensors

In our experiments, it was not a problem to supply the sensor with 3.3 V. This is because the sensor will output the voltage level with which it is supplied. So make sure you don’t exceed 3.3V, otherwise use a voltage divider at the output.

The mechanics:

Now let’s start with the mechanical part of this project.

  • Creating a base for the chassis
  • Assembly of the line followers
  • Fixing the Raspberry Pi and the motor driver

We used a thin wooden plate as the base. This will just go on screwed on the chassis (see Figure 3). With two small holes you can route the motor cables to the top. Of course you can too Implement your own idea of ​​a base plate. Note, however, that these are not becomes too heavy. The small engines easily reach their limits. Use therefore base plates made of wood, plastic or a maximum of aluminum. Keep the overall weight of the robot as low as possible.

Autonomous Car
Figure 3: chassis with a wooden plate as a base

For this project you need two line follower modules. We have attached them on the top of two metal brackets that were attached to the front of the chassis, so our car now resembles a forklift (see Figure 4 to 6). The distance between the two sensors is approx. 2 cm. Our goal is that to lock the black line between the two sensors. So, any of the IR sensor detects the black line, the raspberry Pi will send a command to change the direction immediately by activating the left or right side motor.

Autonomous Car
Figure 4: metal brackets for the sensors


Autonomous Car
Figure 5:The mounting surfaces must be close to the floor.


Autonomous Car
Figure 6: The line follower sensors were taped to the forks.

We use a ready-made module as the motor driver. That saves us a lot of wiring work and thus also minimizes the potential for errors. Our finished module uses the L298 driver and is directly compatible with the instructions. The finished module has the advantage that all components are mounted on a small circuit board. This can easily be screwed onto the base plate. Another advantage is that most L298 prefabricated modules have a 5V output. So you generate an additional 5V from the applied motor voltage. You have the option of also supplying the Raspberry Pi. The whole thing should be completely mobile or portable, we are not suppose to use long power lines. Everything should be on the robot chassis.

Connection plans:

The overall project consists of several individual blocks (see Figure 7):

  • The Raspberry Pi
  • The line follower sensors
  • The motor driver L298
  • The motors
  • The voltage source(s)
Autonomous Car
Figure 7: connection of all individual components

Above is the block diagram that shows how things are connected to the Raspberry Pi. While the interfacing is explained via the circuit diagram (see Figure 8). The circuit diagram is very easy to follow, there are no complicated connections.

Autonomous Car
Figure 8: Connection of the motor driver L298, and line follower sensors

Raspberry Pi mobile power supply:

Since the car is supposed to be movable, it is necessary to connect it to supply lines from a power supply unit. As I mentioned earlier the L298N Motors driver breakout board also has the +5v, which can be used to power up the Raspberry Pi. What you can do is simply connect the +5v pin from the L298 Dual Motor driver with the 5V GPIO pin i.e with pin2. Or if you want to keep the Motor power source different from the Raspberry Pi power source then another option is to use a small power bank to power up the Raspberry Pi. This why there will be no risk, if there will be any load on motors power supply the raspberry operation will not be affected. You can connect the Power Bank with the Raspberry Pi through the micro USB cable.

Power supply to motors:

The motors of the Dagu Rover 5 chassis run with an operating voltage of 9V. Here is a six-compartment battery for AA batteries (see Figure 9). This is connected to the input for the motor voltage and GND on the L298 module on. The connection for the motor supply is usually connected with VMS or VMOT is designated. If you use other motors that require a higher voltage, That’s no problem either. The L298 is suitable for motor voltages up to 35V. For more details read my article on the L298N motor driver pinout, technical specs, and its use with Arduino.

Autonomous Car
Figure 9: Two battery compartments for six AA batteries

Alignment and initial tests:

You have now assembled and wired all individual components. The Raspberry Pi should now be powered up. Print then a black line about 1 cm wide on a white sheet of paper. Now place the sheet of paper under the sensor fork so that the black line is between the two IR Sensors. The LEDs on the two sensors should now illuminate, which indicates that the infrared light is reflected from the white paper. Do some final adjustments if necessary also check the distance. This should ideally be 3mm (see Figure 10). Adjust the sensors if necessary. Now slide the black line underneath the sensors, and make sure that the LED goes out as soon as the line is pushed into the sensor area (see Figure 11).

Autonomous Car
Figure 10: The sensor fork with the connected line follower sensors


Autonomous Car

The Software:

You should have done all  the mechanical and electrical tasks at this point. The sensors are also adjusted and react as desired. So now you can turn to the software. We will use Python as the programming language which works great with the Raspberry Pi. We have divided the program into two parts: First part of the program all GPIO pins are defined and the required functions are created. We have added the possibility of PWM control of the two enable inputs. In order you can control the speed of the vehicle. We have found, that the line can be missed when the car is at maximum speed break loose. You can find your ideal speed by trying it out. Adapt to this the values ​​in pwmX.start (n) in the range from 0 to 100.

#! / usr / bin / python3
# coding = utf -8
# Auto.py file, part 1
from time import sleep
import RPi. GPIO as GPIO
import sys
GPIO. setmode (GPIO. BOARD)
EnableA = 13
EnableB = 15
Input1 = 3
Input2 = 5
Input3 = 7
Input4 = 11
# the two line followers (r) real and (l) inks
lf_r = 18
lf_l = 22
GPIO. setup (EnableA, GPIO .OUT)
GPIO. setup (EnableB, GPIO .OUT)
GPIO. setup (Input1, GPIO .OUT)
GPIO. setup (Input2, GPIO .OUT)
GPIO. setup (Input3, GPIO .OUT)
GPIO. setup (Input4, GPIO .OUT)
GPIO. setup (lf_l, GPIO .IN, pull_up_down = GPIO. PUD_DOWN)
GPIO. setup (lf_r, GPIO .IN, pull_up_down = GPIO. PUD_DOWN)
pwmA = GPIO. PWM (EnableA, 100)
pwmB = GPIO. PWM (EnableB, 100)
# Function for driving straight ahead
def before ():
pwmA. start (90)
GPIO. output (Input3, True)
GPIO. output (Input4, False)
pwmB. start (90)
GPIO. output (Input1, True)
GPIO. output (Input2, False)
# Left turn function
def left ():
pwmA. start (90)
GPIO. output (Input1, True)
GPIO. output (Input2, False)
pwmB. start (90)
GPIO. output (Input3, False)
GPIO. output (Input4, True)
# Function to turn right
def right ():
pwmB. start (90)
GPIO. output (Input3, True)
GPIO. output (Input4, False)
pwmA. start (90)
GPIO. output (Input1, False)
GPIO. output (Input2, True)

The second part of the program now contains the actual logic of the Autonomous car or the self driving car. You recognize, that the way it works is really simple. if queries constantly check the State of the sensors. If the left sensor hits the line, the car follows right and vice versa. If both sensors only see the white paper, the Line will be located exactly between the sensors, and the car will go straight until one of the sensors recognizes the line again. The waiting time sleep (0.01) is more experimental. If you notice that your Autonomous car is the If the line is partially overlooked, simply reduce the waiting time.

# Auto.py file, part 2
while True:
if GPIO. input (lf_l) == False and GPIO. input (lf_r) == True:
right ()
elif GPIO. input (lf_r) == False and GPIO. input (lf_l) == True:
Left ()
in front ()
sleep (0.01)
except KeyboardInterrupt:
GPIO. cleanup ()
sys. exit ()

Final Testing:

Now think of a line shape. Print out some lines, and fix the paper sheets one after the other on the floor so that a continuous Lineage is created. You can make some curves to add a little complexity to check how your autonomous or self driving car behaves. Then you can place your robot car on the sheet with the lines between the IR sensors (see Figure 12). Using SSH, start the Python program. The car is now starting to move and is traveling along the black Line to finish.

Autonomous Car
Figure 12: final testing


Engr Fahad

My name is Shahzada Fahad and I am an Electrical Engineer. I have been doing Job in UAE as a site engineer in an Electrical Construction Company. Currently, I am running my own YouTube channel "Electronic Clinic", and managing this Website. My Hobbies are * Watching Movies * Music * Martial Arts * Photography * Travelling * Make Sketches and so on...

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button