Build your own robot car with the Raspberry Pi!

Build your own robot car with the Raspberry Pi!

A L298N motor driver or dual H-Bridge / H-Bridge is used to control the direction and speed of motors. The logic of the Raspberry Pi computer cannot handle this task directly and therefore a motor driver is required. A Single Board Computer (SBC) like the Raspberyy Pi does not have the power to supply DC motors or stepper motors with sufficient energy. Normally an SBC operates with voltages of 3.3V or 5.0V and a very weak current of about 30 mA. A classic DC motor, on the other hand, works with voltages from 3V to 24V and a current of 600mA to 4A. A stepper motor operates with a current of 400mA to 1.2A at a voltage of 12V.

There are different types of L298N motor drivers which are dependent on the used motors (DC motors, stepper motors, brushless motors etc.). For small robots DC motors with a voltage of 3V to 24V and a current of 0.3A to 2A are used and for these an H-bridge as shown here is sufficient. For strong high performance brushless motors from RC model making special speed controllers are required.

L298N Dual H-Bridge DC motor driver modules for Arduino and Raspberry Pi

The following motor driver is an inexpensive model which has no LEDs to show the status of the individual channels. I often use this type of motor driver in my robot cars. This motor driver is sufficient for the small DC motors in the robot cars built here. With the pulse width modulation PWM it is possible to control the speed of the motors per channel.

Loading ...

Python program – L298N H-bridge

For the control of the L298N motor driver I have developed a Python program. With this program, which you integrate as a module into your main program, it is possible to control two motors of the robot. The Python module has the following functions for controlling the motors:

  • setMotorMode(motor, mode)
  • setMotorLeft(power)
  • setMotorRight(power)
  • exit()

Within the program the individual functions are further explained in the source code.

setMotorMode(motor, mode)

The first input parameter “motor” determines whether the left or right motor should be addressed. The second input parameter “mode” defines the direction of the selected motor.

setMotorLeft(power) / setMotorRight(power)

With the input parameter “power” the direction of rotation (+/-) and the speed (0 – 1) is transferred.


The function “exit()” has no input parameter and ends the program cleanly by setting the GPIO outputs to False and releasing them again.

The module for controlling the H-bridge as well as an example program for controlling the module are available for download on my site. Here you can download the Python programs: L298N software download

Technical description – L298N H-bridge

The motor drivers, expressed as an L298N H-bridge for small motors with a current of no more than 2 A, for example, have the following typical specification. In general, however, there are H-bridges of very different sizes and designs. I have decided to use the cheaper version for my models which I present here. It has LEDs and indicates with these which logical connection is currently active, i.e. has been switched to HIGH. This simplifies the search for the right sensor in the program or in the cabling.

I have summarized the typical components for a robot car on the following page: Robot Car Components

Specification L298N motor driver:

Driver chip: L298N Dual H Bridge DC Motor Driver IC
Driven part of the terminal supply voltage: VMS 5V ~ 24V
Driven part of the peak current IO: 2A per bridge
The logical part of the terminal supply voltage: VSS 4.5V ~ 5.5V
The logical part of the operating current range: 0mA ~ 36mA
Control signal input voltage range: 4.5V ~ 5.5V (high) / 0V (low)
Max power consumption: 20W
Operating temperature: -25°C ~ +130°C
Size: 55 x 60 x 30mm

The following picture shows a L298N H-bridge and the four LEDs below the heat sink are clearly visible, indicating the switching states of the logic inputs.

l298n motor controller LED

l298n motor controller LED

5V power supply

The 5V output of the motor driver, which is available via one of the terminals, is not stable enough to supply the Raspberry Pi with sufficient current. If a higher supply voltage than 12V is applied to the VMS input, this will damage the Raspberry Pi when connected to the 5V output of the motor driver. The built in voltage walk is not good enough to connect an Arduino or Rasbperry Pi to it.

Stepper motor driver

With the two motor drivers presented here not only DC motors can be operated. It is also possible to operate a four phase stepper motor with them. The voltage and current are sufficient to connect standard NEMA17 stepper motors to the motor driver.

(Visited 1,003 times, 4 visits today)