No ACL setup yet! Denying access to everyone.
en:edu:zmitac:macro:embedeed:lab:stm32-pwm
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| en:edu:zmitac:macro:embedeed:lab:stm32-pwm [2024/03/04 21:39] – [Step 1] ktokarz | en:edu:zmitac:macro:embedeed:lab:stm32-pwm [2025/04/02 08:50] (current) – ktokarz | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ====== STM32 Control of build-in LEDs with PWM ===== | + | ====== STM32 Control of built-in LEDs with PWM ===== |
| - | This exercise presents how to handle the brightness control of three LEDs mounted on the Nucleo STM32WB55 board. LEDs are electrically connected to microcontroller' | + | This exercise presents how to handle the brightness control of three LEDs mounted on the Nucleo STM32WB55 board. LEDs are electrically connected to the microcontroller' |
| ===== Prerequisites ===== | ===== Prerequisites ===== | ||
| Line 6: | Line 6: | ||
| ==== Tasks to be implemented ==== | ==== Tasks to be implemented ==== | ||
| - | 1. Write the program that will switch the red LED for one second, then the green LED for one second, and the blue LED for one second repeatedly. This part is to be familiar with the environment, | + | 1. Write the program that will switch the red LED for one second, then the green LED for one second, and the blue LED for one second repeatedly. This part is to recall the knowledge from the first STM laboratory, |
| \\ | \\ | ||
| 2. Implement a program that will light LEDs consecutively with red, green, and blue with a PWM signal. Every LED should start from dark, go up to the maximum brightness and then go down to dark. This must be implemented with the use of a timer. | 2. Implement a program that will light LEDs consecutively with red, green, and blue with a PWM signal. Every LED should start from dark, go up to the maximum brightness and then go down to dark. This must be implemented with the use of a timer. | ||
| ==== Task 1 ==== | ==== Task 1 ==== | ||
| - | In the Arduino framework for STM32 handling the GPIO pins is done in the same way as for the classic Arduino board. First, the pin should be configured as the output. | + | In the Arduino framework for STM32 handling the GPIO pins is done the same way as for the classic Arduino board. First, the pin should be configured as the output. |
| <code c> | <code c> | ||
| pinMode(LED_RED, | pinMode(LED_RED, | ||
| </ | </ | ||
| - | The red LED is connected to the pin PB1, the green LED to the pin PB0, and the blue LED to the pin PB5. LED is lit when the pin state is " | ||
| - | The hardware timer library implements functions which allow us to control | + | To turn the LED on we have to set the output pin to " |
| - | In the case of setting the PWM duty cycle expressed in percentages, the minimum value is 0, and the max (full brightness) is 100. Note that full brightness may be too bright for the observation camera, so consider using a range between 0 and 50%. | + | <code c> |
| + | DigitalWrite(LED_RED, 1); | ||
| + | </ | ||
| - | ==== Steps ==== | + | To turn the LED off we have to clear the output pin. |
| - | To use PWM in STM32WB55, it is best to use a built-in hardware timer library. | + | < |
| - | < | + | DigitalWrite(LED_RED, |
| - | #include < | + | |
| </ | </ | ||
| - | The hardware timer uses internal timer modules and allows us to define channels attached | + | Use the delay function |
| + | |||
| + | ==== Task 2 ==== | ||
| + | To do the second task it is necessary | ||
| + | === Hardware we use === | ||
| + | In the STM32WB55, there are two timers capable of generating the PWM signal for up to four channels | ||
| + | |||
| + | === Software library === | ||
| + | The Arduino framework for STM implements the Hardware Timer library. The library implements a set of functions which allow us to choose the working mode of the timer, enable or disable its operation, and control the duty cycle of the PWM signal expressing it in different formats, including percentages. | ||
| === Step 1 === | === Step 1 === | ||
| - | Include | + | In the first step, we include |
| + | < | ||
| + | We use pins which are not connected to on-board LEDs, so the pins here are dummies. In the case of our own PCB design, it is possible to connect proper outputs to LEDs directly and control them without the need to use interrupts. | ||
| + | </ | ||
| <code c> | <code c> | ||
| #include < | #include < | ||
| Line 38: | Line 50: | ||
| #define PWM_freq 100 | #define PWM_freq 100 | ||
| </ | </ | ||
| - | GPIO pins controlling | + | GPIO dummy pins for PWM channels |
| === Step 2 === | === Step 2 === | ||
| Line 58: | Line 70: | ||
| channelB = STM_PIN_CHANNEL(pinmap_function(digitalPinToPinName(LED_PWM_BLUE), | channelB = STM_PIN_CHANNEL(pinmap_function(digitalPinToPinName(LED_PWM_BLUE), | ||
| - | // Instantiate HardwareTimer object. Thanks to ' | + | // Instantiate HardwareTimer object. Thanks to the ' |
| MyTimLED = new HardwareTimer(Instance); | MyTimLED = new HardwareTimer(Instance); | ||
| // Configure and start PWM | // Configure and start PWM | ||
| - | MyTimLED-> | + | MyTimLED-> |
| - | MyTimLED-> | + | MyTimLED-> |
| - | MyTimLED-> | + | MyTimLED-> |
| </ | </ | ||
| - | To modify the intensity of the chosen LED after initialisation | + | |
| + | |||
| + | === Step 3 === | ||
| + | Define callback functions to control real on-board LEDs. There will be one common callback to turn on all LEDs at the beginning of every cycle, and three separate callbacks for three channels to turn LEDs off. This enables different brightness of specific LEDs. | ||
| + | < | ||
| + | MyTimLED-> | ||
| + | MyTimLED-> | ||
| + | </ | ||
| + | |||
| + | === Step 4 === | ||
| + | To modify the intensity of the chosen LED in the loop() function, | ||
| <code c> | <code c> | ||
| MyTimLED-> | MyTimLED-> | ||
| </ | </ | ||
| - | === Step 3 === | + | === Step 5 === |
| Write a loop for each colour (R, G, then B) to light the colour from dark to max value. | Write a loop for each colour (R, G, then B) to light the colour from dark to max value. | ||
| - | <note important> | ||
| Mind to compose code to increase and decrease each colour. A hint is below (for channelR): | Mind to compose code to increase and decrease each colour. A hint is below (for channelR): | ||
| <code c> | <code c> | ||
| Line 95: | Line 116: | ||
| </ | </ | ||
| ==== Result validation ==== | ==== Result validation ==== | ||
| - | You should be able to observe the pulsing colours of the RGB LED, increasing and decreasing brightness linearly. | + | You should be able to observe the pulsing colours of the LEDs, increasing and decreasing brightness linearly. |
| ===== FAQ ===== | ===== FAQ ===== | ||
| - | **What is the maximum number of channels? | + | **What is the maximum number of channels? |
| \\ | \\ | ||
| - | **What is the maximum bit resolution for PWM?**: Maximum resolution is 16 bits. Note that we can express the duty cycle in a variety of formats. In the presented example we expressed it in percentage so it varies between 1 and 100. It can be also expressed in ticks, Hertz, microseconds, | + | **What is the maximum bit resolution for PWM?**: Maximum resolution is 16 bits. Note that we can express the duty cycle in a variety of formats. In the presented example, we expressed it in percentage so it varies between 1 and 100. It can also be expressed in ticks, Hertz, microseconds, |
| \\ | \\ | ||
| - | **What PWM frequency should I use? | + | **What PWM frequency should I use? |
en/edu/zmitac/macro/embedeed/lab/stm32-pwm.1709584742.txt.gz · Last modified: by ktokarz
