how can use i2c for external sensor
Posted: Tue Nov 21, 2017 9:32 am
I want to use i2c for external sensor.
Can anyone tell me that which pin should i use.
and how can i read data from the external sensor.I have also mention my code as below.
I am trying to read device_id of external sensor.but i could not receive/communicate with it.
void i2c_master_init(void)
{
int i2c_master_port = I2C_MASTER_NUM;
i2c_config_t conf;
conf.mode = I2C_MODE_MASTER;
conf.sda_io_num = I2C_MASTER_SDA_IO;
conf.sda_pullup_en = GPIO_PULLUP_ENABLE;
conf.scl_io_num = I2C_MASTER_SCL_IO;
conf.scl_pullup_en = GPIO_PULLUP_ENABLE;
conf.master.clk_speed = I2C_MASTER_FREQ_HZ;
i2c_param_config(i2c_master_port, &conf);
i2c_driver_install(i2c_master_port, conf.mode,I2C_EXAMPLE_MASTER_RX_BUF_DISABLE,I2C_EXAMPLE_MASTER_TX_BUF_DISABLE, 0);
}
//i have used gpio25 as SDA and gpio26 as SCL.
esp_err_t i2c_master_read_data_kt(char* register_address, uint8_t* data_rd, size_t size)
{
if (size == 0) {
return ESP_OK;
}
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
i2c_master_write_byte(cmd, ( SLAVE_ADDRESS << 1 ) | READ_BIT, ACK_CHECK_EN);
i2c_master_write_byte(cmd,0x00 ,ACK_CHECK_EN);
//i2c_master_write(cmd,0x00,size,ACK_CHECK_EN);
if (size > 1) {
i2c_master_read(cmd, data_rd, size - 1, ACK_VAL);
}
i2c_master_read_byte(cmd, data_rd + size - 1, NACK_VAL);
i2c_master_stop(cmd);
esp_err_t ret = i2c_master_cmd_begin(I2C_MASTER_NUM, cmd, 1000 / portTICK_RATE_MS);
i2c_cmd_link_delete(cmd);
return ret;
}
// i have put register-value(device_id address) directly as 0x00.
tell me steps to communicate with external sensor using i2c.
Can anyone tell me that which pin should i use.
and how can i read data from the external sensor.I have also mention my code as below.
I am trying to read device_id of external sensor.but i could not receive/communicate with it.
void i2c_master_init(void)
{
int i2c_master_port = I2C_MASTER_NUM;
i2c_config_t conf;
conf.mode = I2C_MODE_MASTER;
conf.sda_io_num = I2C_MASTER_SDA_IO;
conf.sda_pullup_en = GPIO_PULLUP_ENABLE;
conf.scl_io_num = I2C_MASTER_SCL_IO;
conf.scl_pullup_en = GPIO_PULLUP_ENABLE;
conf.master.clk_speed = I2C_MASTER_FREQ_HZ;
i2c_param_config(i2c_master_port, &conf);
i2c_driver_install(i2c_master_port, conf.mode,I2C_EXAMPLE_MASTER_RX_BUF_DISABLE,I2C_EXAMPLE_MASTER_TX_BUF_DISABLE, 0);
}
//i have used gpio25 as SDA and gpio26 as SCL.
esp_err_t i2c_master_read_data_kt(char* register_address, uint8_t* data_rd, size_t size)
{
if (size == 0) {
return ESP_OK;
}
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
i2c_master_write_byte(cmd, ( SLAVE_ADDRESS << 1 ) | READ_BIT, ACK_CHECK_EN);
i2c_master_write_byte(cmd,0x00 ,ACK_CHECK_EN);
//i2c_master_write(cmd,0x00,size,ACK_CHECK_EN);
if (size > 1) {
i2c_master_read(cmd, data_rd, size - 1, ACK_VAL);
}
i2c_master_read_byte(cmd, data_rd + size - 1, NACK_VAL);
i2c_master_stop(cmd);
esp_err_t ret = i2c_master_cmd_begin(I2C_MASTER_NUM, cmd, 1000 / portTICK_RATE_MS);
i2c_cmd_link_delete(cmd);
return ret;
}
// i have put register-value(device_id address) directly as 0x00.
tell me steps to communicate with external sensor using i2c.