0% found this document useful (0 votes)
13 views30 pages

STM32 Microcontroller Activities Guide

The document provides an introduction to STM32 microcontrollers, emphasizing their importance in engineering through activities involving digital inputs, outputs, analog signals, and PWM. It includes detailed tutorials on setting up hardware, configuring pins, and writing code to control components like LEDs and buttons. The activities aim to enhance understanding of microcontroller operations and their applications in real-world systems.

Uploaded by

jounchoo
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views30 pages

STM32 Microcontroller Activities Guide

The document provides an introduction to STM32 microcontrollers, emphasizing their importance in engineering through activities involving digital inputs, outputs, analog signals, and PWM. It includes detailed tutorials on setting up hardware, configuring pins, and writing code to control components like LEDs and buttons. The activities aim to enhance understanding of microcontroller operations and their applications in real-world systems.

Uploaded by

jounchoo
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

STM32 Introduction Activity

Authors: Benjamin Zaionz, Avi Bhadore, Michelle Chen, Silas Ifeanyi,


Michael McPhee, Nancy Nelson, Chris Rennick
Adapted for ME 262 by Allyson Giannikouris

© 2025 Pearl Sullivan Engineering IDEAs Clinic 1


Contents
Required Materials .......................................................................................................................4
The Value of Microcontroller Concepts in Engineering ...................................................................4
Digital Inputs and Outputs: .......................................................................................................4
Analog Inputs and ADCs: ..........................................................................................................4
UART and Serial Communication: .............................................................................................4
PWM and Oscilloscopes: ..........................................................................................................4
1 Activity – Digital Inputs and Outputs.......................................................................................6
1.1 Background ..................................................................................................................6
1.1.1 Breadboard ..............................................................................................................6
1.1.2 Digital Signals ...........................................................................................................6
1.1.3 STM Board ................................................................................................................7
1.1.4 Pinout Map ...............................................................................................................7
1.1.5 LED Polarity ..............................................................................................................8
1.1.6 Pull-up and Pull-down Resistors ................................................................................9
1.2 Tutorial ............................................................................ Error! Bookmark not defined.
1.2.1 Hardware ............................................................................................................... 10
1.2.2 Configuration.......................................................................................................... 11
1.2.3 Code ...................................................................................................................... 15
1.2.4 Results ................................................................................................................... 16
1.3 Challenge ................................................................................................................... 16
2 Activity – Analog Inputs and UART ........................................................................................ 17
2.1 Background ................................................................................................................ 17
2.1.1 Analog Signals ........................................................................................................ 17
2.1.2 Measuring Analog Signals using Microcontrollers - ADCs.......................................... 17
2.1.3 Potentiometers ....................................................................................................... 17
2.2 Tutorial ....................................................................................................................... 17
2.2.1 Hardware ............................................................................................................... 17
2.2.2 Configuration.......................................................................................................... 18
2.2.3 Code ...................................................................................................................... 20
2.2.4 UART Console......................................................................................................... 21
2.3 Challenge ................................................................................................................... 24
3 Activity 3 – PWM and Oscilloscope ...................................................................................... 25

© 2025 Pearl Sullivan Engineering IDEAs Clinic 2


3.1 Background ................................................................................................................ 25
3.1.1 PWM ...................................................................................................................... 25
3.1.2 Oscilloscopes ........................................................................................................ 26
3.2 Tutorial ....................................................................................................................... 26
3.2.1 Hardware ............................................................................................................... 26
3.2.2 Configuration.......................................................................................................... 26
3.2.3 Code ...................................................................................................................... 29
3.2.4 Results ................................................................................................................... 29
3.3 Challenge 3 ................................................................................................................ 29
Related Resources ..................................................................................................................... 30

© 2025 Pearl Sullivan Engineering IDEAs Clinic 3


Required Materials
• 2 Switches (Push Button)
• 1 LED
• 1 Breadboard
• 1 Resistor (100 Ω)
• 1 Potentiometer
• 1 Oscilloscope
• 1 STM32 U083RC
• Assorted Wires

