This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
en:iot-open:practical:hardware:sut:stm32:emb1a_1 [2024/03/03 08:39] – created ktokarz | en:iot-open:practical:hardware:sut:stm32:emb1a_1 [2024/03/25 08:23] (current) – [Project information] pczekalski | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== STM_1A: Use of fan ===== | ||
+ | This scenario presents how to control the fan's rotation speed with a PWM signal. You can observe the rotation via the camera, but it would be rather hard to notice the change in the rotation speed. The fan is mounted on the top of the pressure tube and blows the air onto the pressure sensor. You can observe changes in sensor readings according to different fan speeds. How to use the sensor is shown in another scenario. | ||
+ | |||
+ | ===== Prerequisites ===== | ||
+ | A good understanding of the PWM signal and duty cycle is necessary. We also use built-in timers to control the PWM hardware channels of the STM32WB55 chip. In this case, we do not use an external library; instead, we use the hardware timer library built in the Arduino Core STM32 framework (stm32duino)((" | ||
+ | |||
+ | ===== Suggested Readings and Knowledge Resources ===== | ||
+ | * [[en: | ||
+ | * [[en: | ||
+ | * [[en: | ||
+ | * [[en: | ||
+ | * [[en: | ||
+ | ===== Hands-on Lab Scenario ===== | ||
+ | |||
+ | ==== Task to be implemented ==== | ||
+ | Implement a program that will change the fan's rotation speed up from 0% to 100% and down from 100% to 0%. Cotrol the rotation speed with the PWM signal. | ||
+ | |||
+ | ==== Start ==== | ||
+ | The hardware timer library implements functions which allow us to control the duty cycle of the PWM signal and express it in different formats, including percentages. In the laboratory equipment, the fan is connected to the hardware timer instance shared with the servo motor. It means that both elements use the same base frequency of the PWM signal. If you use a fan and servo in the same project the frequency of the PMW signal need to match the servo requirements. If you use the fan only the frequency can be freely chosen. | ||
+ | In the case of setting the PWM duty cycle expressed in percentages, | ||
+ | |||
+ | ==== Steps ==== | ||
+ | To use PWM in STM32WB55, it is best to use a built-in hardware timer library. | ||
+ | < | ||
+ | #include < | ||
+ | </ | ||
+ | The hardware timer library uses internal timer modules and allows us to define channels attached to the timer. In this example, we will use 1 channel only for the fan. A PWM frequency is controlled with the timer shared with the servo motor. Channels control the PWM duty cycle. | ||
+ | === Step 1 === | ||
+ | Include the library, and define PIN assignments to channels and PWM frequency (50Hz): | ||
+ | <code c> | ||
+ | #include < | ||
+ | |||
+ | // Pin definition for the fan | ||
+ | #define FAN_PWM_PIN | ||
+ | |||
+ | #define PWM_fan_freq 50 | ||
+ | </ | ||
+ | The GPIO pin controlling the fan is A2 (PA_1 in STM-type numbering). | ||
+ | |||
+ | === Step 2 === | ||
+ | Define variables for the timer object and handlers of channels. | ||
+ | <code c> | ||
+ | // PWM variables definitions | ||
+ | HardwareTimer *MyTimFan; | ||
+ | uint32_t channelFAN; | ||
+ | </ | ||
+ | |||
+ | Instantiate the timer object and initialise the fan channel for PWM, no rotation (0%): | ||
+ | <code c> | ||
+ | //Create Timer instance type based on the fan pin (the same timer is used for servo if needed). | ||
+ | TIM_TypeDef *FanInstance = (TIM_TypeDef *)pinmap_peripheral(digitalPinToPinName(FAN_PWM_PIN), | ||
+ | | ||
+ | //Define the channel for fan | ||
+ | channelFAN = STM_PIN_CHANNEL(pinmap_function(digitalPinToPinName(FAN_PWM_PIN), | ||
+ | |||
+ | // | ||
+ | MyTimFan = new HardwareTimer(FanInstance); | ||
+ | |||
+ | //Configure and start PWM | ||
+ | MyTimFan-> | ||
+ | </ | ||
+ | |||
+ | To modify the fan's rotation speed after initialisation use the setCaptureCompare function. In the following example, the duty_cycle_value can vary between 0 and 100. | ||
+ | <code c> | ||
+ | MyTimFan-> | ||
+ | }; | ||
+ | </ | ||
+ | |||
+ | === Step 3 === | ||
+ | Write a loop to change the rotation from stop to max, and then from max to stop. | ||
+ | <note important> | ||
+ | Mind to compose the code for cyclic speed changes up and down. A hint is below (for increasing the speed): | ||
+ | <code c> | ||
+ | // Increase fan speed | ||
+ | for (int duty_cycle_value = 0; duty_cycle_value <= 100; duty_cycle_value++) { | ||
+ | // Gradually increase duty cycle for fan speed | ||
+ | MyTimFan-> | ||
+ | delay(20); // Delay for smooth transition | ||
+ | } | ||
+ | </ | ||
+ | ==== Result validation ==== | ||
+ | You should be able to observe the changes in the fan's rotation speed. It is advisable to connect this scenario with the scenario for reading the pressure measurements. | ||
+ | |||
+ | ===== FAQ ===== | ||
+ | **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, microseconds, | ||
+ | \\ | ||
+ | **What PWM frequency should I use?**: It depends on the signal use. For the fan or LEDs, no specific frequency is required, but for the servo, the frequency needs to be precisely 50Hz. If you use a servo and fan in the same project set the timer frequency at 50Hz. | ||
+ | |||
+ | <WRAP noprint> | ||
===== Project information ===== | ===== Project information ===== | ||
{{: | {{: | ||
Line 15: | Line 106: | ||
{{: | {{: | ||
</ | </ | ||
- | + | </ | |
- | + |