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 09:38] – [Steps] 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: | + | |
- | A good understanding of the hardware timers is essential if you plan to use asynchronous programming (see note below). Consider getting familiar with | ||
- | |||
- | 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: | ||
- | * [[https:// | ||
===== Hands-on Lab Scenario ===== | ===== Hands-on Lab Scenario ===== | ||
==== Task to be implemented ==== | ==== Task to be implemented ==== | ||
- | Create | + | Implement |
- | + | ||
- | <note tip> | + | |
- | * simple: a dummy loop where you set RGB LED values, read from TCS and display on LCD, | + | |
- | * advanced: where the data is read and presented on the LCD in one or two asynchronous routines run by timers. | + | |
- | </ | + | |
==== Start ==== | ==== Start ==== | ||
- | Check if you can see the LCD and RGB LED in the camera view. | + | Assuming |
- | <note important> | + | |
==== Steps ==== | ==== Steps ==== | ||
- | In the steps below, we present only the part for reading from the TCS sensor. Control of RGB LED with PWM and handling LCD is presented | + | To use PWM in ESP32, it is best to use built-in '' |
+ | The '' | ||
=== Step 1 === | === Step 1 === | ||
- | Included necessary library: | + | Define some parameters, including channel numbers, PWM resolution (here 8-bit) and PWM frequency (5000Hz): |
<code c> | <code c> | ||
- | #include " | + | #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 === | === Step 2 === | ||
- | Declare | + | Initialise 3 channels for PWM and make them dark: |
<code c> | <code c> | ||
- | static Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_300MS, TCS34725_GAIN_1X); | + | ledcSetup(PWM1_Ch, PWM_Freq, PWM_Res); |
- | static uint16_t r, g, b, c; | + | |
- | static bool isTCSOk = false; | + | ledcSetup(PWM3_Ch, PWM_Freq, PWM_Res); |
+ | | ||
+ | ledcAttachPin(RGBLED_G_PIN, | ||
+ | ledcAttachPin(RGBLED_B_PIN, | ||
+ | delay(100); | ||
+ | ledcWrite(PWM1_Ch, | ||
+ | ledcWrite(PWM2_Ch, | ||
+ | ledcWrite(PWM3_Ch, | ||
</ | </ | ||
- | A word of explanation regarding | + | To control |
- | * '' | + | |
- | * '' | + | |
- | + | ||
- | Refer to the Q&A section for the ranges. | + | |
=== Step 3 === | === Step 3 === | ||
- | Initialise | + | 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> | <code c> | ||
- | | + | |
- | </code> | + | for (int dutyCycle = 0; dutyCycle |
- | You communicate with TCS sensor via I2C interface. In standard and typical configurations there is no need to instantiate '' | + | |
+ | | ||
+ | delay(20); // Delay for smooth transition | ||
+ | } | ||
+ | |||
+ | delay(100); | ||
- | === Step 4 === | + | // Decrease brightness |
- | To read, use the following code: | + | for (int dutyCycle |
- | <code c> | + | // Gradually decrease duty cycle for Red LED |
- | if(isTCSOk) | + | |
- | { | + | |
- | | + | |
} | } | ||
+ | |||
</ | </ | ||
- | R, G, and B reflect filtered values for Red, Green and Blue, respectively, | ||
+ | < | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | </ | ||
==== Result validation ==== | ==== Result validation ==== | ||
- | <todo @pczekalski> | + | You should be able to observe |
- | //Provide some result validation methods for self-assessment.// | + | |
===== FAQ ===== | ===== FAQ ===== | ||
- | **What is the range of the values for the integration time of the TCS sensor?**: | ||
- | <code c> | ||
- | #define TCS34725_INTEGRATIONTIME_2_4MS | ||
- | (0xFF) /**< 2.4ms - 1 cycle - Max Count: 1024 */ | ||
- | #define TCS34725_INTEGRATIONTIME_24MS | ||
- | (0xF6) /**< 24.0ms - 10 cycles - Max Count: 10240 */ | ||
- | #define TCS34725_INTEGRATIONTIME_50MS | ||
- | (0xEB) /**< 50.4ms - 21 cycles - Max Count: 21504 */ | ||
- | #define TCS34725_INTEGRATIONTIME_60MS | ||
- | (0xE7) /**< 60.0ms - 25 cycles - Max Count: 25700 */ | ||
- | #define TCS34725_INTEGRATIONTIME_101MS | ||
- | (0xD6) /**< 100.8ms - 42 cycles - Max Count: 43008 */ | ||
- | #define TCS34725_INTEGRATIONTIME_120MS | ||
- | (0xCE) /**< 120.0ms - 50 cycles - Max Count: 51200 */ | ||
- | #define TCS34725_INTEGRATIONTIME_154MS | ||
- | (0xC0) /**< 153.6ms - 64 cycles - Max Count: 65535 */ | ||
- | #define TCS34725_INTEGRATIONTIME_180MS | ||
- | (0xB5) /**< 180.0ms - 75 cycles - Max Count: 65535 */ | ||
- | #define TCS34725_INTEGRATIONTIME_199MS | ||
- | (0xAD) /**< 199.2ms - 83 cycles - Max Count: 65535 */ | ||
- | #define TCS34725_INTEGRATIONTIME_240MS | ||
- | (0x9C) /**< 240.0ms - 100 cycles - Max Count: 65535 */ | ||
- | #define TCS34725_INTEGRATIONTIME_300MS | ||
- | (0x83) /**< 300.0ms - 125 cycles - Max Count: 65535 */ | ||
- | #define TCS34725_INTEGRATIONTIME_360MS | ||
- | (0x6A) /**< 360.0ms - 150 cycles - Max Count: 65535 */ | ||
- | #define TCS34725_INTEGRATIONTIME_401MS | ||
- | (0x59) /**< 400.8ms - 167 cycles - Max Count: 65535 */ | ||
- | #define TCS34725_INTEGRATIONTIME_420MS | ||
- | (0x51) /**< 420.0ms - 175 cycles - Max Count: 65535 */ | ||
- | #define TCS34725_INTEGRATIONTIME_480MS | ||
- | (0x38) /**< 480.0ms - 200 cycles - Max Count: 65535 */ | ||
- | #define TCS34725_INTEGRATIONTIME_499MS | ||
- | (0x30) /**< 499.2ms - 208 cycles - Max Count: 65535 */ | ||
- | #define TCS34725_INTEGRATIONTIME_540MS | ||
- | (0x1F) /**< 540.0ms - 225 cycles - Max Count: 65535 */ | ||
- | #define TCS34725_INTEGRATIONTIME_600MS | ||
- | (0x06) /**< 600.0ms - 250 cycles - Max Count: 65535 */ | ||
- | #define TCS34725_INTEGRATIONTIME_614MS | ||
- | (0x00) /**< 614.4ms - 256 cycles - Max Count: 65535 */ | ||
- | </ | ||
- | **What | + | **What |
- | <code c> | + | \\ |
- | typedef enum { | + | **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. |
- | TCS34725_GAIN_1X = 0x00, /* No gain | + | \\ |
- | TCS34725_GAIN_4X = 0x01, /* 4x gain | + | **What is the maximum bit resolution for PWM?**: it is between 1 and 14 bits in this particular MCU. |
- | | + | \\ |
- | TCS34725_GAIN_60X = 0x03 /* 60x gain */ | + | **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. |
- | } tcs34725Gain_t; | + | |
- | </ | + | |
+ | <WRAP noprint> | ||
===== Project information ===== | ===== Project information ===== | ||
{{: | {{: | ||
Line 147: | Line 112: | ||
{{: | {{: | ||
</ | </ | ||
- | + | </ | |
- | + |