Engineers Garage

  • Electronic Projects & Tutorials
    • Electronic Projects
      • Arduino Projects
      • AVR
      • Raspberry pi
      • ESP8266
      • BeagleBone
      • 8051 Microcontroller
      • ARM
      • PIC Microcontroller
      • STM32
    • Tutorials
      • Audio Electronics
      • Battery Management
      • Brainwave
      • Electric Vehicles
      • EMI/EMC/RFI
      • Hardware Filters
      • IoT tutorials
      • Power Tutorials
      • Python
      • Sensors
      • USB
      • VHDL
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Product News
    • Business News
    • Company/Start-up News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • DigiKey Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Learn
    • eBooks/Tech Tips
    • Design Guides
    • Learning Center
    • Tech Toolboxes
    • Webinars & Digital Events
  • Resources
    • Digital Issues
    • EE Training Days
    • LEAP Awards
    • Podcasts
    • Webinars / Digital Events
    • White Papers
    • Engineering Diversity & Inclusion
    • DesignFast
  • Guest Post Guidelines
  • Advertise
  • Subscribe

Dc motor speed and direction control with stm32f103 microcontroller and l293d motor driver

By EG Projects September 4, 2021

In this tutorial i am going to teach you how to control speed of dc motor using stm32 microcontroller and l293d half h bridge control ic. Dc motors are common motors you see in your houses(DC water pumps, DC fans), toys etc. DC motors rotate at a constant speed but their speed can be varied and controlled by utilizing few techniques. One such technique and the most popular is to supply a variable voltage to it. By variable voltage i mean to supply less amount of voltage(power) than maximum rating of a particular dc motor. Like our roof fans(work on Ac voltage) rotating speed is controlled by varying the potentiometer(variable resistor) connected in the buttons socket. The resistor varies the power when we rotate its knob in either directions.
Variable resistor is a good choice to control motor speed. But if we want to insert some control logic(e.g changing motor rotation direction) or accurately control the motors rpm (resolutions per minute) then variable resistor technique can not full fill our requirement. To achieve the upper logic pwm(pulse width modulation) technique is best suited. In pulse width modulation input constant voltage to motor is divided in to sub cycles to reduce the amount of input voltage.
In this project our purpose is to control the speed of motor with stm32 microcontroller. Pwm technique is utilized in the project to control the motor speed and direction. St32f103c8t6 micorcontroller pre assembled board is used in the project. Stm32cubemx is used for stm32f103c8t6 microcontrollers gpio, timers configuration. Keil arm mdk 5 is used for writing and compiling the project code. Code is written by utilizing stm32 HAL libraries. If you are new and don’t know about stm32cubemx, keil arm mdk 5 and HAL libraries i advise you to first take the getting started tutorial with stm32cubemx and keil arm and HAL libraries. Just click the below button to take the tutorial.   

Getting started stm32cubemx, keil arm and HAL libraries

I am going to control the speed of two dc motors connected to l293d half h bridge ic. What is l293d? Microcontrollers works on 3.3 to 5 volt TTL logic. Where as dc motors work on 5 to +24 volts. Its not possible to run motors directly from the microcontoller output pins. Microcontroller output pins can source only 5 volts and 25 mA of current which is not suitable for dc motors to start rotation. Stm32 microcontrollers works on 3.3 volt TTL logic. For this project i am using 12 volt dc motors which require 200 mA of current for constant rotation. So we need an external circuit to drive motor with it using stm32 microcontroller.     

Transistor or Mosfet as external circuit to drive, control the speed and direction of DC motor

An external transistor or mosfet is a best choice to drive a 12 volt motor using stm32 microcontroller. The base of transistor, mosfet is connected to stm32 output pin and motor will be inserted between collector of transistor. A variable pwm signal to the base of transistor from stm32 output pin can easily control the rotation speed of dc motor. But we also require to change the direction of motor which is not possible in this configuration.  

Half h bridge circuit for dc motor direction control

In half h bridge circuit two transistors are used to control the direction of dc motor rotation. Making a half h bridge circuit with requirements of dc motor power is not an easy task. L293d is a pre assembled ic containing two half h bridge configurations in it. We can drive, control direction and speed of two dc motors easily with l293d motor driver ic.  

L293d single half h bridge channel equivalent circuit

L293d single half h bridge channel equivalent circuit
To learn about pin out and purpose of each pin in l293d you have to take an another tutorial. I explained the l293d circuit and its working in the tutorial very deeply. Tutorial will help you in understanding the circuit diagram of the project below. Click the below button to take the tutorial.

L293d pin out and working

Project working

I am going to control the direction of rotation of dc motors and their speed in the project. For this purpose i decided to connect four buttons as input. Buttons will control the direction and speed of the motors. I will discuss purpose of each button later. For four buttons as input i require four gpio’s of stm32 microcontroller declared as input. I declared the Port-B pins 6, 7, 8 and 9 as input. I also activated the pull up resistors associated with these pins.
To control the direction of two dc motors through half h bridge circuits i need four controls. Basically four pgio pins of microcontroller are required to control the direction of two dc motor rotation. I declared four stm32 microcontroller Port-A pins 0, 1, 2 and 3 as output.      

