STM32 DC Motor Speed Control PWM With L293D – Motor Driver Library Examples

Previous Tutorial Previous Tutorial Tutorial 38 Next Tutorial Next Tutorial
STM32 DC Motor Speed PWM Control Examples & Driver (Library)
STM32 Course Home Page ????

 

STM32 DC Motor PWM Speed And Direction Control With L293D Tutorial

 

In this tutorial, we’ll be discussing DC motor speed control with STM32 PWM and L293D H-bridge motor driver. I’ll also show you the DC_MOTOR library (driver) that I’ve developed for STM32 microcontrollers and discuss how it works and how it’s been built in this way. And we’ll create 3 different example projects with STM32 uC and DC motors.

We’ll conclude this tutorial with some tests, measurements, and spot the light on potential improvements and features that you can make and add to this driver library code. But first of all, we’ll discuss how the DC Motor & L293D H-Bridge Driver work and how to control the DC motor’s speed and direction of rotation.

In this tutorial: 3 LABs

LAB 45 1 DC Motor Speed Control With Tip122 Transistor – Testing Our Motor Library
LAB 46 1 DC Motor Control With L293D – Testing Our Motor Library
LAB 47 2 DC Motors Control With L293D – Testing Our Motor Library

[toc]


   Required Components For LABs   

 

All the example code/LABs/projects in the course are going to be done using those boards below.

QTY Component Name ???? Amazon.com ???? eBay.com
2 BreadBoard Amazon eBay
1 LEDs Kit Amazon Amazon eBay
1 Resistors Kit Amazon Amazon eBay
1 Capacitors Kit Amazon Amazon eBay & eBay
2 Jumper Wires Pack Amazon Amazon eBay & eBay
1 9v Battery or DC Power Supply Amazon Amazon Amazon eBay
1 Micro USB Cable Amazon eBay
2 Push Buttons Amazon Amazon eBay
2 DC Motor 12v Amazon eBay
1 DC Motor Driver (H-Bridge) Amazon Amazon eBay  eBay
1 Potentiometers Amazon Amazon eBay

★ Check The Full Course Complete Kit List

Some Extremely Useful Test Equipment For Troubleshooting:

Affiliate Disclosure: When you click on links in this section and make a purchase, this can result in this site earning a commission. Affiliate programs and affiliations include, but are not limited to, the eBay Partner Network (EPN) and Amazon.com.


  DC Motor Control & L293D H-Bridge Driver  

 

At the beginning of this tutorial, I’d like to make sure that all of you have the basic concepts behind DC motors operation and how we use PWM signals to control the speed of a DC motor. How an H-Bridge works and does the polarity-inversion to control the direction of the motor’s rotation. All of that and more are discussed in this previous tutorial about DC motor control with L293D driver. Check it out and come back to resume developing our DC motor driver library for STM32 microcontrollers.

DC Motor Speed Control - PWM With L293D IC

This file is the datasheet for the H-Bridge IC we’ll be using in this tutorial (L293D). Which is capable of delivering 0.6A of current per CH for 2 independent channels (2 motors). The motor supply voltage VM can be in the range from 4.5v up to 36v. You’ll also find the most important piece of information which is the pinout diagram and also a PCB layout example if you’re designing your own board, it’s going to be a good reference.

STM32 DC Motor Speed Control L293D H-Bridge Pinout

Near the end of the datasheet document, you’ll find some application examples. The most important one for us is the bidirectional motor control using one channel of the L293D IC. Here is the given figure for that configuration. And note that the fly-back diodes are not integrated within the IC. Therefore, we may include them in the wiring diagram later on. If not placed, you can still have everything running just as fine. As long as, your motor’s load is moderate. As it goes up, the IC will start heating up.

STM32 With L293D DC Motor Direction Control

 


 STM32 With L293D Motor Driver IC 

Connection Diagram

STM32 DC Motor Speed Control L293D H-Bridge Connection Diagram

Guidelines For Designing Our DC Motor Driver (Library)

Here are some guidelines and requirements that I did consider before designing the DC_MOTOR driver library with the current APIs. It can be improved in the future but for the beginning, it should be in line with the following:

  • Has Configurable TIMERx and CHx for eachDC_MOTOR instance
  • Multiple DC_MOTOR Instances Should Be Running Smoothly On the Same Timer With No Intervention (The PWM Frequency & Resolution Shall Be Shared Among Multiple Motors On The Same Timer).
  • The User has full control over the PWM Resolution for motor speed control
  • The PWM Frequency shall be an approximated value to what the user selects

 


   STM32 Dc Motor Driver (Library)   

 

