DC motor control using computer vision
Embedded systems course · project
Raspberry Pi 4B · OpenCV · C · L298N

In the third embedded systems lecture, I introduced a new development platform: the Raspberry Pi 4B. It’s a small computer on which you can install Debian (a version of Ubuntu). It also has GPIO pinouts for communication and other tasks.
The main goal of this project was to control a car platform created by previous students. We attempted to control two 9V DC motors, and using computer vision, we tried to follow this platform. Normally, the goal would have been to track a specific human, such as face tracking, but in the end, the performance wasn’t ideal.
I used a specific color object that could mimic my clothing color, and I worked solely with the OpenCV library, without using YOLOv8 or any other versions. Additionally, we had to control the motors strictly using C language, as it was a project requirement.
For the motor control, I used the L298N motor driver, which allows you to control two DC motors using a single driver. The system is powered by a 12V lead-acid battery, which feeds the driver. By using enable pins, we can control one motor with two pins.
In this application, velocity is not relevant, and the logic works as follows:
- When an object appears in the camera view, we draw a blob. If the area of the blob decreases, the platform moves forward, with both motors rotating in the same direction.
- If the blob area increases, the platform moves backward with the motors rotating in an anticlockwise direction.
- The platform has four wheels, with two of them being passive, and there is no steering angle. To turn left or right, I use the center point of the blob. If the center moves to the right, the robot moves right and tries to re-center the point. The same logic applies for the left.
- For rotation, we reverse the direction of the two motors—one rotating clockwise and the other counterclockwise.
Since we were strictly required to use C language for controlling the DC motors, I did not know C++ at the time. To communicate between Python and C, I found a shared library file (.so). The program starts with Python code, which creates the .so file containing the x,y,scale. Then, the C code uses this library to determine the motor direction.
Unfortunately, the system could not be tested on the ground due to the lack of an independent battery source for the Raspberry Pi.
Below, I’m sharing the GitHub link and a video of the process.