Close Menu
  • Articles
    • Learn Electronics
    • Product Review
    • Tech Articles
  • Electronics Circuits
    • 555 Timer Projects
    • Op-Amp Circuits
    • Power Electronics
  • Microcontrollers
    • Arduino Projects
    • STM32 Projects
    • AMB82-Mini IoT AI Camera
    • BLE Projects
  • IoT Projects
    • ESP8266 Projects
    • ESP32 Projects
    • ESP32 MicroPython
    • ESP32-CAM Projects
    • LoRa/LoRaWAN Projects
  • Raspberry Pi
    • Raspberry Pi Projects
    • Raspberry Pi Pico Projects
    • Raspberry Pi Pico W Projects
  • Electronics Calculator
Facebook X (Twitter) Instagram
  • About Us
  • Disclaimer
  • Privacy Policy
  • Contact Us
  • Advertise With Us
Facebook X (Twitter) Instagram Pinterest YouTube LinkedIn
How To Electronics
  • Articles
    • Learn Electronics
    • Product Review
    • Tech Articles
  • Electronics Circuits
    • 555 Timer Projects
    • Op-Amp Circuits
    • Power Electronics
  • Microcontrollers
    • Arduino Projects
    • STM32 Projects
    • AMB82-Mini IoT AI Camera
    • BLE Projects
  • IoT Projects
    • ESP8266 Projects
    • ESP32 Projects
    • ESP32 MicroPython
    • ESP32-CAM Projects
    • LoRa/LoRaWAN Projects
  • Raspberry Pi
    • Raspberry Pi Projects
    • Raspberry Pi Pico Projects
    • Raspberry Pi Pico W Projects
  • Electronics Calculator
How To Electronics
Home » STM32 PWM (Pulse Width Modulation) Tutorial with Servo Motor
STM32 Projects

STM32 PWM (Pulse Width Modulation) Tutorial with Servo Motor

Mamtaz AlamBy Mamtaz AlamUpdated:September 16, 20224 Mins Read
Share Facebook Twitter LinkedIn Telegram Reddit WhatsApp
STM32 PWM
Share
Facebook Twitter LinkedIn Pinterest Email Reddit Telegram WhatsApp

This post is all about the detailed tutorial of using PWM (Pulse Width Modulation) in the STM32 (STM32F103C) microcontroller.


Overview

Pulse-width modulation (PWM) is used for controlling the amplitude of digital signals in order to control devices and applications requiring power or electricity. This is an advanced tutorial on PWM generation. We will learn how to generate a variable PWM signal with STM32 Series (STM32F103C) microcontroller. Variable PWM signal is used for controlling the speed of DC motors/Fans. It is also used in AC Light Dimmer. PWM solar chargers also work on variable PWM signal. Servo Motors angle and direction is also controlled with the PWM signal.

In this tutorial, we will learn about PWM (Pulse Width Modulation) in STM32 using Servo Motor. We will also learn how to interface Servo with the STM32F103C8 board. A potentiometer is also interfaced to vary the position of the servo motor’s shaft, and an LCD to display the angle value.



Check this post related to Servo Motor Control: How to Control Multiple Servo Motors with Arduino


Components Required

Here is a list of components needed to learn about PWM signal generation in STM32. All the components can be purchased easily from Amazon.

S.N.Components NameQuantityPurchase Links
1STM32F103C Board1Amazon | AliExpress
216x2 LCD Display1Amazon | AliExpress
3Servo Motor SG901Amazon | AliExpress
4Potentiometer 10K1Amazon | AliExpress
5Potentiometer 100K1Amazon | AliExpress
6Connecting Wires1Amazon | AliExpress
7Breadboard1Amazon | AliExpress


What is PWM (Pulse with Modulation)?

Pulse-width modulation (PWM) is a modulation process or technique used in most communication systems for encoding the amplitude of a signal right into a pulse width or duration of another signal, usually a carrier signal, for transmission. Although PWM is also used in communications, its main purpose is actually to control the power that is supplied to various types of electrical devices, most especially to inertial loads such as AC/DC motors.

Duty Cycle of the PWM

When the signal is high, we call this “on time”. To describe the amount of “on time”, we use the concept of the duty cycle. Duty cycle is measured in percentage. The percentage duty cycle specifically describes the percentage of time a digital signal is on over an interval or period of time. This period is the inverse of the frequency of the waveform.