The Value of Microcontroller Concepts in Engineering


Incorporating activities related to digital inputs, outputs, analog signals, and PWM using STM32
introduces key foundational concepts essential for any modern engineering program. These
concepts are vital in understanding how microcontrollers operate and how they are used to interact
with real-world systems.

Digital Inputs and Outputs:


The ability to read and send digital signals is fundamental in nearly all electronic devices.
Controlling an LED with a pushbutton might seem simple, but this exercise helps you grasp the
principles of digital communication between a microcontroller and external components. In real-
world applications, digital inputs and outputs are used in everyday systems, from simple
appliances to complex control systems in cars or industrial machines.

Analog Inputs and ADCs:


Analog signals are everywhere in the real world, like temperature, light, or sound. Microcontrollers
cannot process these signals directly without an Analog-to-Digital Converter (ADC). Learning to
work with ADCs is crucial because sensors that detect analog signals are key in robotics,
automation, and even biomedical applications. Understanding how to read analog signals and
convert them into data that a microcontroller can use opens possibilities for building complex,
responsive systems.

UART and Serial Communication:


UART is an important protocol for data exchange between devices. Many embedded systems
require communication between the microcontroller and a computer or another microcontroller.
Mastering UART is necessary for creating systems that can share data, like sensors sending
information to a central processor or logging data to a computer.

PWM and Oscilloscopes:


PWM (Pulse Width Modulation) is a versatile technique used for controlling devices like motors and
LEDs with variable brightness. It’s widely used in power control applications, signal modulation,

© 2025 Pearl Sullivan Engineering IDEAs Clinic 4


and communication systems. Learning how to generate PWM signals and analyze them with an
oscilloscope will help you develop a clear understanding of how to create efficient control systems
in real-world applications. These skills apply directly to automotive systems, robotics, and any
system that requires precision control.

In your program, these fundamental skills will enable you to work confidently with hardware,
understand embedded systems, and create projects that blend software and hardware seamlessly.

© 2025 Pearl Sullivan Engineering IDEAs Clinic 5


1 Activity – Digital Inputs and Outputs
1.1 Background
1.1.1 Breadboard
A breadboard is a prototyping tool used for building and testing circuits without the need for
soldering. It consists of rows and columns of conductive strips underneath a plastic surface with
holes, allowing you to easily insert components and wires to form electrical connections. A typical
breadboard is divided into two main areas: the power rails and the terminal strips.

The power rails, running along the sides, are used for connecting power (e.g., 3.3V or GND)
throughout your circuit. The terminal strips in the center are arranged in rows and connect vertically
underneath the board, making it possible to link components without direct wiring. Figure 1 shows
a how the connections are made internally on a typical breadboard.

Figure 1: Breadboard Connections

1.1.2 Digital Signals


With regards to voltage, signals can be broken down into 2 categories, digital and analog. A digital
signal can be in 1 of 2 states. Typically, these states are thought of as “High” and “Low”. In most
electronics, “High” voltage is 3.3V, and “Low” voltage is 0V, or ground (GND). Analog signals will be
covered in the next section.

© 2025 Pearl Sullivan Engineering IDEAs Clinic 6


1.1.3 STM Board
The STM32 U083RC has dedicated pins that can be configured as either digital inputs or outputs.
Digital outputs supply a digital signal (either 0, or 3.3V) and can be used for various digital
applications. One such application are LEDs, which are the subject of this activity. Digital inputs
“sense” the state of a digital signal. One such application is detecting the state of a switch, which
will be used to control the LEDs in the activity mentioned above.

1.1.4 Pinout Map


A pinout map shows the available pins on your STM32 microcontroller and what functions those
pins can be used for. Every pin can have multiple uses, like:

• Digital I/O (Input/Output): Turn a pin on or off (used for LEDs, buttons, etc.).
• Analog Inputs: Read continuous voltage values from sensors or potentiometers.
• Communication Protocols: SPI, I2C, UART, and others for communication with external
devices.