The ECUAL DC_MOTOR driver is built for STM32 microcontrollers using one Timer channel in PWM mode. You’ll have to configure an instance of it and use the APIs to initialize, start, change speed, stop your motor, and that’s all. The code should be easily ported to any other STM32 microcontroller or reconfigured to use any Timer and PWM Channel that you want just as we’ll see in this section. And here is a link for the course’s repo, and you’ll find the DC_MOTOR driver in the ECUAL directory as usual.

DC MOTOR Driver Code Files

The DC_MOTOR driver consists of the following files:

  • DC_MOTOR.h
  • DC_MOTOR.c
  • DC_MOTOR_cfg.h
  • DC_MOTOR_cfg.c
STM32 DC Motor Control PWM L293D Driver Library Files

You’ll need only to modify the configuration files. The source code for this driver is found in (DC_MOTOR.c) and to use it you’ll include the header (DC_MOTOR.h). Nothing in the source code needs to be changed at all unless you need to add any extra features or customize the driver for your application’s needs. For today’s labs, we’ll only be changing the configuration files to build some test applications.

Therefore, I’ll write here the code listing for the DC_MOTOR.h & DC_MOTOR_cfg.c files.

DC_MOTOR.h File

If you’re willing to use multiple DC Motors, just adjust that number definition (DC_MOTOR_UNITS).

DC MOTOR Driver APIs

As you’ve seen in the DC_MOTOR.h file, the provided APIs do all the basic functionalities that you may need from a motor driver library.

DC_MOTOR_Init: initializes the required GPIO pins for direction control logic (DIR1 & DIR2), the associated Timer with the selected PWM channel. And sets the PWM resolution and frequency as configured by the user in the config file.

DC_MOTOR_Start: Sets the motor direction and speed to start the motor

DC_MOTOR_Set_Speed: Sets the speed of the motor by adjusting the PWM duty cycle

DC_MOTOR_Set_Dir: Sets the direction of rotation for the motor (CW or CCW)

DC_MOTOR_Stop: Stops the motor

DC_MOTOR_GET_MaxFreq: Returns the maximum operating frequency (in Hz) for the PWM channel at the selected PWM resolution in the config structure for that specific motor

The example LABs will put everything under test and you’ll see how and when to use each of these functions.

DC_MOTOR_cfg.c File

I’ll discuss those configuration parameters in the next section down below.

Available Configurations For ECUAL DC Motor Driver

From the code above in DC_MOTOR.h & DC_MOTOR_cfg.c you can see that there are a handful of parameters in the configuration structure. The config structure will be used to assign the DIR1 & DIR2 GPIO pins, the associated TIMER peripheral you’ll be using (it can be TIM1, 2, and so on), the PWM_TIMER channel you’ll be using for the motor ENABLEx pin, the CLK speed in MHz for that specific TIMER module, the PWM frequency (in Hz), and finally the PWM resolution (in bits).

Typical Usage Application Example

Here is a typical usage application for this driver code in order to initialize & start a DC Motor, read an analog potentiometer with ADC. And use that reading to control the motor speed in the main loop of the system.

PWM Resolution & Frequency Calculations

As I’ve stated earlier in this tutorial, my DC Motor driver (library) gives the user full control over the PWM signal’s resolution. And on the other hand, it does approximate the PWM frequency given by the user. In other words, for a configuration like this (desired resolution = 10-Bits, desired FPWM = 1kHz).

The actual PWM signal will have exactly 10Bits of resolution (0 up to 1023 speed levels) but the FPWM will be 1.003kHz as shown below. Not bad right? of course it’s not an exact value like the resolution but it’s just as fine as we won’t need that much FPWM accuracy in DC motor control applications.

STM32 DC Motor Control L293D Example Testing Driver

This step is done at the initialization function for the DC Motor driver. Check the source code of the DC_MOTOR_Init function. And here is what I’m doing: first of all, we need to set the Timer’s Prescaler (PSC) and auto-reload register’s value (ARR) to control both PWM signal’s frequency and resolution.

This is the first formula for PWM resolution. We’ll solve for ARR given the user-defined resolution (in bits) and other parameters. We can easily solve for ARR value.

STM32 PWM Resolution General Equation - Formula

And now after knowing the ARR value, we can plug that into the FPWM equation and solve for PSC to find the approximate Prescaler value for the timer to get an FPWM that is as close as possible to the pre-defined value by the user.

STM32 PWM Frequency Formula - STM32 PWM Frequency Equation

 


 STM32 DC Motor Speed Control With Tip122 – LAB 

 