If a digital signal spends half of the time on and the other half off, we would say the digital signal has a duty cycle of 50% and resembles an ideal square wave. If the percentage is higher than 50%, the digital signal spends more time in the high state than the low state and vice versa if the duty cycle is less than 50%. See the figure below for full understanding:

PWM Example STM32


PWM in STM32

STM32F103C has 15 PWM pins and 10 ADC pins. It has 7 timers and each PWM output is provided by a channel connected to 4 timers. It has 16-bit PWM resolution, i.e 2^16. Because of this its counters and variables can be as large as 65535. As it has 72MHz clock rate, its PWM output can have a maximum period of about one millisecond.
1. The value of 65535 gives Full Servo Rotation (100% Duty Cycle)
2. The value of 32767 gives Half Rotation like 90 degrees (50% Duty Cycle)
3. The value of 13107 gives 20% Rotation (20% Duty Cycle)

Pulse-Width-Modulation-Waveform

So in this PWM tutorial, we will use 100K potentiometer and STM32 to vary the rotational angle of SG90 Servo Motor with the PWM technique. A 16×2 LCD is used to display a rotational angle in degrees.


Circuit Diagram & Connection

In STM32F103C, there are 10 ADC pins (PA0-PB1), and we can use only one pin (PA5) for analogread() for setting the shaft position of the motor by the potentiometer. Also among 15 PWM pins of STM32 (PA0, PA1, PA2, PA3, PA6, PA7, PA8, PA9, PA10, PB0, PB1, PB6, PB7, PB8, PB9), one pin will be used for providing pulses to the Servo motor’s PWM pin PA0.

A 16×2 LCD is interfaced to STM32 with following pin connections rs = PB11, en = PB10, d4 = PA4, d5 = PA3, d6 = PA2, d7 = PA1. It has 10K potentiometer attached to it to adjust the contrast.

STM32 PWM Example




Source Code/Program

Here is a code for controlling the Servo Motor with PWM Technique using STM32 Microcontroller.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include<Servo.h>                                                          //including servo library
#include<LiquidCrystal.h>                                               //including LCD display library
const int rs = PB11, en = PB10, d4 = PA4, d5 = PA3, d6 = PA2, d7 = PA1;   //declaring pin names and pin numbers of lcd
LiquidCrystal lcd(rs,en,d4,d5,d6,d7);                               //setting lcd and its paramaters
 
int servoPin = PA0;                                //declare and initialize pin for servo output PWM
int potPin = PA5;                                     //potentiometer ADC input
 
Servo servo;                                            // creating variable servo with datatype Servo
 
void setup()                            
 
{
  lcd.begin(16,2);                                                        
  lcd.setCursor(2,0);                                                        
  lcd.print("PWM Tutorial");                                              
  lcd.setCursor(3,1);                                                      
  lcd.print("Using Servo");                                          
  delay(3000);                                                            
  lcd.clear();                                                              
  servo.attach(servoPin);   //it connects pin PA0 with motor as control feedback by providing pulses
  
}
 
void loop()
{
lcd.clear();                                                                 //clears lcd
int angle;                                                                   //declare varible angle as int
int reading;                                                                 //declare varible reading as int
reading = analogRead(potPin);                                                //read analog value from pin PA3
angle = (reading/24);                                                        //it divides ADC the value according to max angle 170 deg
servo.write(angle);                                                          //it puts angle value at servo
lcd.setCursor(0,0);                                                          //setting cursor at first row and first column
lcd.print("Angle:");                                                         //puts Angle in LCD
lcd.print(angle);                                                            //puts value at angle
delay(100);                                                                  //delay in time
}  

Share. Facebook Twitter Pinterest LinkedIn Tumblr Email Reddit Telegram WhatsApp
Previous ArticleArduino RADAR Model using Ultrasonic Sensor for Detection & Ranging
Next Article IoT Based ECG Monitoring with AD8232 ECG Sensor & ESP32

Related Posts

PIR Motion Detection using Bluetooth & STM32 Board

PIR Motion Detection using Bluetooth & STM32 Board

Updated:December 23, 20234K
STM32 SHT85 Humidity Temperature Sensor

Collecting SHT85 Sensor Data using STM32 & Bluetooth Low Energy

Updated:August 20, 20222K
SX1276 STM32

Interfacing LoRa SX1276 with STM32 Microcontroller | LR1276-915MHz

Updated:May 29, 2023112K
BLE enabled Smart Bulb with STM32 & Javascript

BLE enabled Smart Bulb with STM32 & Javascript

Updated:August 20, 20222K
Create BLE Project using STM32 Microcontroller & BlueIO

