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

Controlling Servo Motor with Stm32f103 microcontroller using stm32cubemx code configurator by STMicroelectronics and keil uvision 5 ide for cortex m1 series microcontrollers

By EG Projects September 4, 2021

This tutorial is a part of series of tutorials on pwm(pulse width modulation) signal generation with stm32f103 microcontroller. Previously we looked upon how to generate pwm signal with stm32 microcontroller using keil uvision 5 ide and stm32cubemx code configurator. We moved forward on generating variable pwm signal using internal timers of stm32f103 microcontroller. We studied about generating pwm, now its time to put it in to practical and control a peripheral with the pwm signal. I decided to control a simple toy servo motor with stm32f103 microcontroller. I used two servo motors in the project for testing the code. First i used tower pro SG-90 servo motor. This motor does well but it did not move its arm at right position there is always some degrees of tolerance in actual results. You can buy one at fairly $3 price. Another motor which i just tested is HS-785HB it properly turns but requires more power(current). For this tutorial i recomend to use tower pro sg90 servo motor due to lower power consumption and simplicity.

Servo motor working principle

Servo motors are small motors unlike size they possess greater torque and can move heavy loads. Their small size possessing high torque made them popular among toy makers. Many toys that are around us contains servo motors in them along with dc motors. Normally servo motors in ideal state consumes little power but during load moving power shoots off and servos start consuming greater amount of current.
Servo motors works on pwm(pulse width modulated) signals. They have an arm/armature which rotates when sufficient voltage, current and pwm signal is applied to motor. When arm rotates it moves every thing that comes in its ways.      

Different types of servo motors

Different types of servo motors
Not every servo motor can move heavy loads. It depends on their specifications and details. Normally toy servo motors can move loads from 1 kg up to 12 kg. Their are two types of servo motors DC and AC. Ac servo motors can move even mush heavier loads they are used in industrial applications. DC servo motors are best for small projects. In this project i am also using a DC servo motor with stm32 microcontroller.
As you learned servo motors work on pwm signal. Most of the dc servo motors require 50 Hz frequency for operation with variable duty cycle. Below is the standard requirement wave forms. Our motor also require the same pattern.   

Servo motor pwm duty cycle and frequency requirement

Servo motor pwm duty cycle and frequency requirement
At period 20 milli seconds and duty cycle 2 milli seconds servo motor arm moves to 180 degree. At duty cycle 1.5 milli seconds arm moves to 90 degree and at duty cycle 1 milli seconds arm rotates to 0 degree.

Servo with stm32 microcontroller

I am going to interface servo with stm32f103c8t6 microcontroller. I purchased a pre assembled and cheap board which mounts the microcontroller on it. For rotating the arm of servo motor three pins of microcontroller are used as input. For outputting pwm signal one pin is used. Port-A pins 0,1 and 2 are used as inputs and Port-B pin#6 is used to output the pwm signal.
Stm32f103 microcontroller works on 3.3 volts where as servo motor tower pro sg90 works on 5 volts. So both modules motor and microcontroller must be powered with different power sources. We can not drive servo directly with stm32 output pwm signal because its in 3.3v wave form and motor requires 5v. I inserted a circuit in between the two modules to converted 3.3v to 5v. First transistor is converting the input signal to output 5v but the signal is inverted. Another transistor inverts the inverted signal and brings it back to original logic. So now 3.3v at input cross-ponds to 5v at output to servo motor.

Servo motor controlled with Stm32f103 microcontroller

Servo motor controlled with Stm32f103 microcontroller
In the above circuit make sure to common the grounds of both motor and microcontroller power supply. You can also use ULN2003 IC here instead of the two transistors. ULN2003 contains the same circuit in it with fly wheeling diodes.  
In stm32cubemx initialize 3 pins Port-A pin 1,2 and 3 as input. If you do not know about how to do this then i have a simple tutorial for you on it.  

Getting started with Stm32f103 microcontroller

Picture

Stm32f103 pwm pin settings
I am going to output pwm signal on PB6. For this you have to do some settings in stm32cubemx ide. Like selecting the channel and configuring some other things. Before moving any forward i would like you to take a simple tutorial on pwm pin selection and duty cycle calculation formula. 

Pwm Settings and formula driving

The above tutorial is very important to understand the code flow and settings below. Internal stm32 microcontroller oscillator is used in the project. Though the board has an external 8 MHz oscillator but i preferred to use internal. Final clock to timer 4 is 1 MHz.  

Stm32 clcok configuration for timer 4 channel 1 in stm32cubemx

Stm32 clcok configuration for timer 4 channel 1 in stm32cubemx
Then i calculated the values for counter register and other values required to input in the timer-4 configuration. You can see the formula and other values calculations in the above tutorial.

Stm32 setting pwm counter period and duty cycle in stm32cubemx

Stm32 setting pwm counter period and duty cycle in stm32cubemx
My counter period is 1000. It means at 1000 the pwm duty cycle will be 100% with period 20 milli seconds or 50 Hz frequency. At 500 pwm duty cycle will be 50% which translates to 10 milli seconds. At 5% it will be 1 milli second and at 10% it will be 2 milli seconds and at 7.5% it will be 1.5 milli seconds.

Picture

This counter value is used in code to move the servo motor arm. Below is the code. When button-3 is pressed motor moves to 180 degrees. When button-2 is pressed button rotates to 90 degree and when button-1 is pressed it comes back to 0 degrees. 
Internal pull up resistors of pins Port-A Pin# 1,2 and 3 are activated. Hence only buttons are connected to ground directly. 
Download the project code. Code is written in keil uvision 5 ide. Stm32cubemx is used for microcontroller configuration. Code folder contains all keil and stm32cubemx files. Code is open source you can modify and use it according to your needs. Please provide us your feed back on the project.
Code/Files

You may also like:


  • What are top applications of CAN protocol?

  • What are different types of industrial robots?

  • What types of motors are used in electric vehicles?

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

  • What drone parts you need to build a quadcopter?

  • What is FreeRTOS?

Filed Under: Electronic Projects, Featured Contributions, STM32

 

Next Article

← Previous Article
Next Article →

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



Tell Us What You Think!! Cancel reply

You must be logged in to post a comment.

EE TECH TOOLBOX

“ee
Tech Toolbox: Internet of Things
Explore practical strategies for minimizing attack surfaces, managing memory efficiently, and securing firmware. Download now to ensure your IoT implementations remain secure, efficient, and future-ready.

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

  • Multiple DC/DC converters and a single input source
  • Xiaomi Mijia 1C Robot problem of going backwards while working
  • Will this TL084C based current clamp circuit work?
  • ADS optimization cockpit window does not open
  • Voltage mode pushpull is a nonsense SMPS?

RSS Electro-Tech-Online.com Discussions

  • Parts required for a personal project
  • Curved lines in PCB design
  • Wideband matching an electrically short bowtie antenna; 50 ohm, 434 MHz
  • using a RTC in SF basic
  • PIC KIT 3 not able to program dsPIC

Featured – RPi Python Programming (27 Part)

  • 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
  • RPi Python Programming 26 – Interfacing ADXL345 accelerometer sensor with Raspberry Pi

Recent Articles

  • AC-DC power supply extends voltage range to 800 V DC
  • Infineon’s inductive sensor integrates coil system driver, signal conditioning circuits and DSP
  • Arm Cortex-M23 MCU delivers 87.5 µA/MHz active mode
  • STMicroelectronics releases automotive amplifiers with in-play open-load detection
  • Convection-cooled power controller integrates EtherCat connectivity

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