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:emb9b_1 [2024/02/28 21:21] – [Steps] pczekalski | en:iot-open:practical:hardware:sut:esp32:emb9b_1 [2025/04/28 20:34] (current) – [FAQ] pczekalski | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== EMB9B: | + | ====== EMB9B: |
- | 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: | ||
+ | A good understanding of the hardware timers is essential if you plan to use asynchronous programming (see note below). Consider getting familiar with the following: | ||
+ | * [[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, | + | #define SCL 4 |
- | | + | #define SDA 5 |
- | | + | |
- | | + | static Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_300MS, TCS34725_GAIN_1X); |
- | | + | uint16_t r, g, b, c, colorTemp, lux; |
- | ledcAttachPin(RGBLED_B_PIN, PWM3_Ch); | + | static bool isTCSOk = false; |
- | delay(100); | + | |
- | ledcWrite(PWM1_Ch,0); | + | |
- | ledcWrite(PWM2_Ch,0); | + | |
- | | + | |
</ | </ | ||
- | 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 <= 100; dutyCycle++) { | + | |
- | // Gradually increase duty cycle for Red LED | + | |
- | ledcWrite(PWM3_Ch, dutyCycle); | + | |
- | delay(20); // Delay for smooth transition | + | |
- | } | + | |
delay(100); | delay(100); | ||
+ | ... | ||
- | | + | |
- | for (int dutyCycle = 100; dutyCycle >= 0; dutyCycle--) { | + | </code> |
- | // Gradually decrease duty cycle for Red LED | + | 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 | + | |
- | } | + | |
+ | <note important> | ||
+ | |||
+ | === Step 4 === | ||
+ | Besides reading raw values for channels R, G, B and C, the '' | ||
+ | |||
+ | To read, use the following code: | ||
+ | <code c> | ||
+ | if(isTCSOk) | ||
+ | { | ||
+ | tcs.getRawData(& | ||
+ | colorTemp = tcs.calculateColorTemperature_dn40(r, | ||
+ | lux = tcs.calculateLux(r, | ||
+ | } | ||
</ | </ | ||
+ | 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 // | ||
+ | <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 | + | typedef enum { |
- | \\ | + | TCS34725_GAIN_1X = 0x00, / |
- | **What is the maximum bit resolution for PWM?**: it is between 1 and 14 bits in this particular MCU. | + | TCS34725_GAIN_4X = 0x01, / |
+ | | ||
+ | | ||
+ | } tcs34725Gain_t; | ||
+ | </ | ||
+ | |||
+ | **C channel reading | ||
+ | <WRAP noprint> | ||
===== Project information ===== | ===== Project information ===== | ||
{{: | {{: | ||
Line 109: | Line 167: | ||
{{: | {{: | ||
</ | </ | ||
- | + | </ | |
- | + |