Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
en:iot-open:practical:hardware:sut:stm32:emb1a_1 [2024/03/03 08:40] ktokarzen:iot-open:practical:hardware:sut:stm32:emb1a_1 [2024/03/25 08:23] (current) – [Project information] pczekalski
Line 1: Line 1:
-====== STM_10: Use of fan ===== +====== STM_1A: Use of fan ===== 
-This scenario presents how to handle the brightness control of the tri-coloured LEDsOne is observable via camera, as presented in the figure (component 9A), while another is hidden inside the black enclosure and lights a colour sensor (component 9B). Both LEDs are electrically bound and cannot be controlled independentlyThose LEDs have 3 colour channels, controlled independently: R (Red), G (Green) and B (Blue). Mixing of those colours creates other ones, such as pink and violet. Each R G B channel can be controlled with a separate GPIO to switch it on or off or control brightness using a PWM signal, as presented in this tutorial.+This scenario presents how to control the fan's rotation speed with a PWM signalYou 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 ===== ===== Prerequisites =====
Line 8: Line 8:
   * [[en:iot-open:introductiontoembeddedprogramming2:cppfundamentals]]   * [[en:iot-open:introductiontoembeddedprogramming2:cppfundamentals]]
   * [[en:iot-open:hardware2:stm32]]   * [[en:iot-open:hardware2:stm32]]
-  * [[en:iot-open:hardware2:actuators_light|]]+  * [[en:iot-open:hardware2:actuators_motors|]]
   * [[en:iot-open:practical:hardware:sut:stm32|]]   * [[en:iot-open:practical:hardware:sut:stm32|]]
   * [[en:iot-open:embeddedcommunicationprotocols2:pwm|]]   * [[en:iot-open:embeddedcommunicationprotocols2:pwm|]]
Line 14: Line 14:
  
 ==== Task to be implemented ==== ==== Task to be implemented ====
-Implement a program that will light LEDs consecutively with R, G, and B. Use 50% of the maximum brightness. Use a PWM signal to control each GPIO for R, G and B, each colour separately to let you easily observe itYou can also experiment with various duty cycle settings for all channels to obtain different LED colours.+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 ==== ==== 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, all three LEDs are connected to one hardware timer instance. Thanks to this LEDs operate as three separate channels of a single timer+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 motorIt means that both elements use the same base frequency of the PWM signal. If you use 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, 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%.+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 low values of the duty cycle values do not provide enough energy to the fan motor so it does not start rotating from 1%.
  
 ==== Steps ==== ==== Steps ====
Line 25: Line 25:
 #include <HardwareTimer.h> #include <HardwareTimer.h>
 </code> </code>
-The hardware timer uses internal timer modules and allows us to define channels attached to the timer. We will use 1 channel per colour (R, G and B, so 3 in total). A PWM frequency is controlled with the timer to be shared for all 3 R, G and B channels. Channels control the PWM duty cycle.+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 === === Step 1 ===
-Include the library, and define PIN assignments to channels and PWM frequency (100Hz):+Include the library, and define PIN assignments to channels and PWM frequency (50Hz):
 <code c> <code c>
 #include <HardwareTimer.h> #include <HardwareTimer.h>
  
-// Pins definition for RGB LED +// Pin definition for the fan 
-#define LED_PWM_GREEN D3  //Arduino numbering D3, STM numbering PA_10 +#define FAN_PWM_PIN   PA1 //Arduino numbering A2, STM numbering PA_1
-#define LED_PWM_BLUE  D6  //Arduino numbering D6, STM numbering PA_8 +
-#define LED_PWM_RED   D9  //Arduino numbering D9, STM numbering PA_9+
  
-#define PWM_freq 100+#define PWM_fan_freq 50
 </code> </code>
-GPIO pins controlling  LEDS are D9 (Red), D3 (Green) and D6 (Blue), respectively.+The GPIO pin controlling the fan is A2 (PA_1 in STM-type numbering).
  
 === Step 2 === === Step 2 ===
Line 44: Line 42:
 <code c> <code c>
 // PWM variables definitions // PWM variables definitions
-HardwareTimer *MyTimLED              //Hardware Timer for PWM +HardwareTimer *MyTimFan //Hardware Timer class for Fan 
-uint32_t channelR, channelG, channelB; //RGB channels+uint32_t channelFAN     //1 channel
 </code> </code>
  
-Instantiate the timer object and initialise the 3 channels for PWM and make them dark (1duty cycle):+Instantiate the timer object and initialise the fan channel for PWM, no rotation (0%):
 <code c> <code c>
-//Create Timer instance type based on the chosen LED pin (the same timer is used for all LEDs).   +//Create Timer instance type based on the fan pin (the same timer is used for servo if needed).   
-TIM_TypeDef *Instance = (TIM_TypeDef *)pinmap_peripheral(digitalPinToPinName(LED_PWM_RED), PinMap_PWM);+TIM_TypeDef *FanInstance = (TIM_TypeDef *)pinmap_peripheral(digitalPinToPinName(FAN_PWM_PIN), PinMap_PWM);
          
-//Define three separate channels for LEDs +//Define the channel for fan 
-channelR = STM_PIN_CHANNEL(pinmap_function(digitalPinToPinName(LED_PWM_RED), PinMap_PWM)); +channelFAN = STM_PIN_CHANNEL(pinmap_function(digitalPinToPinName(FAN_PWM_PIN), PinMap_PWM));
-channelG = STM_PIN_CHANNEL(pinmap_function(digitalPinToPinName(LED_PWM_GREEN), PinMap_PWM)); +
-channelB = STM_PIN_CHANNEL(pinmap_function(digitalPinToPinName(LED_PWM_BLUE), PinMap_PWM));+
  