Figure 2 and Figure 3 show the pinout map for the STM32 F401RE board. While we are using a
different board, the pin assignments shown are the same for the board we are using. You will likely
need to refer to this figure throughout the activity, or use the schematics to find the same
information. The digital I/O pins are labelled D0 – D15, and the analog inputs are labelled A0 – A5.
These names and numbers are consistent with the Arduino standard for shields.

The pins are also labelled with letters and numbers, like PA0, PB9, PC12, etc. The "P" stands for
"port," and each letter represents a port of pins (A, B, C, etc.), while the number represents the
specific pin in that port.

© 2025 Pearl Sullivan Engineering IDEAs Clinic 7


Figure 2: Left Side Pinout Map1

Figure 3: Right Side Pinout Map1

1.1.5 LED Polarity


Many electronic components are non-polar, which means that the orientation of the component
relative to the flow of electricity does not matter. One such components are resistors. Other
components are polar, which means that the orientation of the component relative to the flow of
electricity does matter. One such components are diodes. Diodes are designed specifically to allow
electricity to flow in a certain direction and not in the other. LEDs (light emitting diodes) are diodes
that also emit light. Figure 4 shows an LED.

1
Figure 2 and 3 from: [Link]
applications-programming-peripherals/

© 2025 Pearl Sullivan Engineering IDEAs Clinic 8


Figure 4: LED Example

Notice that one leg is longer than the other. The longer leg is the anode, which means it is
connected to a positive voltage. The shorter leg is the cathode, which means it is connected to a
negative voltage (GND). If the LED is connected in the wrong orientation, then no electricity will
flow, and the LED will not light up.

1.1.6 Pull-up and Pull-down Resistors


If a line in a circuit isn’t explicitly set to a voltage it is considered “floating”. In electronics, it is
undesirable for any lines to be floating, so we use pull-up and pull-down resistors to passively set
the voltage of lines that are not being set otherwise. Figure 5 shows the wiring diagram for pull-up
and pull-down resistors. Please note that MCU stands for microcontroller unit, which in our case is
the STM32 U083RC.

© 2025 Pearl Sullivan Engineering IDEAs Clinic 9


Figure 5: Pull-up and Pull-down Resistor Diagrams

Consider the pull-down resistor diagram. When the switch is open, the voltage on the MCU pin is
very close to ground (if the pull-down resistor has a low resistance, which they typically do). When
the switch is closed, the voltage on the MCU pin is 5V, and the resistor measures 5V across. This
behaviour is mirrored for the pull-up resistor.

1.2 Activity
For the first activity, you will control 1 LED with a button. When the button is pressed, the LED will
turn on, and when the button is released, the LED will turn off.

1.2.1 Hardware
Connect the LED, pushbutton, resistor, and STM32 board according to Figure 6 and Figure 7 below.
Note that the bent leg of the LED is the longer-legged anode. Ignore the blue wires in Figure 7, they
are for a different microcontroller.

D3

D4

Figure 6: Circuit Diagram

© 2025 Pearl Sullivan Engineering IDEAs Clinic 10


Figure 7: Circuit Drawing – Ignore the blue wires, they are for an old board. The green wires are correct for the U083RC.

The power rails (on the left side of the board, which are connected by the black and red wire)
provide 3.3V and GND to the circuit.

The button is connected to the 3.3V rail, and a digital input pin. When the button is pressed the
digital input pin will be set to “High”. When the button is not pressed, the voltage is unknown, so we
configure the pin as pull-down so that it will passively set itself in the “Low” state.

The LED is connected in series with a resistor to prevent the LED from burning out. The resistor is
connected to a digital output, which when toggled on can turn the LED on. Notice that the LED is
oriented with the longer leg connected to the resistor (which is connected to the digital output), and
the shorter leg is connected to GND. If the orientation of the LED is flipped, it will not turn on.

1.2.2 Configuration
Start STM32CubeIDE, and log into myST. Select myST and follow the log in process. If you do not
see the myST icon in the menu, go to Help -> STM32CubeUpdates -> Connection to myST. If you do
not already have an account, create one. You may be prompted to select a working directory. If so,
select a path for your project without any spaces.