Finally to control the motor speed i need two pwm signals. I am going to use the Port-B pins 0 and 1 as pwm sources. Generating pwm using stm32 is not an easy task. One has to take care of many constraints. I have an another tutorial on how to generate pwm with stm32 microcontroller. I discussed all the constraints frequency, duty cycle and counter etc in that tutorial. Please take that tutorial before moving any further. If you do not take the tutorial you will be unable to understand the code below.

Stm32 pwm generation formula calculations and constraints

If you have gone through the above tutorial you now probably have know how of how to generate desired pwm with stm32 microcontroller. To control the dc motor speed i have to vary the pwm signal. Varying the pwm signal is not a hard task if you have gone through the above tutorial. Foe varying the pwm signal you need to calculate some more variables. In this project i am generating a variable duty cycle pwm signal on 50 hz frequency. Actually i calculated these values for an another project(Servo motor with stm32 microcontrolelr) and used those values in this project. You can see that project settings if you want. It will help you in stm32cubemx pwm settings.    

Stm32cubemx settings for this project borrowed from another projecct

Stm32cubemx gpio settings and timer pwm settings are given below. You have to take the above tutorials to understand each constraint and how they are calculated and inserted in stm32cubemx.  

Stm32cubemx pwm timer 3 and gpio configurations

Stm32cubemx pwm timer 3 and gpio configurations
I am using internal 8 Mhz oscillator of stm32f103 microcontroller for clock source to gpio pins and for pwm generation. These settings are must to generate the 50 Hz frequency pwm signal as discussed above.

stm32f103 microcontroller pwm clock settings in stm32cubemx

stm32f103 microcontroller pwm clock settings in stm32cubemx
For pwm i am using Port-B pins 0 and 1 of stm32. With both the pins of microcontroller timer-3 is associated. With pin 0 timer 3 channel 3 is linked and with pin 1 timer 3 channel 4 is linked. We have to activate these channels before starting to work with them. How to activate channels? Settings are given below. 

Stm32 timer 3 pwm channel settings in stm32cubemx

Stm32 timer 3 pwm channel settings in stm32cubemx

Project circuit

Port-A pins A0 and A1 are used to control the direction of motor-1. Microcontroller pin A0 is connected to input-1 of l293d and pin A1 is connected to input-2 of l293d. Pwm signal is supplied to enable pin of motor-1 or channel 1 of l293d. Stm32 Port-B pin 1 is connected to enable 1 of l293d. Similarly Port-A pins A2 and A3 of stm32 microcontroller is used to control the speed of motor 2 or channel 2 of l293d. A2 pin of stm32 is connected to input 4 of stm32 microcontroller and A3 pin of stm32 is connected to input 3 pin of l293d channel 2. Enable pin of channel 2 of l293d is connected to pin B0 of stm32 microcontroller. 
Our 12 volt motors are connected with output pins of l293d. Motor 1 is connected against output pins of channel 1 and motor 2 is connected across output pins of channel 2 of l293d motor driver. Ground pins are grounded.
Note: The ground of stm32 microcontroller power and l293d motor power must be commonly grounded for circuit completion and proper working.   

Dc motor speed and direction control with stm32 microcontroller and l293d motor driver

Dc motor speed and direction control with stm32 microcontroller and l293d motor driver

Input buttons functions 

  • B9 – Pressing button B9 will toggle the rotation direction of dc motor. If its moving in forward direction pressing the B9 button will change the direction to back ward. 
  • B8 – Pressing button B8 will set the pwm duty cycle to 75% and it lowers with motor rotation speed.
  • B7 – Pressing button B7 will set the pwm duty cycle to 50%. Dc motor know rotates at half speed than maximum.
  • B6 – Pressing button B6 will set the pwm duty cycle to 25% will further lowers down the rotation speed.

Coming to the code. Main logic of the code is before and between the while 1 loop. If you have gone through the above tutorials on Pwm and stm32cubemx getting started you can easily understand the code statements below. Statements are same as in the previous tutorials only the sequence of statements are changed as desired in the project. 

In the above code first statements before while 1 loop starts both the dc motors and pwm signal is outputted with 100% duty cycle. In while 1 loop each button press is continuously checked. If any button is pressed it corresponding function(direction reversed, speed changed) is executed. I hope the above code is easy for you if you have gone through all the tutorials enlisted in the post.
Future work:  
With the same code and logic above you can not only drive dc motors but also servo motors. In circuit you have to change some settings like input voltage to l293d for servo motor driving(5 to 12 v). Servo motors work on 50 Hz frequency and duty cycle between 1 milli second to 3 milli second. We generated pwm on 50 Hz frequency. So yon can connect a servo at output and check if the code works(It definitely will). You can also fade an led by connecting it at l293d output with the same code, circuit above. You may need to fix the direction and not change it for led. Since led polarity can’t be changed.
Download the project code. Folder contains stm32cubemx and keil arm ide project files. All project libraries and dependencies are included in the folder. Please provide us your feed back on the project.
Code/files

