This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
en:iot-open:practical:hardware:sut:esp32:emb9a_1 [2024/02/29 16:31] – [Suggested Readings and Knowledge Resources] pczekalski | en:iot-open:practical:hardware:sut:esp32:emb9a_1 [2024/03/22 10:00] (current) – [Project information] pczekalski | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== EMB9A: | + | ====== EMB9A: |
- | A colour sensor (TCS 34725) can detect | + | This scenario presents how to handle |
===== Prerequisites ===== | ===== Prerequisites ===== | ||
- | To implement this scenario, it is necessary to get familiar with the following scenarios first: | + | A good understanding of the PWM signal and duty cycle is necessary. We also use built-in timers |
- | * [[en: | + | |
- | * [[en:iot-open: | + | |
- | To simplify TCS sensor use, we will use a dedicated library: | ||
- | <code bash> | ||
- | lib_deps = adafruit/ | ||
- | </ | ||
- | <note important> | ||
===== Suggested Readings and Knowledge Resources ===== | ===== Suggested Readings and Knowledge Resources ===== | ||
* [[en: | * [[en: | ||
* [[en: | * [[en: | ||
+ | * [[en: | ||
* [[en: | * [[en: | ||
* [[en: | * [[en: | ||
- | * [[en: | ||
===== Hands-on Lab Scenario ===== | ===== Hands-on Lab Scenario ===== | ||
==== Task to be implemented ==== | ==== Task to be implemented ==== | ||
- | // | + | Implement |
==== Start ==== | ==== Start ==== | ||
- | //Write starting conditions, i.e. what to do in the beginning, what to pay attention to before beginning, how the mechanical part should look, etc.// Include needed compiler configuration, etc. | + | Assuming you will use 8-bit PWM resolution, the minimum value is 0, and the max (full brightness) is 255. Note that full brightness may be too bright for the observation camera, so consider using a range between 0 and 60 (eventually up to 100). |
==== Steps ==== | ==== Steps ==== | ||
- | // Write some extra information if, i.e. some steps are optional; otherwise, cancel this paragraph (but do not remove | + | To use PWM in ESP32, it is best to use built-in '' |
+ | The '' | ||
=== Step 1 === | === Step 1 === | ||
- | //Describe activities done in Step 1.// | + | Define some parameters, including channel numbers, PWM resolution (here 8-bit) and PWM frequency (5000Hz): |
+ | <code c> | ||
+ | #define RGBLED_B_PIN 26 | ||
+ | #define RGBLED_G_PIN 21 | ||
+ | #define RGBLED_R_PIN 33 | ||
- | ... | + | #define PWM1_Ch |
+ | #define PWM2_Ch | ||
+ | #define PWM3_Ch | ||
- | === Step n === | + | #define PWM_Res |
- | //Describe activities done in Step n.// | + | #define PWM_Freq |
+ | </ | ||
+ | GPIO pins controlling | ||
+ | |||
+ | === Step 2 === | ||
+ | Initialise 3 channels for PWM and make them dark: | ||
+ | <code c> | ||
+ | ledcSetup(PWM1_Ch, | ||
+ | ledcSetup(PWM2_Ch, | ||
+ | ledcSetup(PWM3_Ch, | ||
+ | ledcAttachPin(RGBLED_R_PIN, | ||
+ | ledcAttachPin(RGBLED_G_PIN, | ||
+ | ledcAttachPin(RGBLED_B_PIN, | ||
+ | delay(100); | ||
+ | ledcWrite(PWM1_Ch, | ||
+ | ledcWrite(PWM2_Ch, | ||
+ | ledcWrite(PWM3_Ch, | ||
+ | </code> | ||
+ | To control the LED (via PWM), use '' | ||
+ | === Step 3 === | ||
+ | Write a loop for each colour (R, G, then B) to light the colour from dark to max value (60 or 100, give it a test). | ||
+ | <note important> | ||
+ | Mind to compose code to increase and decrease each colour. A hint is below (PWM Channel 3 so that controls Blue): | ||
+ | <code c> | ||
+ | // Increase brightness | ||
+ | for (int dutyCycle = 0; dutyCycle <= 100; dutyCycle++) { | ||
+ | // Gradually increase duty cycle for Red LED | ||
+ | ledcWrite(PWM3_Ch, | ||
+ | delay(20); // Delay for smooth transition | ||
+ | } | ||
+ | |||
+ | delay(100); | ||
+ | |||
+ | // Decrease brightness | ||
+ | for (int dutyCycle = 100; dutyCycle >= 0; dutyCycle--) { | ||
+ | // Gradually decrease duty cycle for Red LED | ||
+ | ledcWrite(PWM3_Ch, | ||
+ | delay(20); // Delay for smooth transition | ||
+ | } | ||
+ | |||
+ | </code> | ||
+ | < | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | </ | ||
==== Result validation ==== | ==== Result validation ==== | ||
- | //Provide some result validation methods for self-assessment.// | + | You should be able to observe the pulsing colours of the RGB LED, increasing and decreasing brightness linearly. |
===== FAQ ===== | ===== FAQ ===== | ||
- | This section is to be extended as new questions appear. \\ | ||
- | When using the printed version of this manual, please refer to the latest online version of this document to obtain the valid and up-to-date list of the FAQ. | ||
- | //Provide some FAQs in the following form:\\ | ||
- | **Question? | ||
- | // | ||
+ | **What if I bind a PWM channel to more than one GPIO?**: As you control the duty cycle writing to the channel rather than the GPIO, you will control those GPIOs simultaneously (in parallel) with the same signal. That can be handy to control parallelly separate devices that should behave the same way, i.e. servos. | ||
+ | \\ | ||
+ | **What is the maximum number of channels? | ||
+ | \\ | ||
+ | **What is the maximum bit resolution for PWM?**: it is between 1 and 14 bits in this particular MCU. | ||
+ | \\ | ||
+ | **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 hand. In the example above, we propose 5kHz, which this MCU can easily handle. | ||
+ | |||
+ | <WRAP noprint> | ||
===== Project information ===== | ===== Project information ===== | ||
{{: | {{: | ||
Line 62: | Line 112: | ||
{{: | {{: | ||
</ | </ | ||
- | + | </ | |
- | + |