© 2025 Pearl Sullivan Engineering IDEAs Clinic 11


Figure 8: Log into myST

Select File > New, then choose STM32 Project.

Figure 9: New Project

It may take a minute, but when it’s ready a new window will pop up. Here we select the board we are
using. You are using a Nucleo64F401RE, so:

1. Click “Board Selector”


2. In the search box, type “NUCLEO-64”
3. Expand the drop down for MCU/MPU Series and select “STM32U0”
4. In the Boards List, choose “NUCLEO-U083RC”
5. Click Next

© 2025 Pearl Sullivan Engineering IDEAs Clinic 12


Figure 10: Board Selection

Name your project in the next window. It is best to keep spaces and special characters out of the
file name. Keep everything else in the default settings and click Finish. Leave the language as C, not
C++.

If you see the prompt titled “Open Associated Perspective?” click “Yes”

Next, click “Yes” on the Board Project Options screen

Click on PB5 and select GPIO_Output. This configures the pin as a digital output (GPIO stands for
general purpose input output). Nothing else needs to be configured for PB5.

Click on PB3 and select GPIO_Input. The pins should be green with their respective mode
displayed as shown in Figure 11 below.

© 2025 Pearl Sullivan Engineering IDEAs Clinic 13


Figure 11: Pin Configuration

On the left side, under System core, select GPIO > PB3. Under GPIO Pull-up/Pull-down, select
Pull-down as shown in Figure 12 below.

© 2025 Pearl Sullivan Engineering IDEAs Clinic 14


Figure 12: GPIO Input Configuration

Save the .ioc file and select Yes to regenerate code. If you are prompted to “Open Associated
Perspective?” click Yes. This will update and open your main.c file. If main.c does not open, you can
find it in the project explorer under Core > Src > main.c

1.2.3 Code
Find the while loop in the main() function. Insert the following code:
/* USER CODE BEGIN 3 */

// Button is pressed
if(HAL_GPIO_ReadPin (GPIOB, GPIO_PIN_3)){
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5, GPIO_PIN_SET);
}
// Button is not pressed
else{
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5, GPIO_PIN_RESET);
}

HAL_GPIO_WritePin() is a function that sets the state of a digital output pin. Notice that we are
writing to GPIOB, and GPIO_PIN_5 (which is pin PB5). The last argument of the function determines
whether the pin is being set to “High” (Set) or “Low” (Reset).

HAL_GPIO_ReadPin() is a function that reads the state of a digital input pin. Notice that we are
reading from GPIOB, and GPIO_PIN_3 (which is pin PB3).

© 2025 Pearl Sullivan Engineering IDEAs Clinic 15


The above code reads the state of PB3. It sets PB5 to “High” when PB3 is “High” (which corresponds
to the button being pressed) and sets PB5 to “Low” when PB3 is “Low” (which corresponds to the
button not being pressed).

Run the code by selecting Run > Run. Leave the configuration as default. If prompted, update the
board firmware. To do this, click open in update mode, then upgrade.

1.2.4 Results
The system is working if the LED lights up when the button is pressed and turns off when the button
is not pressed.

1.3 Challenge
Create a system with 2 pushbuttons. You may need to ask for a second push button. One of the
buttons turns the LED on, and one of the buttons turns the LED off. The LED should remain on after
the “On” button has been released until the “Off” Button is pressed. The LED should remain off after
the “Off” button has been released until the “On” button is pressed. The functionality is parallel to a
set/reset coil pair in PLC programming.

Hint: You will need to connect a second GPIO Input. Make sure to use a digital input and configure
the new input in the .ioc file as you did in the first part of the activity. You will need to configure it as
a pull-down input just like the first button. See Figure 3 for the pinout map, as you will need to know
which port (e.g., PB3) is linked to the pin (e.g., D3).

© 2025 Pearl Sullivan Engineering IDEAs Clinic 16


