Autonomous car with Raspberry pi 4 and Open CV

TJM
3 min readJun 26, 2021

--

In this blog we will be describing the process of our first Raspberry pi 4 project. This publication is written as a memory post for us to make further improvements on the project and to give an idea for a beginner to what problems we encountered and how best we could solve them. Also we have done this project with minimum cost and hardware requirements.

Thejaka Mahaulpatha & Sahan Mendes — 2021

Requirements

  1. Raspberry pi 4 ( Here we assume you have a basic setup with a display, keyboard and a mouse)
  2. zk-4wd Car Chassy ( wheels and 4 DC motors incuded)
  3. 8 * 1.5V batteries to power the motors
  4. Power bank to power the Raspberry pi 4
  5. Raspberry pi camera — V2 Camera Module, CSI-2, 3280 x 2464 Resolution (You can use any other suitable after a bit of googling)
  6. LD293 IC ( You can use the LD298 as well)
  7. Breadboard
  8. Female to male jumper wires ( About 10 )
  9. Male to male wires ( About 10 )
  10. wifi connection

Steps

1. Assemble the Car

You can refer to the manual the kit has or can follow the below link. We just put everything together from what we had. You can do it better by having the a neat setup than ours.

2. Create a simple Lane for the car to run on

The lane you create should be white and black borders to make it better contrast.

3. Connect the raspberry pi and the motors through a bread board

You can follow a simple guides that are available in the internet. I have listed what we have used below.

4. Write the code required with Python

The code was modified by including our own and freely available codes in the internet. You need to find your own solutions with the problems you encounter. I have mention some of these encounters at the end of this article. The code is available in the git with below link.

4. Run and test

The first encounter was as the zk 4WD does not come with a steering motor we had rotate by turning the wheels in opposite direction.

The second was that we had to deal with many noice in the path detected and had to apply Gaussian blur to reduce.

As we used a basic breadboard and the IC had only 2 PWMs we could not control the speed of all the 4 wheels hence we had to slow down the car by adding some sleep functions inside. I know this is not the best way but at the time it was.

The angles taken by the car was too large for smaller angles. Also the time between the detection and the action of motors was bit too big making it delay the response. We are trying to make the this run much better and accurately. But that is for another day.

--

--