LAB Number 45
LAB Title STM32 DC Motor Speed Control With Tip122 Transistor
  • Set up a new project as usual with a system clock @ 72MHz or whatever your uC board supports
  • Enable TIMERx/CHx For PWM mode in CubeMX
  • Enable Any ADC Channel in CubeMX
  • Add the ECUAL / DC_MOTOR driver files to our project. As shown here
  • Configure 1 DC_MOTOR instance in DC_MOTOR_cfg.c file
  • Initialize the DC_MOTOR in the main application, start the DC Motor, read an analog potentiometer with ADC. And use that reading to control the motor speed in the main loop of the system.

 

STM32 DC Motor Speed Control With Tip122 Transistor

 

And now, let’s build this system step-by-step

 

Step1: Open CubeMX & Create New Project

Step2: Choose The Target MCU & Double-Click Its Name

STM32F103C8 (the one I’ll be using) or any other STM32 part you’ve got

Step3: Go To The RCC Clock Configuration

Step4: Set The System Clock To Be 72MHz or whatever your uC board supports

Step5: Enable The TIMERx/CHx PWM That You’ll be using For L293D ENABLEx Pin

We’ll have to do this step just to have the TIM_HAL files added to our project by CubeMX. And also it gives us a startup configuration for the PWM channel of that specific timer module as we’ll see hereafter. This is the best way to keep the code generic and portable to any STM32 microcontroller.

This step will result in adding a TIMER_Init function in our code by CubeMX, but we’ll remove it afterward as the DC_MOTOR_Init will handle that initialization procedure.

STM32 DC Motor Control PWM With L293D

Step6: Enable The ADCx / CHx That You’ll Be Using For The Potentiometer

STM32 DC Motor Speed Control L293D Example Code

Step7: Generate The Initialization Code & Open The Project In Your IDE

Step8: Add the ECUAL/ DC_MOTOR driver files to your project

Follow This Tutorial which shows you How To Add Any ECUAL Driver To An STM32 Project step-by-step.

Adding ECUAL Driver To STM32 Project

You basically right-click the project name in the IDE’s navigator and choose to create a new > source folder. And name it ECUAL and go to the GitHub repo, download the files and copy the “DC_MOTOR” folder and back to the IDE, right-click on the ECUAL and paste the library into it.

Now, we can start developing our application in the main.c source file.

Here Are The Drivers Configurations I Used For This LAB

DC_MOTOR_cfg.c File

The DC_MOTOR_F_PWM and DC_MOTOR_PWM_RES are defined in the DC_MOTOR.h file. The FPWM is 500Hz and the resolution is 10-Bits.

Here is The Application Code For This LAB (main.c)

Note that: I’ve deleted the auto-generated TIM2 initialization function and its parameter. Since our DC Motor library will handle that, we don’t need that function at all.

Do also look into the stm32f1xx_hal_msp.c file. You’ll find some initialization functions that get called in main.c at this line ( HAL_Init(); ),. While one of which is being used by my library for the DC Motor initialization routine. Which is the Timer PWM pin initialization.

As you can see, it does handle the PWM pin initialization for us for TIMER2 / CH1 pin. This can be hectic to do in our library init function ( DC_Motor_Init() ) in a generic way. So I’ve decided to use that function instead to keep the code more generic across all STM32 microcontrollers. Without having to figure out a way to find out which pin belongs to which timer’s channel and all that stuff.

Download The STM32 DC Motor Speed Control LAB45

STM32 DC Motor Speed Control With Tip122 Example

The Result For This LAB Testing (Video)

Note that: this LAB is just a test for the basic functionality of our DC Motor Control library. Which is mainly built for L293D drivers and any H-Bridge that share the same control logic. That’s why the DIR1 & DIR2 pins are not useful in this LAB, despite the fact that they’re already initialized and reserved by the DC Motor library for DC_MOTOR1 structure.

 


 STM32 DC Motor Control With L293D – LAB 

 

LAB Number 46
LAB Title 1 DC Motor Control With L293D – Testing Our Motor Library
  • Set up a new project as usual with a system clock @ 72MHz or whatever your uC board supports
  • Enable TIMERx/CHx For PWM mode in CubeMX
  • Enable Any ADC Channel in CubeMX
  • Add the ECUAL / DC_MOTOR driver files to our project. As shown here
  • Add the ECUAL / BUTTONS driver files to our project
  • Configure 1 DC_MOTOR instance in DC_MOTOR_cfg.c file
  • Configure 1 BUTTON instance in BUTTONS_cfg.c file
  • Initialize the BUTTON & DC_MOTOR in the main application, start the DC Motor, read an analog potentiometer with ADC. And use that reading to control the motor speed in the main loop of the system. Read the buttons and use it to flip the direction of motor rotation. Repeat!

 