2 Activity – Analog Inputs and UART
2.1 Background
2.1.1 Analog Signals
Earlier, digital signals were defined as signals that can take on 2 different states or voltage levels.
Analog signals, in contrast, can take on any voltage between its minimum and maximum.

2.1.2 Measuring Analog Signals using Microcontrollers - ADCs


Microcontrollers are digital devices, which makes it somewhat complicated to read analog signals.
Specialized devices are required to achieve this. ADCs (analog to digital converters) are used to
convert analog signals into digital data that can be interpreted by microcontrollers. Since a
specialized device is required to read analog signals, it requires configuration (the STM32 U083RC
has on board ADCs, but external ADCs exist as well).

2.1.3 Potentiometers
A potentiometer acts as a variable voltage divider. The outer pins are connected to a voltage supply
and ground, while the middle pin outputs a voltage between these two values, depending on the
position of the dial. The output voltage varies linearly with the dial's angle, ranging from 0 V (ground)
to the supplied voltage (3.3 V in this case).

Figure 13: Potentiometer

2.2 Tutorial
In this tutorial, you will connect the 3.3V pin to an ADC input pin, then read the voltage and print the
resulting value to a console using UART.

2.2.1 Hardware
Connect a wire between A0 and the 3V3 pin as shown in Figure 14.

© 2025 Pearl Sullivan Engineering IDEAs Clinic 17


Figure 14: Wiring Diagram for ADC Activity

Notice that we are essentially reading a digital “High” signal with an analog input pin. Since this is
the maximum voltage the ADC can read, we expect to read the maximum digital value.

2.2.2 Configuration
Switch tabs to the *.ioc file for your project. If you closed it, you can find it in the file tree in the
Project Explorer pane on the left side. Double click to open it.

Click PA0 and select ADC1_IN4, which selects ADC1 channel 4. The pin should be green and
display the pin mode as shown in Figure 15 below.

© 2025 Pearl Sullivan Engineering IDEAs Clinic 18


Figure 15: ADC Tutorial Pinout

Now we must configure the ADC. Select Analog > ADC1. In the Parameter settings change
Resolution to “ADC 8-bit resolution”. Change Continuous Conversion Mode to “Enabled”.
Change End of Conversion Selection to “End of sequence of conversion”. Keep the rest as
default. The configuration is shown in Figure 16 below.

© 2025 Pearl Sullivan Engineering IDEAs Clinic 19


Figure 16: ADC Tutorial Configuration

Note that the ADC is configured for 8-bit operation. This means that the maximum value is going to
be 28 - 1 = 255. Since we are supplying the ADC with the maximum input voltage, it should read 255.

Save the .ioc file and click “yes” if prompted to generate code.

2.2.3 Code
Import the following libraries by adding the following two “#include” statements. These go near the
top of the code under “#include “main.h” in the “private includes” section of the code.
/* USER CODE BEGIN Includes */
#include <string.h>
#include <stdio.h>

Start the ADC and declare variables to read the ADC and transmit them using UART.

© 2025 Pearl Sullivan Engineering IDEAs Clinic 20


/* USER CODE BEGIN 2 */

//Start the ADC


HAL_ADC_Start(&hadc1);

//Declare variables to hold ADC data, and UART Transmit messages


uint8_t ADC_Value;
char msg[20];

The ADC reads in an 8-bit unsigned integer, so we use a 8-bit unsigned integer to store it. We create
an array of chars as a buffer to transmit messages using UART.

In the main loop, we read from the ADC. First, we wait for the ADC to finish its conversion. Then we
read the ADC_Value and send it to the console using UART.

/* USER CODE BEGIN WHILE */


while (1)
{
// Wait for the ADC to finish conversion
HAL_ADC_PollForConversion(&hadc1, HAL_MAX_DELAY);

//Read ADC value


ADC_Value = HAL_ADC_GetValue(&hadc1);

//Transmit message through UART


printf("ADC Value: %d \n\r", ADC_Value);
/* USER CODE END WHILE */
}

2.2.4 UART Console


