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:esp32:emb1a_1 [2024/03/05 20:56] – [Result validation] pczekalskien:iot-open:practical:hardware:sut:esp32:emb1a_1 [2024/04/09 12:55] (current) – [FAQ] pczekalski
Line 1: Line 1:
-====== EMB1A: Use of RGB LEDs =====+====== EMB1A: Controlling a FAN with PWM =====
 This scenario presents how to handle the rotation speed of the fan. It is done using a PWM signal, as presented in this tutorial. The fan pumps the air into the yellow pressure chamber with a pressure sensor inside. Because it is pretty hard to monitor rotation speed with the camera, this scenario should be run along with scenario following scenario [[en:iot-open:practical:hardware:sut:esp32:emb1b_1]] that will indirectly measure rotation speed using pressure changes. This scenario presents how to handle the rotation speed of the fan. It is done using a PWM signal, as presented in this tutorial. The fan pumps the air into the yellow pressure chamber with a pressure sensor inside. Because it is pretty hard to monitor rotation speed with the camera, this scenario should be run along with scenario following scenario [[en:iot-open:practical:hardware:sut:esp32:emb1b_1]] that will indirectly measure rotation speed using pressure changes.
  
 ===== Prerequisites ===== ===== 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 ESP32 chip. In this case, we do not use an external library; instead, we use built-in tools in the Arduino framework for ESP32 so that no additional libraries will be included in the project.+A good understanding of the PWM signal and duty cycle is necessary. We also use built-in timers to control the ESP32 chip'PWM hardware channels. In this case, we do not use an external library; instead, we use built-in tools in the Arduino framework for ESP32so no additional libraries will be included in the project.
  
 ===== Suggested Readings and Knowledge Resources ===== ===== Suggested Readings and Knowledge Resources =====
Line 21: Line 21:
 ==== Steps ==== ==== Steps ====
 To use PWM in ESP32, it is best to use built-in ''ledc*'' functions [[https://docs.espressif.com/projects/esp-idf/en/stable/esp32/api-reference/peripherals/ledc.html|LEDC documentation on the ESP32 manufacturer's page]]. To use PWM in ESP32, it is best to use built-in ''ledc*'' functions [[https://docs.espressif.com/projects/esp-idf/en/stable/esp32/api-reference/peripherals/ledc.html|LEDC documentation on the ESP32 manufacturer's page]].
-The ''leds'' use timers and have channels attached to the timer. We will use one channel numbered 0. The channel controls the PWM duty cycle. Do not be mislead with the use of the ''leds'' functions: they generate PWM signal and it is meaningless either you control LED, bulb or fan (as here).+The "ledsuse timers and have channels attached to the timer. We will use one channel numbered 0. The channel controls the PWM duty cycle. Do not be misled by the "ledsfunctions: they generate PWM signaland it is meaningless whether you control an LED, bulbor fan (as here).
 === Step 1 === === Step 1 ===
 Define some parameters, including channel numbers, PWM resolution (here 8-bit) and PWM frequency (1000Hz): Define some parameters, including channel numbers, PWM resolution (here 8-bit) and PWM frequency (1000Hz):
Line 33: Line 33:
  
 === Step 2 === === Step 2 ===
-Initialise 3 channels for PWM and make them dark:+Initialise fan PWM channel and make it to stop (duty cycle 0):
 <code c> <code c>
-    ledcSetup(fanPWMChannelfanPWMFrequencyfanPWMResolution); +    ledcSetup(FAN_PWM_ChFAN_PWM_FREQFAN_PWM_RESOLUTION); //Initialise channel 
-    ledcAttachPin(fanPWMPinfanPWMChannel);+    ledcAttachPin(FAN_PINFAN_PWM_Ch);                      //Bind it to the PWM
     delay(100);     delay(100);
-    ledcWrite(fanPWMChannel,0);+    ledcWrite(FAN_PWM_Ch,0);                                 //Write to CHANNEL, not to PIN!
 </code> </code>
-To control the fan rotation speed (via PWM), use ''ledcWrite(PWM_Channel, duty_cycle_value);''.+
 === Step 3 === === Step 3 ===
 +To control the fan rotation speed (via PWM), use ''ledcWrite(FAN_PWM_Ch, duty_cycle_value);''.\\
 +<note tip>Note you write to **channel**, not to the **pin**!\\
 +A common source code mistake causes the fan not to operate correctly.</note>
 +A ''ledcWrite'' with a ''duty_cycle_value'' equal to 0 causes the fan to stop.\\
 +The maximum ''duty_cycle_value'' is determined by the ''FAN_PWM_RESOLUTION'', which is 8-bit in our case, so maximum is 255. In that case, the fan operates at full speed.
  
 <note>There is a number of handy functions in the ''ledc'' library, including (among others): <note>There is a number of handy functions in the ''ledc'' library, including (among others):
Line 54: Line 59:
 ===== FAQ ===== ===== FAQ =====
  
-**The fan is not rotating?**: Besides possible configuration and programming errors, it is essential to understand that setting duty cycle, e.g. to 1, won't start spinning: a minimum value (a threshold) causes rotation. Moreover, the relation between rotation speed, air pressure and duty cycle controlling the fan is not linear.+**The fan rotates itself. Why?**: It is connected via the MOS FET transistor that tends to saturate when the GPIO (35) is not controlling it. So, to ensure the fan is stopped, bind a PWM channel to it and force the duty cycle set to 0. 
 + 
 +**The fan is not rotating?**: Besides possible configuration and programming errors, it is essential to understand that setting duty cycle, e.g. to 1, won't start spinning: a minimum value (a threshold) causes rotation. Moreover, the relation between rotation speed, air pressure and duty cycle controlling the fan is not linear. You may also be a victim of the common coding mistake of using a GPIO number instead of the channel number (first parameter) in the ''ledcWrite'' function: use the PWM channel number!
 \\ \\
 **What is the maximum number of channels?**: the MCU we use here is ESP32-S3, so it has 8 PWM channels. You can use timers and software implementation of the PWM signal if you need more, but that is a bit tricky and may not be as precise as hardware PWM implementation. **What is the maximum number of channels?**: the MCU we use here is ESP32-S3, so it has 8 PWM channels. You can use timers and software implementation of the PWM signal if you need more, but that is a bit tricky and may not be as precise as hardware PWM implementation.
 \\ \\
-**What is the maximum bit resolution for PWM?**: it is between 1 and 14 bits in this particular MCU.+**What is the maximum bit resolution for PWM?**: In this particular MCU, it is between 1 and 14 bits.
 \\ \\
 **What PWM frequency should I use?**: there is no straightforward answer to this question, but setting too low a frequency will cause the inability to control the fan: 1000Hz (1kHz) seems reasonable and has been tested with this configuration. **What PWM frequency should I use?**: there is no straightforward answer to this question, but setting too low a frequency will cause the inability to control the fan: 1000Hz (1kHz) seems reasonable and has been tested with this configuration.
  
 +<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 78: Line 86:
 {{:en:iot-open:ccbync.png?100|}} {{:en:iot-open:ccbync.png?100|}}
 </figure> </figure>
- +</WRAP>
- +
en/iot-open/practical/hardware/sut/esp32/emb1a_1.1709672172.txt.gz · Last modified: 2024/03/05 20:56 by pczekalski
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