STM32 DC Motor Speed Control With L293D Driver IC Connection

 

And now, let’s build this system step-by-step

 

Step1: Open CubeMX & Create New Project

Step2: Choose The Target MCU & Double-Click Its Name

STM32F103C8 (the one I’ll be using) or any other STM32 part you’ve got

Step3: Go To The RCC Clock Configuration

Step4: Set The System Clock To Be 72MHz or whatever your uC board supports

Step5: Enable The TIMERx/CHx PWM That You’ll be using For L293D ENABLEx Pin

We’ll have to do this step just to have the TIM_HAL files added to our project by CubeMX. And also it gives us a startup configuration for the PWM channel of that specific timer module as we’ll see hereafter. This is the best way to keep the code generic and portable to any STM32 microcontroller.

This step will result in adding a TIMER_Init function in our code by CubeMX, but we’ll remove it afterward as the DC_MOTOR_Init will handle that initialization procedure.

STM32 DC Motor Control PWM With L293D

Step6: Enable The ADCx / CHx That You’ll Be Using For The Potentiometer

STM32 DC Motor Speed Control L293D Example Code

Step7: Generate The Initialization Code & Open The Project In Your IDE

Step8: Add the ECUAL/ DC_MOTOR driver files to your project

Follow This Tutorial which shows you How To Add Any ECUAL Driver To An STM32 Project step-by-step.

Adding ECUAL Driver To STM32 Project

You basically right-click the project name in the IDE’s navigator and choose to create a new > source folder. And name it ECUAL and go to the GitHub repo, download the files and copy the “DC_MOTOR” folder and back to the IDE, right-click on the ECUAL and paste the library into it.

Step9: Add the ECUAL/ BUTTONS driver files to your project

The same as the previous step

Step10: Add the MATH files to your project

Now, we can start developing our application in the main.c source file.

Here Are The Drivers Configurations I Used For This LAB

DC_MOTOR_cfg.c File

The DC_MOTOR_F_PWM and DC_MOTOR_PWM_RES are defined in the DC_MOTOR.h file. The FPWM is 500Hz and the resolution is 10-Bits.

BUTTONS_cfg.c File

Here is The Application Code For This LAB (main.c)

Note that: I’ve deleted the auto-generated TIM2 initialization function and its parameter. Since our DC Motor library will handle that, we don’t need that function at all.

Download The STM32 DC Motor Speed Control With L293D LAB46

STM32 DC Motor Speed Control With L293D Example Wiring

The Result For This LAB Testing (Video)

 


 STM32 Multi DC Motor Control L293D – LAB 

 

LAB Number 47
LAB Title 2 DC Motors Control With L293D – Testing Our Motor Library
  • Set up a new project as usual with a system clock @ 72MHz or whatever your uC board supports
  • Enable 2 TIMERx/CHx For PWM mode in CubeMX
  • Enable Any 2 ADC Channels in CubeMX
  • Add the ECUAL / DC_MOTOR driver files to our project. As shown here
  • Add the ECUAL / BUTTONS driver files to our project
  • Configure 2 DC_MOTOR instance in DC_MOTOR_cfg.c file
  • Configure 2 BUTTON instance in BUTTONS_cfg.c file
  • Initialize the BUTTONs & DC_MOTORs in the main application, start the DC Motors, read the analog potentiometers with ADC. And use that reading to control the 2 motor speeds in the main loop of the system. Read the buttons and use that signal to flip the direction of the 2 motors rotation. Repeat!

 

And now, let’s build this system step-by-step

 

Step1: Open CubeMX & Create New Project

Step2: Choose The Target MCU & Double-Click Its Name

STM32F103C8 (the one I’ll be using) or any other STM32 part you’ve got

Step3: Go To The RCC Clock Configuration

Step4: Set The System Clock To Be 72MHz or whatever your uC board supports

Step5: Enable The 2 TIMERx/CHx PWMs That You’ll be using For L293D ENABLE1, 2 Pins

We’ll have to do this step just to have the TIM_HAL files added to our project by CubeMX. And also it gives us a startup configuration for the PWM channel of that specific timer module as we’ll see hereafter. This is the best way to keep the code generic and portable to any STM32 microcontroller.

This step will result in adding a TIMER_Init function in our code by CubeMX, but we’ll remove it afterward as the DC_MOTOR_Init will handle that initialization procedure.

STM32 Multi DC Motors Control L293D Example Project L47

Step6: Enable The 2 ADCx / CHx That You’ll Be Using For The 2 Potentiometers

STM32 Multi DC Motors Control L293D Example Code L47

Step7: Generate The Initialization Code & Open The Project In Your IDE