-// Instantiate HardwareTimer object. Thanks to 'new' instantiation, HardwareTimer is not destructed when setup() function is finished. +//Instantiate HardwareTimer object. Thanks to 'new' instantiation, HardwareTimer is not destructed when setup() function is finished. 
-MyTimLED = new HardwareTimer(Instance);+MyTimFan = new HardwareTimer(FanInstance);
  
-// Configure and start PWM +//Configure and start PWM 
-MyTimLED->setPWM(channelR,LED_PWM_RED,PWM_freq,1);   /* 100 Hertz, 1% dutycycle. */ +MyTimFan->setPWM(channelFANFAN_PWM_PIN500);   // 50 Hertz, 0% dutycycle.
-MyTimLED->setPWM(channelG,LED_PWM_GREEN,PWM_freq,1); /* 100 Hertz, 1% dutycycle. */ +
-MyTimLED->setPWM(channelB,LED_PWM_BLUE,PWM_freq,1);  /* 100 Hertz, 1% dutycycle. */+
 </code> </code>
  
-To modify the intensity of the chosen LED after initialisation use the setCaptureCompare function. In the following example, the duty_cycle_value can vary between 0 and 100.+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> <code c>
-MyTimLED->setCaptureCompare(channelR, duty_cycle_value, PERCENT_COMPARE_FORMAT);   //modify duty cycle+MyTimFan->setCaptureCompare(channelFAN, duty_cycle_value, PERCENT_COMPARE_FORMAT); //modify duty cycle 
 +};
 </code> </code>
  
 === Step 3 === === Step 3 ===
-Write a loop for each colour (R, G, then B) to light the colour from dark to max value+Write a loop to change the rotation from stop to max, and then from max to stop
-<note important> Full duty cycle (100%) will be too bright for the remote access video camera to handle itUse some reasonable range such as 0..50% is strongly advised.</note> +<note important> Too low duty cycle (1%) will not cause the fan to rotateYou can do some experiments on the minimal duty cycle which starts the fan to rotate.</note> 
-Mind to compose code to increase and decrease each colour. A hint is below (for channelR):+Mind to compose the code for cyclic speed changes up and down. A hint is below (for increasing the speed):
 <code c> <code c>
-  // Increase brightness +  // Increase fan speed 
-  for (int duty_cycle_value = 0; duty_cycle_value <= 50; duty_cycle_value++) { +  for (int duty_cycle_value = 0; duty_cycle_value <= 100; duty_cycle_value++) { 
-    // Gradually increase duty cycle for Red LED +    // Gradually increase duty cycle for fan speed 
-    MyTimLED->setCaptureCompare(channelR, duty_cycle_value, PERCENT_COMPARE_FORMAT);+    MyTimFan->setCaptureCompare(channelFAN, duty_cycle_value, PERCENT_COMPARE_FORMAT);
     delay(20); // Delay for smooth transition     delay(20); // Delay for smooth transition
   }   }
- 
-  delay(100); 
- 
-  // Decrease brightness 
-  for (int duty_cycle_value = 50; duty_cycle_value >= 0; duty_cycle_value--) { 
-    // Gradually decrease duty cycle for Red LED 
-    MyTimLED->setCaptureCompare(channelR, duty_cycle_value, PERCENT_COMPARE_FORMAT); 
-    delay(20); // Delay for smooth transition 
-  } 
- 
 </code> </code>
 ==== 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 changes in the fan's rotation speed. It is advisable to connect this scenario with the scenario for reading the pressure measurements.
  
 ===== FAQ ===== ===== FAQ =====
-**What is the maximum number of channels?**: the MCU we use here is STM32WB55 on the Nucleo board. The pins available are connected to timer 1 with three PWM channels (connected to our RGB LED) and timer 2 with 4 PWM channels (with two of them connected to the servo and fan described in other scenarios). A single timer can generate PWM signals with independent duty cycles and identical frequency.+**What is the maximum number of channels?**: the MCU we use here is STM32WB55 on the Nucleo board. The pins available are connected to timer 1 with three PWM channels (connected to RGB LED described in another scenario) and timer 2 with 4 PWM channels (with two of them connected to the servo and fan). A single timer can generate PWM signals with independent duty cycles and identical frequency.
 \\ \\
-**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, and 1-16 bits numbers.+**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, and 1-16 bits numbers.
 \\ \\
-**What PWM frequency should I use?**: there is no straightforward answer to this question: assuming you observe LED remotely with a camera, even 50Hz would be enough. But it would give a severe flickering experience to the live user, on the other handIn the example abovewe propose 100Hz, but this MCU can easily handle higher frequencies.+**What PWM frequency should I use?**: It depends on the signal useFor the fan or LEDsno 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 =====
 {{:en:iot-open:logo_iot_200_px.png?200|}}\\ {{:en:iot-open:logo_iot_200_px.png?200|}}\\
Line 120: Line 106:
 {{:en:iot-open:ccbync.png?100|}} {{:en:iot-open:ccbync.png?100|}}
 </figure> </figure>
- +</WRAP>
- +
en/iot-open/practical/hardware/sut/stm32/emb1a_1.1709455251.txt.gz · Last modified: 2024/03/03 08:40 by ktokarz
CC Attribution-Share Alike 4.0 International
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0