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

Stm32f103 Pwm(Pulse width modulation) signal generation using internal timers, keil MDK-ARMv6 and Stmcubemx Ide

By EG Projects September 4, 2021

This tutorial is about generating pwm (pulse width modulation) signal with stm32f103 microcontroller using its internal hardware timers. Stm32f103 microcontroller components/peripherals initialization code is generated using stmcubemx ide and code is written and compiled in keil MDK-ARMv6 ide. A simple led is derived on a fixed pwm signal output. Led dims and blinks according to the duty cycle and frequency that a particular pwm pin is outputting. A single Pwm signal is generated/Outputted in the tutorial, but you can generate multiple pwm signals with the same method and settings. 

I am going to output a 1 Hz frequency and 50% duty cycle pwm(pulse width modulation) signal using timer-4 of stm32f103 microcontroller. Timer-4 channel-1 is used to output signal. Channel-1 corresponds to PB6 pin of stm32f103 microcontroller. At PB6 an led is connected on which pwm output can be seen. 1 Hz frequency in time domain is T=1/f > T=1/1 Hz > T=1 s. So 1 Hz frequency translates to 1 s and my duty cycle is 50% so the led at PB6 pin will blink at half a second rate.

How to Generate Desired/Specific PWM Frequency? Formula derivation and Calculations.

Formula for calculating values for pwm generating is not very complex, it only requires in depth knowledge of the stm32f103 clock, timer structure and timer registers. I divided the formula and method in steps and it is very easy know to calculate the values. Below on right side formula for calculating the value’s for generating specific pwm signal is given and on left side the generic terms used in the formula with their definition/meaning are given.

Timer Tick Frequency

Timer tick frequency is the frequency at which the timer is completing its one instruction cycle. 

Counter Frequency

Counter Frequency is the frequency at which we want our timer tick counter to increment.

Stm32f103 Pwm(Pulse Width Modulation) frequency calculation formula

Stm32f103 Pwm(Pulse Width Modulation) frequency calculation formula

Pwm Resolution

Pwm resolution is number of Pwm steps required to generate a required counter frequency. Usually we use an arbitrary number here and then adjust it according to our need. Pwm resolution must be in between 0-65535 since this value is stored in 16-bit register and maximum 16-bit count is 65535. 

Stm32f103 pwm formula for Prescaller value

Stm32f103 pwm formula for Prescaller value
Replacing the Counter Frequency with its formula in the Timer Prescaller Value equations yields the equation given on the left side. Now its easy to determine the prescaller value.
Note: Pwm Resolution and Timer Prescaller are 16-bit registers their value must not exceed 65535.  
Now looking at the final formula we need two things in it. Timer Prescaller Value and Pwm Resolutin(Steps).  You can assign Timer Prescaller Value or Pwm Resolution(Steps) a fixed value and calculate the other perimeter. 

Stmcube-Mx code initializing steps an generating keil MDK-ARM code

I assume that you people are familiar with stmcube mx project creating process and know about the necessary steps. If not then take a simple tutorial 

  • Getting started with stmcube-mx keil MDK-ARM v5​

Stm32f103 timer 4 configuration using input HSI Clock

Stm32f103 timer 4 configuration using input HSI Clock
Stm32f103c8t6 has 4, 16 bit timers, Timer 1,2,3 and 4. We can use all four timers for generating pwm(pulse width modulation) signal. Each timer has fixed pins on which the pwm signal can be output called channels. Timer 1 and 2 are multiplexed with other peripherals to use their pwm function we have to make sure that the timer is not colliding with any other peripheral function.
Timer 3 and 4 are stand alone and they do not collide with any other peripheral function. So its good to use them. I am using Timer 4 in the project/tutorial.
I am using Internal clock source so check this check box in the timer settings. I am using timer 4, channel 1 for pwm output so i selected channel 1. Channel 1 corresponds to PB6 of stm32f103 microcontroller. The settings diagram is given on the right side. 

Stm32f103 timer input clock configuration

Stm32f103 timer input clock configuration
I am using internal HSI(High Speed Internal) oscillator/clock store. After dividing the clock with prescallers at different stages the final clock that is supplied to timer 4 is 0.0625MHz, Translating 0.0625Mhz to hz is 62500Hz. So the final timer input frequency is 62500 Hz.

Calculating Values for 1Hz frequency and 50% duty cycle pwm signal output

In the final timer settings it’s time to input the Counter Period/Pwm Resolution(Steps), Prescaler value and Pulse required. Pulse is the duty cycle required and in our case its 50%. Lets Solve the upper formula according to the pwm calculation formula’s given above.
Given values:
Timer Input Clock                                              =                        0.0625Mhz or 62500Hz
Required Frequency                                          =                        1 Hz   (In Time Domain 1 second)
Counter Period/Pwm Resolution(Steps)            =                         3906 (I picked a random value)

Counter Frequency= frequency required * Counter Period
Counter Frequency= 1Hz * 3906 =3906 Hz

Timer Prescaller value=( Timer Input Clock/Counter Frequency)-1
Timer Prescaller value=(62500 Hz / 3906 Hz) – 1 = 15

Now we have both the value’s Timer Presaller value, Counter Period/Pwm Resolution(Steps) and also they are in 16-bit range. Now input the values in the timer 4 configuration. I selected the counter to be in UP mode(Count for 0 to 3906). Pulse is the duty cycle in our case its 50% so 3906*50% > 3906*0.5 = 1953.

Stm32f103 timer 4 pwm settings

Stm32f103 timer 4 pwm settings
Next advance tutorial on Variable Pwm(Pulse Width Modulation) Output with Stm32f103 microcontroller using stm32cubemx and keil uvision ide. Click the below button to move to tutorial.

Stm32f103 Variable Pwm output using internal timers

Download the Project code. Folder contains the stmcubemx project file and keil Mdk-Arm v5 project files. Please give us your feed back on the project.

 

Watch the Project Video Here…

                                                                             Files/Code Folder 

You may also like:


  • What drone parts you need to build a quadcopter?

  • What is FreeRTOS?

  • What are the different types of integrated circuits?

  • What is an SoC?

  • What is a low power design?

  • What is the 1-Wire protocol?

Filed Under: Electronic Projects, STM32

 

Next Article

← Previous Article
Next Article →

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

  • Resistor Selection for Amplifier Layout
  • Permittivity and Permealibility in CST
  • Testing 5kW Grid Tied inverter over 200-253VAC
  • Single ended measuring ports and balanced antenna
  • Thermal modelling of repetitive power pulse

RSS Electro-Tech-Online.com Discussions

  • Can I make two inputs from one??
  • Beats Solo 4
  • Behlke swich
  • Is AI making embedded software developers more productive?
  • Simple LED Analog Clock Idea

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