To see the values that are being written we need to use a UART console. For this example, we will
use a serial connection using STM32CudeIDE.

Before setting up the connection, we must determine which serial port to use.

With the STM32 board plugged into your computer, open device manager (Windows) and check
which port it is connected to. A sample of the port number displayed on Device Manager is shown
in Figure 17 below.

© 2025 Pearl Sullivan Engineering IDEAs Clinic 21


Figure 17: Device Manager with Port Number Displayed

Now we will set up the serial connection. If it isn’t already open at the bottom of the screen, click
Window > Show View > Console.

Figure 18: View Console

© 2025 Pearl Sullivan Engineering IDEAs Clinic 22


By default, this opens the Debug and Download console. We instead want the serial console. To do
this we need to open a new console, so click the new console button, then select “Command
Shell Console”.

Figure 19: New Command Shell Console

Under Connection Type select Serial Port. Click “New…” to create a new serial connection. Select
the correct serial port number, name the connection, and leave the rest of the settings unchanged.
Click “Finish” then “OK”.

Figure 20: New Serial Port Connection

This will open a new console, as shown in Figure 21. Notice that the ADC is reading 4095 as
mentioned above.

© 2025 Pearl Sullivan Engineering IDEAs Clinic 23


Figure 21: Serial Port Connection Console

2.3 Challenge
Use a potentiometer to control the frequency of a blinking LED. See the Potentiometers section for
how to use the potentiometer. The outer pins are connected to a voltage supply and ground, while
the middle pin outputs a voltage between these two values, depending on the position of the dial.

Hint: You may want to call the HAL_Delay(int milliseconds) function.

© 2025 Pearl Sullivan Engineering IDEAs Clinic 24


3 Activity 3 – PWM and Oscilloscope
3.1 Background
3.1.1 PWM
PWM (Pulse Width Modulation) is a technique that is used to approximate analog signals using
digital signals (for more on PWM, see page 3 of this document). Figure 22 and Figure 23
demonstrate a PWM signal over time. Figure 22 demonstrates the concept of duty cycle, which is
the percentage of time that the signal is in the “High” state for. Figure 23 demonstrates amplitude,
period and frequency. Amplitude is the voltage of the “High” state, or the maximum voltage that the
signal reaches. The period is the time for one full cycle to occur (time from rising edge to rising
edge). The frequency is 1 divided by the period.

Figure 22: PWM Duty Cycle

Figure 23: PWM Frequency and Amplitude

© 2025 Pearl Sullivan Engineering IDEAs Clinic 25


3.1.2 Oscilloscopes
Oscilloscopes are electronic devices that are used to view the voltage of a signal over time. Cursors
can be used to measure the time and voltage of signals. They are very useful for hardware
debugging since they give a visual of signals which can be compared with expected results.

3.2 Tutorial
In this tutorial, you will generate a PWM signal on the STM32 board and view the square waves on an
oscilloscope.

3.2.1 Hardware
Connect the jumpers and oscilloscope probes to the board as shown in Figure 24 and
Figure 25 below. (Yellow to D12, Black to GND)

Figure 24: PWM Board Connections Figure 25: Oscilloscope Probe Connection

Connect the probe to input 1 on the oscilloscope.

3.2.2 Configuration
Switch tabs to the *.ioc file for your project. If you closed it, you can find it in the file tree in the
Project Explorer pane on the left side. Double click to open it.

Select Timers > TIM3. Under Mode > Channel 1, select PWM Generation Channel 1. The Mode
tab should match Figure 26. If the Mode settings aren’t visible, they may be hidden by the

© 2025 Pearl Sullivan Engineering IDEAs Clinic 26


configuration pane. Click on the light grey bar above Configuration and pull down. The chip view
should now show PA6 in green with a name of TIM3_CH1.

Figure 26: PWM Mode Settings

Under the Configuration tab, ensure that the Counter Settings match Figure 27.

Figure 27: Counter Settings

© 2025 Pearl Sullivan Engineering IDEAs Clinic 27


