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/03/02 12:03] – pczekalski | en:iot-open:practical:hardware:sut:esp32:emb9a_1 [2024/03/22 12:00] (current) – [Project information] pczekalski | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== EMB9A: Use of RGB LEDs ===== | ||
| + | This scenario presents how to handle the brightness control of the tri-coloured LEDs. One 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 independently. Those LEDs have 3 colour channels, controlled independently: | ||
| + | ===== 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. | ||
| + | |||
| + | ===== Suggested Readings and Knowledge Resources ===== | ||
| + | * [[en: | ||
| + | * [[en: | ||
| + | * [[en: | ||
| + | * [[en: | ||
| + | * [[en: | ||
| + | ===== Hands-on Lab Scenario ===== | ||
| + | |||
| + | ==== 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 it. | ||
| + | |||
| + | ==== Start ==== | ||
| + | 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 ==== | ||
| + | To use PWM in ESP32, it is best to use built-in '' | ||
| + | The '' | ||
| + | === 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 | ||
| + | |||
| + | #define PWM_Res | ||
| + | #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, | ||
| + | </ | ||
| + | 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 | ||
| + | } | ||
| + | |||
| + | </ | ||
| + | |||
| + | < | ||
| + | * '' | ||
| + | * '' | ||
| + | * '' | ||
| + | * '' | ||
| + | </ | ||
| + | ==== Result validation ==== | ||
| + | You should be able to observe the pulsing colours of the RGB LED, increasing and decreasing brightness linearly. | ||
| + | |||
| + | ===== FAQ ===== | ||
| + | |||
| + | **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 ===== | ||
| + | {{: | ||
| + | This Intellectual Output was implemented under the Erasmus+ KA2.\\ | ||
| + | Project IOT-OPEN.EU Reloaded – Education-based strengthening of the European universities, | ||
| + | Project number: 2022-1-PL01-KA220-HED-000085090. | ||
| + | |||
| + | **__Erasmus+ Disclaimer__**\\ | ||
| + | This project has been funded with support from the European Commission. \\ | ||
| + | This publication reflects the views of only the author, and the Commission cannot be held responsible for any use that may be made of the information contained therein. | ||
| + | |||
| + | **__Copyright Notice__**\\ | ||
| + | This content was created by the IOT-OPEN.EU Reloaded consortium, 2022, | ||
| + | The content is Copyrighted and distributed under CC BY-NC [[https:// | ||
| + | <figure label> | ||
| + | {{: | ||
| + | </ | ||
| + | </ | ||