You may also like:


  • What are different types of industrial robots?

  • What types of motors are used in electric vehicles?

  • What is Wireless Electric Vehicle Charging System (WEVCS)?

  • What are the components of robotic arms and industrial robots?

  • What drone parts you need to build a quadcopter?

  • What’s a protective relay and what does it protect?

Filed Under: Electronic Projects, Featured Contributions, STM32

 

Next Article

← Previous Article
Next Article →

Comments

  1. es rauta says

    February 21, 2023 at 2:43 pm

    Hello!
    I was using this project with my STM STEVAL-PCC009V2 with STM32F103RBT6 controller ( L298N instead of l293d) and found the tutorial VERY explaining, clear and informative. Explaining Stm32cubemx I found particular clear. Usually I feel using all this stuff Cubemx/Keil confusing but not here. I use Arduino IDE with Generic STM when possible.
    And all was functioning without hazzle.
    Thanks A LOT!
    ER

    Log in to Reply
    • Bijal Parikh says

      July 23, 2024 at 4:16 am

      Hi, Thanks for writing to us. You can read more articles on http://www.engineersgarage.com
      If you have any technical questions or need more information please visit our forums https://www.edaboard.com/ or https://www.electro-tech-online.com/.
      Have a great day

      Log in to Reply

Questions related to this article?
👉Ask and discuss on EDAboard.com and Electro-Tech-Online.com forums.



Tell Us What You Think!! Cancel reply

You must be logged in to post a comment.

EE TECH TOOLBOX

“ee
Tech Toolbox: 5G Technology
This Tech Toolbox covers the basics of 5G technology plus a story about how engineers designed and built a prototype DSL router mostly from old cellphone parts. Download this first 5G/wired/wireless communications Tech Toolbox to learn more!

EE Learning Center

EE Learning Center
“engineers
EXPAND YOUR KNOWLEDGE AND STAY CONNECTED
Get the latest info on technologies, tools and strategies for EE professionals.

HAVE A QUESTION?

Have a technical question about an article or other engineering questions? Check out our engineering forums EDABoard.com and Electro-Tech-Online.com where you can get those questions asked and answered by your peers!


RSS EDABOARD.com Discussions

  • Thermal modelling of repetitive power pulse
  • Redundant XORs
  • No Output Voltage from Voltage Doubler Circuit in Ansys Nexxim (Harmonic Balance Simulation)
  • Discrete IrDA receiver circuit
  • ISL8117 buck converter blowing up

RSS Electro-Tech-Online.com Discussions

  • using a RTC in SF basic
  • Can I make two inputs from one??
  • Help with finding unique wire lug(s)
  • Simple LED Analog Clock Idea
  • Kawai KDP 80 Electronic Piano Dead

Featured – LoRa/LoRaWan Series

  • What is the LoRaWAN network and how does it work?
  • Understanding LoRa architecture: nodes, gateways, and servers
  • Revolutionizing RF: LoRa applications and advantages
  • How to build a LoRa gateway using Raspberry Pi
  • How LoRa enables long-range communication
  • How communication works between two LoRa end-node devices

Recent Articles

  • RPi Python Programming 21: The SIM900A AT commands
  • RPi Python Programming 22: Calls & SMS using a SIM900A GSM-GPRS modem
  • RPi Python Programming 23: Interfacing a NEO-6MV2 GPS module with Raspberry Pi
  • RPi Python Programming 24: I2C explained
  • RPi Python Programming 25 – Synchronous serial communication in Raspberry Pi using I2C protocol

EE ENGINEERING TRAINING DAYS

engineering

Submit a Guest Post

submit a guest post
Engineers Garage
  • Analog IC TIps
  • Connector Tips
  • Battery Power Tips
  • DesignFast
  • EDABoard Forums
  • EE World Online
  • Electro-Tech-Online Forums
  • EV Engineering
  • Microcontroller Tips
  • Power Electronic Tips
  • Sensor Tips
  • Test and Measurement Tips
  • 5G Technology World
  • Subscribe to our newsletter
  • About Us
  • Contact Us
  • Advertise

Copyright © 2025 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy

Search Engineers Garage

  • Electronic Projects & Tutorials
    • Electronic Projects
      • Arduino Projects
      • AVR
      • Raspberry pi
      • ESP8266
      • BeagleBone
      • 8051 Microcontroller
      • ARM
      • PIC Microcontroller
      • STM32
    • Tutorials
      • Audio Electronics
      • Battery Management
      • Brainwave
      • Electric Vehicles
      • EMI/EMC/RFI
      • Hardware Filters
      • IoT tutorials
      • Power Tutorials
      • Python
      • Sensors
      • USB
      • VHDL
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Product News
    • Business News
    • Company/Start-up News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • DigiKey Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Learn
    • eBooks/Tech Tips
    • Design Guides
    • Learning Center
    • Tech Toolboxes
    • Webinars & Digital Events
  • Resources
    • Digital Issues
    • EE Training Days
    • LEAP Awards
    • Podcasts
    • Webinars / Digital Events
    • White Papers
    • Engineering Diversity & Inclusion
    • DesignFast
  • Guest Post Guidelines
  • Advertise
  • Subscribe