The STM32 F401RE has an 84 MHz internal clock. Timer 3 will derive its clock signal from the
internal clock. The prescaler divides the internal clock frequency. We divide the internal clock by 84
so that we work with a 1 MHz clock. The prescaler is 0 coded (which means it starts counting at 0)
so we write 84 – 1.

The counter mode determines whether the counter counts upwards or downwards. We choose to
count upwards somewhat arbitrarily, as it could work counting down as well.

The counter period determines what number the counter will count to before resetting to 0, here we
choose 1000.

The internal clock division divides the internal clock further (similar to the prescaler). It is not
required here, since our desired division is possible just using the prescaler (dividing by 84 is
possible since it can be represented by a 16-bit unsigned integer).

The period, and thus the frequency can be determined by the internal clock frequency, prescaler,
and internal clock division according to the following equations.
𝑓𝑖𝑛𝑡𝑒𝑟𝑛𝑎𝑙 84 𝑀𝐻𝑧
𝑓𝑃𝑊𝑀 𝐶𝑙𝑜𝑐𝑘 = = = 1 𝑀𝐻𝑧
(𝑝𝑟𝑒𝑠𝑐𝑎𝑙𝑒𝑟 + 1)(𝑖𝑛𝑡𝑒𝑟𝑛𝑎𝑙 𝑐𝑙𝑜𝑐𝑘 𝑑𝑖𝑣𝑖𝑠𝑖𝑜𝑛) (84)(1)
𝐶𝑜𝑢𝑛𝑡𝑒𝑟 𝑃𝑒𝑟𝑖𝑜𝑑 1000
𝑇= = = 1 𝑚𝑠
𝑓𝑃𝑊𝑀 𝐶𝑙𝑜𝑐𝑘 1 𝑀𝐻𝑧
1
𝑓𝑃𝑊𝑀 = = 1000 𝐻𝑧
𝑇
Auto reload preload is disabled.

The trigger output settings are left as default.

Ensure that the PWM Generation Channel 1 settings match Figure 28.

Figure 28: PWM Generation Settings

Mode determines whether the signal will be in the “High” state while the counter is below or above
the Pulse value. Here we choose PWM mode 1 so that it remains high while it is less than the Pulse
value.

Pulse determines when the PWM switches states. It will switch from “High” to “Low” in mode 1, and
it will switch from “Low” to “High” in mode 2. Here the pulse is half of the counter period, which
means that the duty cycle will be 50%.

Output compare preload, fast mode, and CH polarity should be left in default settings.

© 2025 Pearl Sullivan Engineering IDEAs Clinic 28


Save the .ioc file and select “Yes” for generating code.

3.2.3 Code
In the main function ensure you have the following code. You only need to add the bolded PWM
start function line. The other code is auto-generated or should already be in your project from the
previous step.
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_ADC1_Init();
MX_TIM3_Init();
/* USER CODE BEGIN 2 */
HAL_ADC_Start(&hadc1);
HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_1);
/* USER CODE END 2 */

The PWM start function can be anywhere in your code after timer 3 is initialized.

3.2.4 Results
Once your code is flashed onto the STM32 board and is running, click the “Auto” button on the
oscilloscope, this will automatically configure the voltage and timing scale to view the signal
clearly. If this does not work, try setting the voltage scale to 1V/div and the timing scale to 100
microseconds. The desired square wave is shown in Figure 29.

Figure 29: PWM Results

Note that the duty cycle is 50%, the period is 1 ms, and the frequency is 1000 Hz.

3.3 Challenge 3
Use a potentiometer to control the duty cycle of the PWM.

Hint: keep track of your data types. You may need to do some research on how to update the
duration of the PWM pulse.

© 2025 Pearl Sullivan Engineering IDEAs Clinic 29


Related Resources
The following resources may be useful as you start to use your STM32 for other projects:

• Resistor Color Code Calculator


• Hints about Wiring for Your Electronics Projects
• STM32 Nucleo Development Board Pinout, Features and Applications ([Link])

© 2025 Pearl Sullivan Engineering IDEAs Clinic 30

You might also like