Create BLE Project using STM32 Microcontroller & BlueIO

Updated:August 20, 202212K
DS18B20 STM32

Interfacing DS18B20 Temperature Sensor with STM32

Updated:August 21, 2022110K
Add A Comment

CommentsCancel reply

Latest Posts
ESP32 Fingerprint Attendance System with Live Web Dashboard

ESP32 Fingerprint Attendance System with Live Web Dashboard

June 21, 2026
IoT Based PM & Air Quality Monitoring System using ESP32

IoT Based PM & Air Quality Monitoring System using ESP32

June 14, 2026
DIY ESP32 MLX90640 IR Thermal Camera with Live Web Display

DIY ESP32 MLX90640 IR Thermal Camera with Live Web Display

May 10, 2026
IoT Activity Tracker with ESP32 & Accelerometer Gyroscope

IoT Activity Tracker with ESP32 & Accelerometer/Gyroscope

May 2, 2026
A Guide to Sourcing Obsolete ICs for Vintage Projects

Beyond AliExpress: A Guide to Sourcing Obsolete ICs for Vintage Projects

April 21, 2026

ESP32 IoT Vehicle Motion Analyzer with MPU6050 & LIS3MDL

April 27, 2026
Building a Smart Sensor Node with a BLE Microcontroller

Building a Smart Sensor Node with a BLE Microcontroller

February 26, 2026
High-Accuracy Pitch, Roll, Yaw with ESP32 & BNO08x IMU

High-Accuracy Pitch, Roll, Yaw with ESP32 & BNO08x IMU

April 27, 2026
Top Posts & Pages
  • ESP32 Fingerprint Attendance System with Live Web Dashboard
    ESP32 Fingerprint Attendance System with Live Web Dashboard
  • ESP32 CAN Bus Tutorial | Interfacing MCP2515 CAN Module with ESP32
    ESP32 CAN Bus Tutorial | Interfacing MCP2515 CAN Module with ESP32
  • IoT AC Energy Meter with PZEM-004T & ESP32 WebServer
    IoT AC Energy Meter with PZEM-004T & ESP32 WebServer
  • 12V DC to 220V AC Inverter Circuit & PCB
    12V DC to 220V AC Inverter Circuit & PCB
  • LD2410 Sensor with ESP32 - Human Presence Detection
    LD2410 Sensor with ESP32 - Human Presence Detection
  • IoT Based Patient Health Monitoring on ESP32 Web Server
    IoT Based Patient Health Monitoring on ESP32 Web Server
  • Silicon Controlled Rectifier (SCR): Construction, Working & Applications
    Silicon Controlled Rectifier (SCR): Construction, Working & Applications
  • Buck Converter: Basics, Working, Design & Application
    Buck Converter: Basics, Working, Design & Application
Categories
  • Arduino Projects (197)
  • Articles (60)
    • Learn Electronics (19)
    • Product Review (15)
    • Tech Articles (28)
  • Electronics Circuits (46)
    • 555 Timer Projects (21)
    • Op-Amp Circuits (7)
    • Power Electronics (13)
  • IoT Projects (205)
    • ESP32 MicroPython (7)
    • ESP32 Projects (82)
    • ESP32-CAM Projects (15)
    • ESP8266 Projects (76)
    • LoRa/LoRaWAN Projects (22)
  • Microcontrollers (38)
    • AMB82-Mini IoT AI Camera (4)
    • BLE Projects (18)
    • STM32 Projects (19)
  • Raspberry Pi (93)
    • Raspberry Pi Pico Projects (57)
    • Raspberry Pi Pico W Projects (12)
    • Raspberry Pi Projects (24)
Follow Us
  • Facebook
  • Twitter
  • Pinterest
  • Instagram
  • YouTube
About Us

“‘How to Electronics’ is a vibrant community for electronics enthusiasts and professionals. We deliver latest insights in areas such as Embedded Systems, Power Electronics, AI, IoT, and Robotics. Our goal is to stimulate innovation and provide practical solutions for students, organizations, and industries. Join us to transform learning into a joyful journey of discovery and innovation.

Copyright © How To Electronics. All rights reserved.
  • About Us
  • Disclaimer
  • Privacy Policy
  • Contact Us
  • Advertise With Us

Type above and press Enter to search. Press Esc to cancel.

Ad Blocker Enabled!
Ad Blocker Enabled!
Looks like you're using an ad blocker. Please allow ads on our site. We rely on advertising to help fund our site.