This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| en:iot-open:practical:hardware:sut:stm32:emb9b_1 [2024/03/02 21:10] – created ktokarz | en:iot-open:practical:hardware:sut:stm32:emb9b_1 [2024/05/02 12:02] (current) – created ktokarz | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ====== | + | ====== |
| - | This scenario presents how to handle | + | A colour sensor (TCS 34725) can detect |
| ===== Prerequisites ===== | ===== Prerequisites ===== | ||
| - | A good understanding of the PWM signal and duty cycle is necessary. We also use built-in timers | + | To implement this scenario, it is necessary to get familiar with at least one of the following scenarios first: |
| + | * [[en: | ||
| + | * [[en: | ||
| + | * [[en:iot-open: | ||
| + | and obligatory: | ||
| + | * [[en: | ||
| + | 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 ==== | ||
| - | Implement | + | Create |
| + | |||
| + | <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 ==== | ||
| - | Assuming | + | Check if you can see the LCD and RGB LED in the camera view. |
| + | <note important> | ||
| ==== Steps ==== | ==== Steps ==== | ||
| - | To use PWM in ESP32, it is best to use built-in '' | + | 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 |
| - | The '' | + | |
| === Step 1 === | === Step 1 === | ||
| - | Define some parameters, including channel numbers, PWM resolution (here 8-bit) and PWM frequency (5000Hz): | + | Included necessary libraries: |
| <code c> | <code c> | ||
| - | #define RGBLED_B_PIN 26 | + | #include < |
| - | #define RGBLED_G_PIN 21 | + | #include " |
| - | #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 === | ||
| - | Initialise 3 channels for PWM and make them dark: | + | Declare |
| <code c> | <code c> | ||
| - | ledcSetup(PWM1_Ch, PWM_Freq, PWM_Res); | + | static Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_300MS, TCS34725_GAIN_1X); |
| - | | + | uint16_t r, g, b, c, colorTemp, lux; |
| - | ledcSetup(PWM3_Ch, PWM_Freq, PWM_Res); | + | static bool isTCSOk = false; |
| - | ledcAttachPin(RGBLED_R_PIN, PWM1_Ch); | + | |
| - | | + | |
| - | ledcAttachPin(RGBLED_B_PIN, | + | |
| - | delay(100); | + | |
| - | ledcWrite(PWM1_Ch, | + | |
| - | ledcWrite(PWM2_Ch, | + | |
| - | ledcWrite(PWM3_Ch, | + | |
| </ | </ | ||
| - | To control | + | A word of explanation regarding |
| + | * '' | ||
| + | * '' | ||
| + | |||
| + | Refer to the Q&A section for the ranges. | ||
| === Step 3 === | === 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). | + | Initialise |
| - | <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> | ||
| - | | + | |
| - | for (int dutyCycle = 0; dutyCycle | + | </code> |
| - | | + | You communicate with the TCS sensor via the I2C interface. In standard and typical configurations there is no need to instantiate the '' |
| - | | + | |
| - | delay(20); // Delay for smooth transition | + | |
| - | } | + | |
| - | delay(100); | + | === Step 4 === |
| + | Besides reading raw values for channels R, G, B and C, the '' | ||
| - | // Decrease brightness | + | To read, use the following code: |
| - | | + | <code c> |
| - | | + | |
| - | | + | |
| - | | + | |
| + | | ||
| + | | ||
| } | } | ||
| - | |||
| </ | </ | ||
| + | R, G, and B reflect filtered values for Red, Green and Blue, respectively, | ||
| - | < | ||
| - | * '' | ||
| - | * '' | ||
| - | * '' | ||
| - | * '' | ||
| - | </ | ||
| ==== Result validation ==== | ==== Result validation ==== | ||
| - | You should be able to observe the pulsing colours of the RGB LED, increasing and decreasing brightness linearly. | + | Driving R, G and B channels for RGB LED that lights the sensors, you should be able to observe |
| ===== 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> |
| - | **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. | + | typedef enum { |
| - | \\ | + | TCS34725_GAIN_1X = 0x00, /* No gain |
| - | **What is the maximum bit resolution for PWM?**: it is between 1 and 14 bits in this particular MCU. | + | TCS34725_GAIN_4X = 0x01, /* 4x 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. | + | TCS34725_GAIN_60X = 0x03 /* 60x gain */ |
| + | } tcs34725Gain_t; | ||
| + | </ | ||
| + | **C channel reading is 0 and R, G or B readings are not reasonable**: | ||
| + | |||
| + | <WRAP noprint> | ||
| ===== Project information ===== | ===== Project information ===== | ||
| {{: | {{: | ||
| Line 111: | Line 155: | ||
| {{: | {{: | ||
| </ | </ | ||
| - | + | </ | |
| - | + | ||