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 Variable Pwm(Pulse Width Modulation) Signal Output with timers, Keil Arm-mdk and Stm32cubemx ide

By EG Projects April 14, 2019

This is an advance tutorial on pwm generation. We are going to output/generate a variable pwm signal with stm32f103 microcontroller. Variable pwm signal is used for controlling the speed of Dc motors and fans. It is also used in dimmers to dim and bright the light bulbs, led arrays etc. Pwm solar chargers also work on variable pwm signal. Servo motors angle and direction is also controlled with pwm signal. In the previous tutorial we learned how to generate pwm(pulse width modulated) signal using internal timers of stm32f103 microcontroller. We also derived the formula for calculating different values required to generate a particular/specific frequency pwm signal. Pwm configuration required to be done in stm32cubemx code generator is also step by step done and discussed in the tutorial. This tutorial is based on the code and configuration done in the previous one. So i recommend you to first see the previous tutorial than start with it. You can easily understand the code below once you take the previous tutorial. Visit the link below.

  • Stm32f103 Pwm generation using internal timer registers, keil and stmcubemx steps 

Now you have taken the above tutorial lets start this tutorial. I am going to generate variable pwm signal on 1 Hz frequency that is generated in the previous tutorial. 1 Hz in time domain is equal to 1 second. I am going to output this variable signal on a led which is connected to output of stm32f103. Timer-4 channel-1 of stm32f103 is used to output variable pwm signal. Channel-1 corresponds to Prt-B Pin number 6 – PB-6 pin of stm32f103 microcontroller. 

Stm32f103 variable pwm output

Stm32f103 variable pwm output

I am using this cheap $1.5 stm32f103c8t6 development board purchased from aliexpress. It does not have a programmer built on it. I am using an external stlink v2 programmer to program the module.
​
Circuit Diagram
Circuit of the project is simple connect the anode(positive led) of led to pin PB-6 and ground the cathode(negative leg). No resistor is required since the stm32 works on 3.3 v and its gpio’s also source max 3.3 volt which is near the voltage required by a red color led.
I am going to increment the duty cycle of output signal from 0 to 100% and then back from 100% to 0. You can see the effect on the led in the video at the bottom of the post
The code of the project is simple i just added the functionality of duty cycle increment and decreas. For 1 Hz frequency we calculated the counter value which is 3906(Previous tutorial). 3906 is the max 100% duty cycle. I am going to increment duty cycle from 0 to 3906(0-100%). After increment i will decrease it in descending order from 3906 to 0(100%-0). This function is achieved by placing the duty cycle in for loop. For increment phase for loop runs from 0 to 3906 and for decreasing it rolls back from 3906 to 0. The important statement in the whole code is.

__HAL_TIM_SetCompare(&htim4, TIM_CHANNEL_1, pwm);

This macro/statement is setting/updating the pwm period. It updates the capture compare register which is continuously capturing and comparing the timer count value with our pwm period value.  
In the video the led blinks its because i am working on high frequency 1 Hz. Which in time domain is equal to 1 s. Now varying the duty cycle, if increasing the duty cycle the led blinks at higher rate, if decreasing the duty cycle the led blinks at slower rate. In many examples about the variable pwm it is shown that the led fads in and out upon variation in pwm duty cycle. In those examples the frequency of the pin is 10 ms to 100 ms and at those frequencies their is no blink possible. If you want the led to fan in and out just change the channel frequency.
Download the project code/files and please give us your feed back on the project. In case if you have any queries please write them below in the comments section.

Watch the Project Video Here…

Project Files


Filed Under: Microcontroller 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

  • How best to synchronise the UCC38C45?
  • floating node warning in LTSpice
  • Collector Current Low side Has a large drop respect High Side during Miller during Double Pulse Test
  • Diode recovery test Irrm timing.
  • The Analog Gods Hate Me

RSS Electro-Tech-Online.com Discussions

  • Raise your hand if your car had one of these:
  • The Analog Gods Hate Me
  • Display TFT ST7789 (OshonSoft Basic).
  • Fixing board, Easy question HEX SCHMITT
  • Can I make two inputs from one??

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

  • How to monitor temperature and humidity on a TFT display with graphics
  • Tria modules integrate edge AI processing with multi-core processors
  • pSemi introduces RF switch with 52 dBm PMAX,PEAK and 90-dBm IIP3 linearity
  • XP Power launches 1.3 kW power supply with 58.9 W/cmÂł density
  • How to enable Wi-Fi provisioning in ESP32-based IoT products

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