Hello,
I want to read the device id from a TCS34725 but it returns the last byte written in the communiation. (In this case TCS34725_ID.)
I am using this code:
#include "esp_log.h"
#include "driver/gpio.h"
#include <driver/i2c.h>
#include "freertos/FreeRTOS.h"
#include <stdio.h>
using namespace std;
#define TCS34725_ADDRESS 0x29
#define TCS34725_COMMAND_BIT 0x80
#define TCS34725_ID 0x12 /* 0x44 = TCS34721/TCS34725, 0x4D = TCS34723/TCS34727 */
#define SDA_PIN GPIO_NUM_5
#define SCL_PIN GPIO_NUM_4
#define ACK_CHECK_EN 0x1 /*!< I2C master will check ack from slave*/
#define ACK_CHECK_DIS 0x0 /*!< I2C master will not check ack from slave */
void task(void *ignore) {
uint8_t data = 0;
int ret;
i2c_cmd_handle_t cmd;
i2c_config_t conf;
conf.mode = I2C_MODE_MASTER;
conf.sda_io_num = SDA_PIN;
conf.scl_io_num = SCL_PIN;
conf.sda_pullup_en = GPIO_PULLUP_ENABLE;
conf.scl_pullup_en = GPIO_PULLUP_ENABLE;
conf.master.clk_speed = 100000;
i2c_param_config(I2C_NUM_0, &conf);
i2c_driver_install(I2C_NUM_0, I2C_MODE_MASTER, 0, 0, 0);
cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
i2c_master_write_byte(cmd, (TCS34725_ADDRESS << 1) | I2C_MASTER_WRITE, ACK_CHECK_EN);
i2c_master_write_byte(cmd, TCS34725_COMMAND_BIT, ACK_CHECK_EN);
i2c_master_write_byte(cmd, TCS34725_ID, ACK_CHECK_EN); // tried with ACK_CHECK_DIS also
i2c_master_stop(cmd);
ret = i2c_master_cmd_begin(I2C_NUM_0, cmd, 100 / portTICK_RATE_MS);
i2c_cmd_link_delete(cmd);
vTaskDelay(1000 / portTICK_PERIOD_MS);
cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
i2c_master_write_byte(cmd, (TCS34725_ADDRESS << 1) | I2C_MASTER_READ, ACK_CHECK_EN);
i2c_master_read_byte(cmd, &data, I2C_MASTER_NACK);
i2c_master_stop(cmd);
ret = i2c_master_cmd_begin(I2C_NUM_0, cmd, 100 / portTICK_RATE_MS);
i2c_cmd_link_delete(cmd);
if (ret == ESP_OK) {
printf("ID %.2x ", data);
}
else
{
printf("No ack");
}
printf("\n");
vTaskDelete(NULL);
}
#ifdef __cplusplus
extern "C" {
#endif
void app_main() {
xTaskCreatePinnedToCore(&task, "task", 2048, NULL, 5, NULL, 1);
}
#ifdef __cplusplus
}
#endif
And here are the write and read result from the logic analyzer:
Any help is appreciated!
Thanks & Regards,
ArpiABeka
ESP32 I2C read issue with TCS34725
Jump to
- English Forum
- Explore
- News
- General Discussion
- FAQ
- Documentation
- Documentation
- Sample Code
- Discussion Forum
- Hardware
- ESP-IDF
- ESP-BOX
- ESP-ADF
- ESP-MDF
- ESP-WHO
- ESP-SkaiNet
- ESP32 Arduino
- IDEs for ESP-IDF
- ESP-AT
- ESP IoT Solution
- ESP RainMaker
- Rust
- ESP8266
- Report Bugs
- Showcase
- Chinese Forum 中文社区
- 活动区
- 乐鑫活动专区
- 讨论区
- 全国大学生物联网设计竞赛乐鑫答疑专区
- ESP-IDF 中文讨论版
- 《ESP32-C3 物联网工程开发实战》书籍讨论版
- 中文文档讨论版
- ESP-AT 中文讨论版
- ESP-BOX 中文讨论版
- ESP IoT Solution 中文讨论版
- ESP-ADF 中文讨论版
- ESP Mesh 中文讨论版
- ESP Cloud 中文讨论版
- ESP-WHO 中文讨论版
- ESP-SkaiNet 中文讨论版
- ESP 生产支持讨论版
- 硬件问题讨论
- 项目展示
Who is online
Users browsing this forum: No registered users and 66 guests
- All times are UTC
- Top
- Delete cookies
About Us
Espressif Systems is a fabless semiconductor company providing cutting-edge low power WiFi SoCs and wireless solutions for wireless communications and Internet of Things applications. ESP8266EX and ESP32 are some of our products.