TCS34725 Color Recognition Sensor
This module is a color sensor based on AMS's TCS3472XFN color light digital converter as its core. The sensor provides digital outputs of red, green, blue (RGB) and clear light sensing values. The integrated infrared blocking filter minimizes the infrared spectral component of incident light and enables accurate color measurement. It features high sensitivity, wide dynamic range and an infrared blocking filter. It minimizes IR and UV spectral component effects to produce accurate color measurements. It also has ambient light intensity detection and maskable interrupts. It communicates via the I2C interface. This design is based on the same design principle and provides 2 different form designs (square version / dual-hole version), giving users more choices for installation dimensions and environments. The dual-hole version is equipped with 2 LED lights to supplement lighting for objects.
Module Source
Purchase link: https://detail.tmall.com/item.htm?_u=52t4uge5c554&id=662903315676&spm=a1z09.2.0.0.47582e8dxs1caw Materials download link: https://pan.baidu.com/s/1z_5qOfe-YMbj0TYSbDD-sQ?pwd=6668 Extraction code: 6668
Specifications
Operating voltage: 3.3-5V Operating current: 2.5~330uA Output method: IIC Number of pins: 7 Pin
View Materials
I2C Address:
The I2C device address is 0X29
Note: 0X29 is a 7-bit device address; the 8-bit device address needs to be shifted one bit to the high bit, becoming 0X52.
I2C Write Timing:
First, the host sends a start signal, then combines its 7-bit I2C address with the write operation bit into 8-bit data and sends it to the slave. After receiving it, the slave responds with an acknowledge signal. The host then sends the command register address to the slave, and after receiving it, the slave sends a response signal. At this time, the host sends the value of the command register, the slave responds with an acknowledge signal, until the host sends a stop signal, ending this IIC write data operation.
I2C Read Timing:
First, the host sends a start signal, then combines its 7-bit I2C address with the write operation bit into 8-bit data and sends it to the slave. After receiving it, the slave responds with an acknowledge signal. The host then sends the command register address to the slave, and after receiving it, the slave sends a response signal. At this time, the host re-sends a start signal and combines its 7-bit address with the read operation bit into 8-bit data and sends it to the slave. After receiving the signal, the slave sends a response signal, then sends the value in its register to the host, and the host gives a response signal, until the host sends a stop signal, ending this communication.
Porting Process
View Pins
Port to Project
Our goal is to port the example to the ESP32-S3 dev board. Complete driver code has been provided for you. Follow the steps below to complete the porting.
For detailed instructions on creating folders and new .c and .h files, refer to section 1.4.2 in the [DHT11 Temperature and Humidity Sensor] chapter; we will not repeat it here.
However, here we change the file names bsp_dht11.c and bsp_dht11.h to bsp_tcs34725.c and bsp_tcs34725.h, and change the folder name to TCS34725.
Write Code
In the bsp_tcs34725.c file, write the following code.
/*
* LCSC-Openkits (LCKFB) software and hardware materials and related expansion board software and hardware materials are all open source on the official website.
* Dev board official website: www.lckfb.com
* Technical support resides on the forum; any technical questions are welcome for exchange and learning at any time.
* LCKFB Forum: club.szlcsc.com
* Follow our Bilibili account: [LCSC-Openkits (LCKFB)] to keep up with our latest updates!
* We do not make money by selling boards; we take cultivating engineers as our mission.
* Change Logs:
* Date Author Notes
* 2024-01-05 LCKFB-lp first version
*/
#include "bsp_tcs34725.h"
#include "stdio.h"
COLOR_RGBC rgb;
COLOR_HSL hsl;
void delay_ms(unsigned int ms)
{
vTaskDelay(ms / portTICK_PERIOD_MS);
}
void delay_us(unsigned int us)
{
ets_delay_us(us);
}
void delay_1ms(unsigned int ms)
{
vTaskDelay(ms / portTICK_PERIOD_MS);
}
void delay_1us(unsigned int us)
{
ets_delay_us(us);
}
/******************************************************************
* Function Name: TC34725_GPIO_Init
* Function Description: tc34725 pin initialization
* Function Parameters: None
* Function Return: None
* Author: LC
* Notes: None
******************************************************************/
void TC34725_GPIO_Init(void)
{
gpio_config_t lll_config = {
.pin_bit_mask = (1ULL<<TCS34725_SCL_PIN)|(1ULL<<TCS34725_SDA_PIN), // Configure pins
.mode =GPIO_MODE_OUTPUT, // Output mode
.pull_up_en = GPIO_PULLUP_ENABLE, // Enable pull-up
.pull_down_en = GPIO_PULLDOWN_DISABLE, // Disable pull-down
.intr_type = GPIO_INTR_DISABLE // Disable pin interrupt
};
gpio_config(&lll_config);
}
/******************************************************************
* Function Name: IIC_Start
* Function Description: IIC start timing
* Function Parameters: None
* Function Return: None
* Author: LC
* Notes: None
******************************************************************/
void IIC_Start(void)
{
SDA_OUT();
SDA(1);
delay_us(5);
SCL(1);
delay_us(5);
SDA(0);
delay_us(5);
SCL(0);
delay_us(5);
}
/******************************************************************
* Function Name: IIC_Stop
* Function Description: IIC stop signal
* Function Parameters: None
* Function Return: None
* Author: LC
* Notes: None
******************************************************************/
void IIC_Stop(void)
{
SDA_OUT();
SCL(0);
SDA(0);
SCL(1);
delay_us(5);
SDA(1);
delay_us(5);
}
/******************************************************************
* Function Name: IIC_Send_Ack
* Function Description: Host sends acknowledge or non-acknowledge signal
* Function Parameters: 0 sends acknowledge 1 sends non-acknowledge
* Function Return: None
* Author: LC
* Notes: None
******************************************************************/
void IIC_Send_Ack(unsigned char ack)
{
SDA_OUT();
SCL(0);
SDA(0);
delay_us(5);
if(!ack) SDA(0);
else SDA(1);
SCL(1);
delay_us(5);
SCL(0);
SDA(1);
}
/******************************************************************
* Function Name: I2C_WaitAck
* Function Description: Wait for slave acknowledge
* Function Parameters: None
* Function Return: 0=acknowledged 1=timeout no acknowledge
* Author: LC
* Notes: None
******************************************************************/
unsigned char I2C_WaitAck(void)
{
char ack = 0;
unsigned char ack_flag = 10;
SCL(0);
SDA(1);
SDA_IN();
delay_us(5);
SCL(1);
delay_us(5);
while( (SDA_GET()==1) && ( ack_flag ) )
{
ack_flag--;
delay_us(5);
}
if( ack_flag <= 0 )
{
IIC_Stop();
return 1;
}
else
{
SCL(0);
SDA_OUT();
}
return ack;
}
/******************************************************************
* Function Name: Send_Byte
* Function Description: Write one byte
* Function Parameters: dat - data to write
* Function Return: None
* Author: LC
* Notes: None
******************************************************************/
void Send_Byte(uint8_t dat)
{
int i = 0;
SDA_OUT();
SCL(0);// Pull clock low to start data transfer
for( i = 0; i < 8; i++ )
{
SDA( (dat & 0x80) >> 7 );
delay_us(1);
SCL(1);
delay_us(5);
SCL(0);
delay_us(5);
dat<<=1;
}
}
/******************************************************************
* Function Name: Read_Byte
* Function Description: IIC read timing
* Function Parameters: None
* Function Return: Data read
* Author: LC
* Notes: None
******************************************************************/
unsigned char Read_Byte(void)
{
unsigned char i,receive=0;
SDA_IN();// Set SDA as input
for(i=0;i<8;i++ )
{
SCL(0);
delay_us(5);
SCL(1);
delay_us(5);
receive<<=1;
if( SDA_GET() )
{
receive|=1;
}
delay_us(5);
}
SCL(0);
return receive;
}
/*******************************************************************************
* @brief Writes data to a slave device.
*
* @param slaveAddress - Adress of the slave device.
* @param dataBuffer - Pointer to a buffer storing the transmission data.
* @param bytesNumber - Number of bytes to write.
* @param stopBit - Stop condition control.
* Example: 0 - A stop condition will not be sent;
* 1 - A stop condition will be sent.
*******************************************************************************/
void TCS34725_I2C_Write(uint8_t slaveAddress, uint8_t* dataBuffer,uint8_t bytesNumber, uint8_t stopBit)
{
uint8_t i = 0;
IIC_Start();
Send_Byte((slaveAddress << 1) | 0x00); // Send slave address write command
I2C_WaitAck();
for(i = 0; i < bytesNumber; i++)
{
Send_Byte(*(dataBuffer + i));
I2C_WaitAck();
}
if(stopBit == 1) IIC_Stop();
}
/*******************************************************************************
* @brief Reads data from a slave device.
*
* @param slaveAddress - Adress of the slave device.
* @param dataBuffer - Pointer to a buffer that will store the received data.
* @param bytesNumber - Number of bytes to read.
* @param stopBit - Stop condition control.
* Example: 0 - A stop condition will not be sent;
* 1 - A stop condition will be sent.
*******************************************************************************/
void TCS34725_I2C_Read(uint8_t slaveAddress, uint8_t* dataBuffer, uint8_t bytesNumber, uint8_t stopBit)
{
uint8_t i = 0;
IIC_Start();
Send_Byte((slaveAddress << 1) | 0x01); // Send slave address read command
I2C_WaitAck();
for(i = 0; i < bytesNumber; i++)
{
if(i == bytesNumber - 1)
{
*(dataBuffer + i) = Read_Byte();// Send NACK on the last byte read
IIC_Send_Ack(1);
}
else
{
*(dataBuffer + i) = Read_Byte();
IIC_Send_Ack(0);
}
}
if(stopBit == 1) IIC_Stop();
}
/*******************************************************************************
* @brief Writes data into TCS34725 registers, starting from the selected
* register address pointer.
*
* @param subAddr - The selected register address pointer.
* @param dataBuffer - Pointer to a buffer storing the transmission data.
* @param bytesNumber - Number of bytes that will be sent.
*
* @return None.
*******************************************************************************/
void TCS34725_Write(uint8_t subAddr, uint8_t* dataBuffer, uint8_t bytesNumber)
{
uint8_t sendBuffer[10] = {0, };
uint8_t byte = 0;
sendBuffer[0] = subAddr | TCS34725_COMMAND_BIT;
for(byte = 1; byte <= bytesNumber; byte++)
{
sendBuffer[byte] = dataBuffer[byte - 1];
}
TCS34725_I2C_Write(TCS34725_ADDRESS, sendBuffer, bytesNumber + 1, 1);
}
/*******************************************************************************
* @brief Reads data from TCS34725 registers, starting from the selected
* register address pointer.
*
* @param subAddr - The selected register address pointer.
* @param dataBuffer - Pointer to a buffer that will store the received data.
* @param bytesNumber - Number of bytes that will be read.
*
* @return None.
*******************************************************************************/
void TCS34725_Read(uint8_t subAddr, uint8_t* dataBuffer, uint8_t bytesNumber)
{
subAddr |= TCS34725_COMMAND_BIT;
TCS34725_I2C_Write(TCS34725_ADDRESS, (uint8_t*)&subAddr, 1, 0);
TCS34725_I2C_Read(TCS34725_ADDRESS, dataBuffer, bytesNumber, 1);
}
/*******************************************************************************
* @brief TCS34725 set integration time
*
* @return None
*******************************************************************************/
void TCS34725_SetIntegrationTime(uint8_t time)
{
TCS34725_Write(TCS34725_ATIME, &time, 1);
}
/*******************************************************************************
* @brief TCS34725 set gain
*
* @return None
*******************************************************************************/
void TCS34725_SetGain(uint8_t gain)
{
TCS34725_Write(TCS34725_CONTROL, &gain, 1);
}
/*******************************************************************************
* @brief TCS34725 enable
*
* @return None
*******************************************************************************/
void TCS34725_Enable(void)
{
uint8_t cmd = TCS34725_ENABLE_PON;
TCS34725_Write(TCS34725_ENABLE, &cmd, 1);
cmd = TCS34725_ENABLE_PON | TCS34725_ENABLE_AEN;
TCS34725_Write(TCS34725_ENABLE, &cmd, 1);
//delay_s(600000);//delay_ms(3);// The delay should be placed after setting AEN
}
/*******************************************************************************
* @brief TCS34725 disable
*
* @return None
*******************************************************************************/
void TCS34725_Disable(void)
{
uint8_t cmd = 0;
TCS34725_Read(TCS34725_ENABLE, &cmd, 1);
cmd = cmd & ~(TCS34725_ENABLE_PON | TCS34725_ENABLE_AEN);
TCS34725_Write(TCS34725_ENABLE, &cmd, 1);
}
/*******************************************************************************
* @brief TCS34725 initialization
*
* @return ID - The value in the ID register
*******************************************************************************/
uint8_t TCS34725_Init(void)
{
uint8_t id=0;
TC34725_GPIO_Init();
TCS34725_Read(TCS34725_ID, &id, 1); // The ID of TCS34725 is 0x44, which can be used to determine whether the connection is successful; 0x4D is TCS34727;
if( (id==0x4D) | (id==0x44) )
{
TCS34725_SetIntegrationTime(TCS34725_INTEGRATIONTIME_24MS);
TCS34725_SetGain(TCS34725_GAIN_1X);
TCS34725_Enable();
return 1;
}
return 0;
}
/*******************************************************************************
* @brief TCS34725 get single channel data
*
* @return data - The conversion value of this channel
*******************************************************************************/
uint16_t TCS34725_GetChannelData(uint8_t reg)
{
uint8_t tmp[2] = {0,0};
uint16_t data;
TCS34725_Read(reg, tmp, 2);
data = (tmp[1] << 8) | tmp[0];
return data;
}
/*******************************************************************************
* @brief TCS34725 get data for each channel
*
* @return 1 - Conversion complete, data available
* 0 - Conversion not complete, data not available
*******************************************************************************/
uint8_t TCS34725_GetRawData(COLOR_RGBC *rgbc)
{
uint8_t status = TCS34725_STATUS_AVALID;
TCS34725_Read(TCS34725_STATUS, &status, 1);
if(status & TCS34725_STATUS_AVALID)
{
rgbc->c = TCS34725_GetChannelData(TCS34725_CDATAL);
rgbc->r = TCS34725_GetChannelData(TCS34725_RDATAL);
rgbc->g = TCS34725_GetChannelData(TCS34725_GDATAL);
rgbc->b = TCS34725_GetChannelData(TCS34725_BDATAL);
return 1;
}
return 0;
}
/******************************************************************************/
// RGB to HSL
void RGBtoHSL(COLOR_RGBC *Rgb, COLOR_HSL *Hsl)
{
uint8_t maxVal,minVal,difVal;
uint8_t r = Rgb->r*100/Rgb->c; //[0-100]
uint8_t g = Rgb->g*100/Rgb->c;
uint8_t b = Rgb->b*100/Rgb->c;
maxVal = max3v(r,g,b);
minVal = min3v(r,g,b);
difVal = maxVal-minVal;
// Calculate lightness
Hsl->l = (maxVal+minVal)/2; //[0-100]
if(maxVal == minVal)// If r=g=b, grayscale
{
Hsl->h = 0;
Hsl->s = 0;
}
else
{
// Calculate hue
if(maxVal==r)
{
if(g>=b)
Hsl->h = 60*(g-b)/difVal;
else
Hsl->h = 60*(g-b)/difVal+360;
}
else
{
if(maxVal==g)
Hsl->h = 60*(b-r)/difVal+120;
else
if(maxVal==b)
Hsl->h = 60*(r-g)/difVal+240;
}
// Calculate saturation
if(Hsl->l<=50)
Hsl->s=difVal*100/(maxVal+minVal); //[0-100]
else
Hsl->s=difVal*100/(200-(maxVal+minVal));
}
}
/******************************************************************************/2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
In the bsp_tcs34725.h file, write the following code.
/*
* LCSC-Openkits (LCKFB) software and hardware materials and related expansion board software and hardware materials are all open source on the official website.
* Dev board official website: www.lckfb.com
* Technical support resides on the forum; any technical questions are welcome for exchange and learning at any time.
* LCKFB Forum: club.szlcsc.com
* Follow our Bilibili account: [LCSC-Openkits (LCKFB)] to keep up with our latest updates!
* We do not make money by selling boards; we take cultivating engineers as our mission.
* Change Logs:
* Date Author Notes
* 2024-01-05 LCKFB-lp first version
*/
#ifndef _BSP_TCS34725_H_
#define _BSP_TCS34725_H_
#include <stdio.h>
#include <inttypes.h>
#include "sdkconfig.h"
#include "driver/gpio.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_rom_sys.h"
#include "esp_timer.h"
#include "driver/uart.h"
#include "rom/ets_sys.h"
#include "esp_system.h"
#include "driver/gptimer.h"
#include "esp_log.h"
#include "freertos/queue.h"
#include "driver/spi_master.h"
#include "nvs_flash.h"
#include "esp_adc/adc_cali_scheme.h"
#include "esp_adc/adc_cali.h"
#include "driver/adc.h"
#include "esp_adc_cal.h"
// Port porting
#define TCS34725_SCL_PIN 1
#define TCS34725_SDA_PIN 2
// Set SDA output mode
#define SDA_OUT() gpio_set_direction(TCS34725_SDA_PIN,GPIO_MODE_OUTPUT)
// Set SDA input mode
#define SDA_IN() gpio_set_direction(TCS34725_SDA_PIN,GPIO_MODE_INPUT)
// Get the level change of SDA pin
#define SDA_GET() gpio_get_level(TCS34725_SDA_PIN)
// SDA and SCL output
#define SDA(x) gpio_set_level(TCS34725_SDA_PIN, (x?1:0))
#define SCL(x) gpio_set_level(TCS34725_SCL_PIN, (x?1:0))
/******************************************************************************/
#define TCS34725_ADDRESS (0x29)
#define TCS34725_COMMAND_BIT (0x80)
#define TCS34725_ENABLE (0x00)
#define TCS34725_ENABLE_AIEN (0x10) /* RGBC Interrupt Enable */
#define TCS34725_ENABLE_WEN (0x08) /* Wait enable - Writing 1 activates the wait timer */
#define TCS34725_ENABLE_AEN (0x02) /* RGBC Enable - Writing 1 actives the ADC, 0 disables it */
#define TCS34725_ENABLE_PON (0x01) /* Power on - Writing 1 activates the internal oscillator, 0 disables it */
#define TCS34725_ATIME (0x01) /* Integration time */
#define TCS34725_WTIME (0x03) /* Wait time (if TCS34725_ENABLE_WEN is asserted) */
#define TCS34725_WTIME_2_4MS (0xFF) /* WLONG0 = 2.4ms WLONG1 = 0.029s */
#define TCS34725_WTIME_204MS (0xAB) /* WLONG0 = 204ms WLONG1 = 2.45s */
#define TCS34725_WTIME_614MS (0x00) /* WLONG0 = 614ms WLONG1 = 7.4s */
#define TCS34725_AILTL (0x04) /* Clear channel lower interrupt threshold */
#define TCS34725_AILTH (0x05)
#define TCS34725_AIHTL (0x06) /* Clear channel upper interrupt threshold */
#define TCS34725_AIHTH (0x07)
#define TCS34725_PERS (0x0C) /* Persistence register - basic SW filtering mechanism for interrupts */
#define TCS34725_PERS_NONE (0b0000) /* Every RGBC cycle generates an interrupt */
#define TCS34725_PERS_1_CYCLE (0b0001) /* 1 clean channel value outside threshold range generates an interrupt */
#define TCS34725_PERS_2_CYCLE (0b0010) /* 2 clean channel values outside threshold range generates an interrupt */
#define TCS34725_PERS_3_CYCLE (0b0011) /* 3 clean channel values outside threshold range generates an interrupt */
#define TCS34725_PERS_5_CYCLE (0b0100) /* 5 clean channel values outside threshold range generates an interrupt */
#define TCS34725_PERS_10_CYCLE (0b0101) /* 10 clean channel values outside threshold range generates an interrupt */
#define TCS34725_PERS_15_CYCLE (0b0110) /* 15 clean channel values outside threshold range generates an interrupt */
#define TCS34725_PERS_20_CYCLE (0b0111) /* 20 clean channel values outside threshold range generates an interrupt */
#define TCS34725_PERS_25_CYCLE (0b1000) /* 25 clean channel values outside threshold range generates an interrupt */
#define TCS34725_PERS_30_CYCLE (0b1001) /* 30 clean channel values outside threshold range generates an interrupt */
#define TCS34725_PERS_35_CYCLE (0b1010) /* 35 clean channel values outside threshold range generates an interrupt */
#define TCS34725_PERS_40_CYCLE (0b1011) /* 40 clean channel values outside threshold range generates an interrupt */
#define TCS34725_PERS_45_CYCLE (0b1100) /* 45 clean channel values outside threshold range generates an interrupt */
#define TCS34725_PERS_50_CYCLE (0b1101) /* 50 clean channel values outside threshold range generates an interrupt */
#define TCS34725_PERS_55_CYCLE (0b1110) /* 55 clean channel values outside threshold range generates an interrupt */
#define TCS34725_PERS_60_CYCLE (0b1111) /* 60 clean channel values outside threshold range generates an interrupt */
#define TCS34725_CONFIG (0x0D)
#define TCS34725_CONFIG_WLONG (0x02) /* Choose between short and long (12x) wait times via TCS34725_WTIME */
#define TCS34725_CONTROL (0x0F) /* Set the gain level for the sensor */
#define TCS34725_ID (0x12) /* 0x44 = TCS34721/TCS34725, 0x4D = TCS34723/TCS34727 */
#define TCS34725_STATUS (0x13)
#define TCS34725_STATUS_AINT (0x10) /* RGBC Clean channel interrupt */
#define TCS34725_STATUS_AVALID (0x01) /* Indicates that the RGBC channels have completed an integration cycle */
#define TCS34725_CDATAL (0x14) /* Clear channel data */
#define TCS34725_CDATAH (0x15)
#define TCS34725_RDATAL (0x16) /* Red channel data */
#define TCS34725_RDATAH (0x17)
#define TCS34725_GDATAL (0x18) /* Green channel data */
#define TCS34725_GDATAH (0x19)
#define TCS34725_BDATAL (0x1A) /* Blue channel data */
#define TCS34725_BDATAH (0x1B)
#define TCS34725_INTEGRATIONTIME_2_4MS 0xFF /**< 2.4ms - 1 cycle - Max Count: 1024 */
#define TCS34725_INTEGRATIONTIME_24MS 0xF6 /**< 24ms - 10 cycles - Max Count: 10240 */
#define TCS34725_INTEGRATIONTIME_50MS 0xEB /**< 50ms - 20 cycles - Max Count: 20480 */
#define TCS34725_INTEGRATIONTIME_101MS 0xD5 /**< 101ms - 42 cycles - Max Count: 43008 */
#define TCS34725_INTEGRATIONTIME_154MS 0xC0 /**< 154ms - 64 cycles - Max Count: 65535 */
#define TCS34725_INTEGRATIONTIME_240MS 0x9C /**< 240ms - 100 cycles - Max Count: 65535 */
#define TCS34725_INTEGRATIONTIME_700MS 0x00 /**< 700ms - 256 cycles - Max Count: 65535 */
#define TCS34725_GAIN_1X 0x00 /**< No gain */
#define TCS34725_GAIN_4X 0x01 /**< 4x gain */
#define TCS34725_GAIN_16X 0x02 /**< 16x gain */
#define TCS34725_GAIN_60X 0x03 /**< 60x gain */
/******************************************************************************/
/******************************************************************************/
#define max3v(v1, v2, v3) ((v1)<(v2)? ((v2)<(v3)?(v3):(v2)):((v1)<(v3)?(v3):(v1)))
#define min3v(v1, v2, v3) ((v1)>(v2)? ((v2)>(v3)?(v3):(v2)):((v1)>(v3)?(v3):(v1)))
typedef struct{
unsigned short c; //[0-65536]
unsigned short r;
unsigned short g;
unsigned short b;
}COLOR_RGBC;//RGBC
typedef struct{
unsigned short h; //[0,360]
unsigned char s; //[0,100]
unsigned char l; //[0,100]
}COLOR_HSL;//HSL
/******************************************************************************/
extern COLOR_RGBC rgb;
extern COLOR_HSL hsl;
void delay_us(unsigned int us);
void delay_ms(unsigned int ms);
void delay_1us(unsigned int us);
void delay_1ms(unsigned int ms);
uint8_t TCS34725_Init(void);
uint8_t TCS34725_GetRawData(COLOR_RGBC *rgbc);
void RGBtoHSL(COLOR_RGBC *Rgb, COLOR_HSL *Hsl);
#endif2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
Porting Verification
In the main function of your own project, write the following.
/*
* LCSC-Openkits (LCKFB) software and hardware materials and related expansion board software and hardware materials are all open source on the official website.
* Dev board official website: www.lckfb.com
* Technical support resides on the forum; any technical questions are welcome for exchange and learning at any time.
* LCKFB Forum: club.szlcsc.com
* Follow our Bilibili account: [LCSC-Openkits (LCKFB)] to keep up with our latest updates!
* We do not make money by selling boards; we take cultivating engineers as our mission.
* Change Logs:
* Date Author Notes
* 2024-01-05 LCKFB-lp first version
*/
#include <stdio.h>
#include "bsp_tcs34725.h"
void app_main(void)
{
float temp;
TCS34725_Init();
printf("start\r\n");
delay_1ms(1000);
while(1)
{
TCS34725_GetRawData(&rgb); // Read twice; during actual testing, it was found that the color read is always the previous color
RGBtoHSL(&rgb,&hsl);
printf("R=%d G=%d B=%d C=%d\r\n",rgb.r,rgb.g,rgb.b,rgb.c);
puts("- - - - - - - - - - - - -");
printf("H=%d S=%d L=%d\r\n",hsl.h,hsl.s,hsl.l);
puts("- - - - - - - - - - - - -");
delay_1ms(1000);
}
}2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
Power-on effect:
Driver code:
File Download
📌 Materials Download Center (click to jump)
📌 In the Materials Download Center -> Module Porting Materials Download, inside the compressed package of this chapter.