Step8: Add the ECUAL/ DC_MOTOR driver files to your project

Follow This Tutorial which shows you How To Add Any ECUAL Driver To An STM32 Project step-by-step.

Adding ECUAL Driver To STM32 Project

You basically right-click the project name in the IDE’s navigator and choose to create a new > source folder. And name it ECUAL and go to the GitHub repo, download the files and copy the “DC_MOTOR” folder and back to the IDE, right-click on the ECUAL and paste the library into it.

Step9: Add the ECUAL/ BUTTONS driver files to your project

The same as the previous step

Step10: Add the MATH files to your project

Now, we can start developing our application in the main.c source file.

Here Are The Drivers Configurations I Used For This LAB

DC_MOTOR_cfg.c File

The DC_MOTOR_F_PWM and DC_MOTOR_PWM_RES are defined in the DC_MOTOR.h file. The FPWM is 500Hz and the resolution is 10-Bits.

BUTTONS_cfg.c File

Here is The Application Code For This LAB (main.c)

Note that: I’ve deleted the auto-generated TIM2 initialization function and its parameter. Since our DC Motor library will handle that, we don’t need that function at all.


Download The STM32 Multi DC Motor Control L293D Driver LAB47

 

The Result For This LAB Testing (Video)

 


 Questions I Usually Get About DC Motor Control 

 

PWM Frequency For DC Motor Speed Control?

This is one of the interesting questions that I’ve received a couple of times in the past and led us to a nice discussion about the matter. What you’d always hear regarding this topic is that FPWM should be not too low not too high for a reliable DC motor speed control.

And it can be really vague to decide which frequency is too low or high. But the commonly suggested values lie between 200Hz up to 2kHz or 5kHz. This is stated in many motor drivers’ ICs datasheets as the maximum switching frequency as well. While in other cases, the favored frequency can be up to 20kHz so as not to have any audible noise from the electronics side of the system. This can’t always be the case for all sorts of applications.

In fact, there is strong reasoning behind this being true. Which is a topic for a future article I’ll be publishing to show you my testing results and how to model a DC motor and take the parameters to MATLAB and simulate what’s going on and how FPWM affects the actual motor speed which can be weird to hear. Most of us at the beginning think that only the duty cycle controls the DC motor speed. However, it’ll turn out to be not exactly true, also the FPWM has a strong effect if not picked correctly as we’ll see.

PWM Resolution For DC Motor Speed Control?

Now, let’s see what value you should pick for the PWM resolution. It totally depends on the levels of control you want to have over the motor’s speed. And it should be the same as the ADC’s resolution for the potentiometer used for control.

If your ADC is 10-bits, there won’t be any reason to have a PWM resolution that goes beyond 10-bits as well. But generally speaking, having 10-bits of resolution is more than enough in most applications. And if you’re going to use my library as it is, you’ll have to keep in mind that the speed values you’re sending to the control function should be correct for the resolution you’ve selected. For example (Desired Resolution = 8bit, so the max speed is 255 & min is 0) and so on.

DC Motor Position Control

How to do position control using a DC Motor? well, this sounds like willing to create a servo motor. Basically, you’ll need to have some sort of feedback to tell the controller where is the DC Motor’s shaft. It can be physical contact with a rotary potentiometer or an optical encoder. I’ll try to include a similar tutorial for this in the future as well. But you can search this topic online to find more ideas and techniques to achieve servo motor control using DC motors.


Did you find this helpful? If yes, please consider supporting this work and sharing these tutorials!

 

 

Stay tuned for the upcoming tutorials and don’t forget to SHARE these tutorials. And consider SUPPORTING this work to keep publishing free content just like this!

 

 

Previous Tutorial Previous Tutorial Tutorial 38 Next Tutorial Next Tutorial
Share This Page With Your Network!
Join Our +25,000 Newsletter Subscribers!

Stay Updated With All New Content Releases. You Also Get Occasional FREE Coupon Codes For Courses & Other Stuff!

Photo of author
Author
Khaled Magdy
Embedded systems engineer with several years of experience in embedded software and hardware design. I work as an embedded SW engineer in the Automotive & e-Mobility industry. However, I still do Hardware design and SW development for DSP, Control Systems, Robotics, AI/ML, and other fields I'm passionate about.
I love reading, writing, creating projects, and teaching. A reader by day and a writer by night, it's my lifestyle. I believe that the combination of brilliant minds, bold ideas, and a complete disregard for what is possible, can and will change the world! I will be there when it happens, will you?

1 thought on “STM32 DC Motor Speed Control PWM With L293D – Motor Driver Library Examples”

Leave a Comment